Check Username or Email availability in asp.net using Ajax C# and VB.Net
In this post i'm explaining how to check username or email availability using ajax in asp.net user registration page.
You may also like to read one of my previous posts where i described how to Create User Registration Form In AspNet.
For this example create a table Users in sql server database with ID,Username and Email columns and add some records in it.
Drag and Place ScriptManager and Ajax UpdatePanel on the page, and inside ContentTemplate of update panel place two textbox for username and email, two image control to display images and two label control to display related messages.
Set AutoPostBack property of textbox to true.
HTML SOURCE OF PAGE
Write below mentioned code in TextChanged Event of Username textbox
C# CODE
VB.NET CODE
Similarly we can check email availability by writing following code in TextChanged Event of email textbox
In this post i'm explaining how to check username or email availability using ajax in asp.net user registration page.
You may also like to read one of my previous posts where i described how to Create User Registration Form In AspNet.
For this example create a table Users in sql server database with ID,Username and Email columns and add some records in it.
Drag and Place ScriptManager and Ajax UpdatePanel on the page, and inside ContentTemplate of update panel place two textbox for username and email, two image control to display images and two label control to display related messages.
Set AutoPostBack property of textbox to true.
HTML SOURCE OF PAGE
1: <form id="form1" runat="server">
2: <div>
3: <asp:ScriptManager ID="ScriptManager1" runat="server"/>
4: </div>
5: <asp:UpdatePanel ID="UpdatePanel1" runat="server">
6: <ContentTemplate>
7: <table><tr><td>UserName: </td>
8: <td>
9: <asp:TextBox ID="txtUserName" runat="server"
10: ontextchanged="txtUserName_TextChanged"
11: AutoPostBack="True">
12: </asp:TextBox></td>
13: <td>
14: <asp:Image ID="imgUser" runat="server" Visible="false"/>
15: </td>
16: <td><asp:Label ID="lblUser" runat="server"/></td></tr>
17:
18: <tr><td>Email ID: </td>
19: <td>
20: <asp:TextBox ID="txtEmail" runat="server"
21: AutoPostBack="True"
22: ontextchanged="txtEmail_TextChanged">
23: </asp:TextBox></td>
24: <td>
25: <asp:Image ID="imgEmail" runat="server" Visible="false"/>
26: </td>
27: <td><asp:Label ID="lblEmail" runat="server"/></td></tr>
28: </table>
29: </ContentTemplate>
30: </asp:UpdatePanel>
31: </form>
Write below mentioned code in TextChanged Event of Username textbox
C# CODE
01
protected
void
txtUserName_TextChanged(
object
sender, EventArgs e)
02
{
03
if
(txtUserName.Text !=
string
.Empty)
04
{
05
string
strConnection = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
06
string
strSelect =
"SELECT COUNT(*) FROM Users WHERE Username = @Username"
;
07
SqlConnection con =
new
SqlConnection(strConnection);
08
SqlCommand cmd =
new
SqlCommand(strSelect,con);
09
10
SqlParameter username =
new
SqlParameter(
"@Username"
, SqlDbType.VarChar);
11
username.Value = txtUserName.Text.Trim().ToString();
12
cmd.Parameters.Add(username);
13
con.Open();
14
int
result = (Int32)cmd.ExecuteScalar();
15
con.Close();
16
17
if
(result >= 1)
18
{
19
imgUser.ImageUrl =
"unavailable.png"
;
20
imgUser.Visible =
true
;
21
lblUser.Text =
"Username not available"
;
22
lblUser.ForeColor = System.Drawing.Color.Red;
23
}
24
else
25
{
26
imgUser.ImageUrl =
"tick.png"
;
27
imgUser.Visible =
true
;
28
lblUser.Text =
"Available"
;
29
lblUser.ForeColor = System.Drawing.Color.Green;
30
}
31
}
32
}
VB.NET CODE
01
Protected
Sub
txtUserName_TextChanged(sender
As
Object
, e
As
EventArgs)
02
If
txtUserName.Text <>
String
.Empty
Then
03
Dim
strConnection
As
String
= ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString
04
Dim
strSelect
As
String
=
"SELECT COUNT(*) FROM Users WHERE Username = @Username"
05
Dim
con
As
New
SqlConnection(strConnection)
06
Dim
cmd
As
New
SqlCommand(strSelect, con)
07
08
Dim
username
As
New
SqlParameter(
"@Username"
, SqlDbType.VarChar)
09
username.Value = txtUserName.Text.Trim().ToString()
10
cmd.Parameters.Add(username)
11
con.Open()
12
Dim
result
As
Integer
=
DirectCast
(cmd.ExecuteScalar(), Int32)
13
con.Close()
14
15
If
result >= 1
Then
16
imgUser.ImageUrl =
"unavailable.png"
17
imgUser.Visible =
True
18
lblUser.Text =
"Username not available"
19
lblUser.ForeColor = System.Drawing.Color.Red
20
Else
21
imgUser.ImageUrl =
"tick.png"
22
imgUser.Visible =
True
23
lblUser.Text =
"Available"
24
lblUser.ForeColor = System.Drawing.Color.Green
25
End
If
26
End
If
27
End
Sub
Similarly we can check email availability by writing following code in TextChanged Event of email textbox
01
protected
void
txtEmail_TextChanged(
object
sender, EventArgs e)
02
{
03
if
(txtEmail.Text !=
string
.Empty)
04
{
05
string
strConnection = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
06
string
strSelect =
"SELECT COUNT(*) FROM Users WHERE Email = @Email"
;
07
SqlConnection con =
new
SqlConnection(strConnection);
08
SqlCommand cmd =
new
SqlCommand(strSelect, con);
09
cmd.Parameters.AddWithValue(
"@Email"
, txtEmail.Text.Trim().ToString());
10
11
con.Open();
12
int
result = (Int32)cmd.ExecuteScalar();
13
con.Close();
14
15
if
(result >= 1)
16
{
17
imgEmail.ImageUrl =
"unavailable.png"
;
18
imgEmail.Visible =
true
;
19
lblEmail.Text =
"Email already registered"
;
20
lblEmail.ForeColor = System.Drawing.Color.Red;
21
}
22
else
23
{
24
imgEmail.ImageUrl =
"tick.png"
;
25
imgEmail.Visible =
true
;
26
lblEmail.Text =
"Available"
;
27
lblEmail.ForeColor = System.Drawing.Color.Green;
28
}
29
}
30
}
0 comments:
Post a Comment