Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to check username availability using asp.net ajax or check username availability in asp.net using ajax

Mar 31, 2011
Introduction:

Here I will explain how to check the username availability using asp.net Ajax .


Description:
I designed one registration page in that user will enter userdetails like username, password etc at the time of enter username in textbox I need to validate the username whether that username available or already taken by any other user for that I am checking username availability in database based on username. Here I am getting one problem that is after enter username in textbox I am getting the data from database and showing the result on page like whether that username available or not but at that time page gets postback and losing the entered values in textboxes for that reason I used ajax concept to check username availability.  

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 

<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" tagPrefix="ajax" %>

To your aspx page and design your page likes this


<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>
If you observe above code I placed textbox in update panel to allow only partial postback based on that we can avoid the complete postaback of page during getting the data from database and I set property AutoPostBack="true" And used ontextchanged="txtUsername_TextChanged" event to display result after enter the text in textbox.

Now add using System.Data.SqlClient; reference in codebehind and write the following code to get the username from database


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;
}
}


Demo

Download sample code attached


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 RSS subscribe by email Subscribe by Email

34 comments :

Anonymous said...

This does not seem to work for me.

Suresh Dasari said...

Please check your code whether you written correct code or not because it's working fine.

Anonymous said...

thanks a lot

Anonymous said...

Thank you... This is very useful.

Anonymous said...

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

yazz said...

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.

Ajay said...

sir,
i want to take a data from database on radio button check changed using ajax .
is it possible .

SonalGawde said...

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 ??

labura said...

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

Anonymous said...

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!

Anonymous said...

Password text box getting empty..

vamsikrishna said...

dear sir

thank you

Anonymous said...

Dear Sir,
please post some articles which shows the use of LINQ to SQL, because it is frequently being asked in interviews.

Anonymous said...

Dear Sir,

Thank u very much to provide these type of code which is helpful to fresher like me...

Anonymous said...

Don't work in Master Page Content

Anonymous said...

Excellent.Thanks Thanks Thanks

parul gupta said...

hey pls send me the example how to send friend requset in asp.net in chat application

parul gupta said...

its urgent rgt now pls,,,,,,,,

Unknown said...

Thanks a lot for such a great article.

Danish Mugal said...
This comment has been removed by the author.
Anonymous said...

Very Nice Brother

Anonymous said...

give me pl/sql stored procedure example

Unknown said...

hai........it's really great.....thanks for your code

rohidas said...

It giving problem that Error 2 The modifier 'protected' is not valid for this item

Anonymous said...

how can impliment the email address is already register and send new password(if it click forgot password) to that perticular email address

Anand Manmohan Bohra said...

Nice Article, good work

Nag said...

Thankyou sirrrrr

Help said...

After enter name in the textbox next i am press the tab previously i am enter the text deleted,The textbox automatically refressed

I don't want that after i am pressing tab also what i am enter value not refress the text box

Alex said...

I used your code .It worked properly .But my other validators on other textboxes have stopped working . Even the simple Required field Validators doesn't work. plz suggest me a solution asap.

p.s. i use visual studio 2012 and i installed ajax control toolkit yesterday only from the manage nuGet packages ...
Thanking You in Advance

bhagat said...

Dear sir
i have same requirement but in entity framework
give me code for that sir
how can i check if mail id is already exists in database using entity framework

Anonymous said...

sir
pls tell me the when i enter the name in textbox it shows error if the name is entered in the database.pls solve it in N-Tier architcture.

udani_wijesinghe said...

It's working.thanks a lot.

Satya said...

I am unable to download the files in the link provided. It opens a new page and not downloading any files.

Unknown said...

i have one column in table and i want to set 0 or 1 to that column depending on button click and also set this to specific id...is any query to do this???

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.