Introduction:
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
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
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. |
|||
|
|||
34 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
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
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
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
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.
It's working.thanks a lot.
I am unable to download the files in the link provided. It opens a new page and not downloading any files.
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???
Note: Only a member of this blog may post a comment.