Name already exists while registration using asp.net c#

Introduction:

  How to validate name existing or  not while registering using asp.net with c# 3.5

Solution:

 
<asp:TextBox id="txtUsername" TextMode="SingleLine"  runat="server" 
         ontextchanged="txtUsername_TextChanged"  />
 
 
In Default.aspx.cs:

private string GetConnectionString()
       {

          return System.Configuration.ConfigurationManager.ConnectionStrings["dbconstr"].Connectionstring;
       }


txtUsername_TextChanged

{

   string iname;

  sqlConnection con=new sqlConnection(GetConnectionString());

 con.Open();

 String sqlStmt=string.Empty;

 sqlStmt="select count(username) as username  from Table1 where username = ' " + txt_name.Text + " ' ";

 sqlCommand cmd=new sqlCommand(sqlStmt,con);

sqlDataReader dr;

dr=cmd.ExecuteReader();

while(dr.Read())
{
  iname=dr["username"].ToString();

 if(iname == "0")
 { 

  Response.write("Your Name is Available");
 }

 else
 {
   Response.write("Your Name is already Exists");
  }


}

Comments