How to check user availability from database in asp.net c# Using Ajax
In this article,
I have design a code to check the user availability from database in asp.net c#. Many times we
want to that we show to user that which value in available in database.
Database (Sql
Server)
Script:-
CREATE DATABASE [CodeSolution]
USE [CodeSolution]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[login](
[user_id] [int] IDENTITY(1,1) NOT NULL,
[username] [nvarchar](max) NULL,
[pwd] [nvarchar](50) NULL,
[name] [nvarchar](max) NULL,
CONSTRAINT
[PK_login] PRIMARY KEY
CLUSTERED
(
[user_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Design:-
Source Code:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="usercheckavailabilty.aspx.cs"
Inherits="usercheckavailabilty" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Check
Username availability</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>
User Name:
</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>
<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(c#):-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
public partial class usercheckavailabilty : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void txtUsername_TextChanged(object
sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtUsername.Text))
{
SqlConnection
con = new SqlConnection("Data Source=NEERAJ-PC;Initial
Catalog=CodeSolution;Persist Security Info=True;User ID=sa;
password=12345678");
con.Open();
SqlCommand
cmd = new SqlCommand("select * from login where username=@username",
con);
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
SqlDataReader
dr = cmd.ExecuteReader();
if
(dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "cancle.jpg";
lblStatus.Text = "User Name Already Taken";
System.Threading.Thread.Sleep(500);
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "available.jpg";
lblStatus.Text = "User Name Available";
System.Threading.Thread.Sleep(500);
}
}
else
{
checkusername.Visible = false;
}
}
}
How to check user availability from database in asp.net c# Using Ajax
Reviewed by NEERAJ SRIVASTAVA
on
12:25:00 PM
Rating:
No comments: