Saturday, April 28, 2012

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

ntroduction:

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

No comments:

Post a Comment