954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Using Ajax to check username availability in ASP.NET

0
By deepz on Apr 22nd, 2011 2:18 pm

Hi,
This code checks the availability of username from the database.

Default.aspx

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

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

Default.aspx.cs

using System.Data.SqlClient;

protected void txtUsername_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtUsername.Text))
{
SqlConnection con = new SqlConnection("your connection string");
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;
}
}
Hi, This code checks the availability of username from the database.




Help Fully Link
http://forums.asp.net/t/1375556.aspx/1

crishlay
Junior Poster
132 posts since May 2010
Reputation Points: 10
Solved Threads: 16
 
pinkygirl
Junior Poster in Training
82 posts since Oct 2010
Reputation Points: 7
Solved Threads: 2
 

use ajax , xmlhttp or json for verification of login

aspproject
Junior Poster in Training
Banned
78 posts since Apr 2011
Reputation Points: 1
Solved Threads: 3
Infraction Points: 5
 

thanks a lot !

its really good.

partha.asp
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You