Here I will explain how to check the username availability using asp.net Ajax .
Now we can see how to check username availability in our application without postback for that First add AjaxControlToolkit reference to your application and add
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Check Username availability Using Ajax</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="scriptmanager1" runat="server"> </asp:ScriptManager> <div> <asp:UpdatePanel ID="PnlUsrDetails" runat="server"> <ContentTemplate> <table> <tr> <td> UserName: </td> <td> <asp:TextBox ID="txtUsername" runat="server" AutoPostBack="true" ontextchanged="txtUsername_TextChanged"/> </td> <td> <div id="checkusername" runat="server" Visible="false"> <asp:Image ID="imgstatus" runat="server" Width="17px" Height="17px"/> <asp:Label ID="lblStatus" runat="server"></asp:Label> </div> </td> </tr> </table> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html> |
protected void txtUsername_TextChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtUsername.Text)) { SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"); con.Open(); SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con); cmd.Parameters.AddWithValue("@Name", txtUsername.Text); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { checkusername.Visible = true; imgstatus.ImageUrl = "NotAvailable.jpg"; lblStatus.Text = "UserName Already Taken"; } else { checkusername.Visible = true; imgstatus.ImageUrl = "Icon_Available.gif"; lblStatus.Text = "UserName Available"; } } else { checkusername.Visible = false; } } | |
![]() |

Now it's ok we are able to show the username availability using ajax but during the time of getting the data from database if we show the progressbar like username checking in yahoo registration page and making the textbox disable during the time of getting the data it's very nice for looking for that check this one here how to show progressbar during username check availability
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
27 comments :
This does not seem to work for me.
Please check your code whether you written correct code or not because it's working fine.
thanks a lot
Thank you... This is very useful.
Dear Sir
Can your tell me
Error: "Element 'ScriptManager' is not a known element. This can occur if there is a compilation error in the website, or the web.config file is missing"
why this error is comming in my visual studio 2010 again again
Thank u very much, the code works fine.
I would like to know if the username check can be fired automatically. In other words, it would start automatically when someone types in something.
sir,
i want to take a data from database on radio button check changed using ajax .
is it possible .
Hi,
This code is taking 6 second to check user is valid or not on local machine but when same is upload on live, it is taking 45 sec to check !! why it is so ??
Dear sir,
I'm trying to use this code to validate my textbox but nothing appear and it always show "UserName Available" even in database has that user.
++++++++++++++++++++
and here is the code behide
protected void tbUser_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(tbUser.Text))
{
SqlConnection con = new SqlConnection("Data Source=BBB-PONGCHAYON_\\SQLEXPRESS;Initial Catalog=MA2;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from staff where s_name = @Name", con);
cmd.Parameters.AddWithValue("@Name", tbUser.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
//imgstatus.ImageUrl = "NotAvailable.jpg";
lblResult.Text = "UserName Already Taken";
}
else
{
checkusername.Visible = true;
//imgstatus.ImageUrl = "Icon_Available.gif";
lblResult.Text = "UserName Available";
lblUser.Text = "@Name";
}
}
else
{
checkusername.Visible = false;
}
}
++++++++++++++++
I'm trying to check the value of the tbuser.text which pass to @Name. and it is the value I input.
I have no idea what is the mistake? Anyone could help?
I do pass the user @Name to lblUser.Text and it show the correct user too.
Best regards,
Labura
Dear Sir
I have a question when i have created simple registration page which starts with
FirstName
LastName
Gender
Date of Birth
...
...
..
etc
Whether i need to declare connection outside the function (ie. txtUserName.Text) in order to implement check availability!
Password text box getting empty..
dear sir
thank you
Dear Sir,
please post some articles which shows the use of LINQ to SQL, because it is frequently being asked in interviews.
Dear Sir,
Thank u very much to provide these type of code which is helpful to fresher like me...
Don't work in Master Page Content
Excellent.Thanks Thanks Thanks
hey pls send me the example how to send friend requset in asp.net in chat application
its urgent rgt now pls,,,,,,,,
Thanks a lot for such a great article.
Very Nice Brother
give me pl/sql stored procedure example
hai........it's really great.....thanks for your code
It giving problem that Error 2 The modifier 'protected' is not valid for this item
how can impliment the email address is already register and send new password(if it click forgot password) to that perticular email address
Nice Article, good work
Thankyou sirrrrr