Check username availability using asp.net ajax


DATABASE TABLE:

CONNECTION STRING:

first prepare connection string in web.config file under configuration tag  as follows:

<connectionStrings>
<add name=”ConnectionString” connectionString=”Data Source=server2;Initial Catalog=Venkat123;Persist Security Info=True;User ID=sa;Password=123″/>
</connectionStrings>

WEBFORM DESIGN:

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title>Check Username availability Using Ajax</title>
<style type=”text/css”>
.waitingdiv {
background-color: #F5F8FA;
border: 1px solid #5A768E;
color: #333333;
font-size: 93%;
margin-bottom: 1em;
margin-top: 0.2em;
padding: 8px 12px;
width: 8.4em;
}
</style>

</head>
<body>
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”scriptmanager1″ runat=”server”>
</asp:ScriptManager>
<script type=”text/javascript”>
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
var state = document.getElementById(‘loadingdiv’).style.display;
if (state == ‘block’) {
document.getElementById(‘loadingdiv’).style.display = ‘none’;
} else {
document.getElementById(‘loadingdiv’).style.display = ‘block’;
}
args.get_postBackElement().disabled = true;
}
</script>
<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”/>
&nbsp;&nbsp;&nbsp;
<asp:Label ID=”lblStatus” runat=”server”></asp:Label>
</div>
</td>
</tr>
</table>
<div class=”waitingdiv” id=”loadingdiv” style=”display:none; margin-left:5.3em”>
<img src=”LoadingImage.gif” alt=”Loading” />Please wait…
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

CODE BEHIND:

protected void txtUsername_TextChanged(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(txtUsername.Text))
{
SqlConnection con = new SqlConnection(“Data Source=server2;Initial Catalog=Venkat123;Persist Security Info=True;User ID=sa;Password=123”);
con.Open();
SqlCommand cmd = new SqlCommand(“select * from Emp where UserName=@Name”, con);
cmd.Parameters.AddWithValue(“@Name”, txtUsername.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = “images.jpg”;
lblStatus.Text = “UserName Already Taken”;
System.Threading.Thread.Sleep(2000);
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = “download.jpg”;
lblStatus.Text = “UserName Available”;
System.Threading.Thread.Sleep(2000);
}
}
else
{
checkusername.Visible = false;
}
}
}

OUTPUTRESULT:

DOWNLOAD SAMPLE CODE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s