.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Check UserName Availability from database Using JQuery in asp.net

Posted By:Pankaj Mishra       Posted Date: December 20, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Check UserName Availability from database Using JQuery in asp.net

In this article we are going to validate the  asp.net textbox control value (User name) with the database entry, if user name already exist in the database it will display the message like twitter style using JQuery and page method in asp.net. Sample Screen shown below

In this article we will see how to check the user name availability on Text change using JQuery in asp.net.

All you need add the reference the file in .aspx page like this


Lets first create a asp.net web page having one textbox control and a HTML span tag to display message

 

 User Name :
  
   



Now we are also going to decorate the message displayed to the user on available or not available using css

So here is the css


 
Now we are going to write the JQuery Function to validate the User Name this JQuery function will also validate user name input should be more than 5 characters





Now Lets create a SQL table and name it to User_Table and to create it you can use Database wizard or you can use this SQL Script

CREATE TABLE [dbo].[User_Table](
  [User_Name] [varchar](30) NOT NULL,
  [First_Name] [varchar](50) NOT NULL,
  [Last_Name] [varchar](50) NOT NULL,
  [Middle_Name] [varchar](50) ,
  [Email_Id] [varchar](70) NOT NULL,
 CONSTRAINT [PK_User_Table] PRIMARY KEY CLUSTERED 
(
  [User_Name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]




Now we have to create one stored procedure to validate the user Name exist in the database or not

CREATE PROCEDURE CheckUserName(  @User_Name VARCHAR(50) )
AS 
BEGIN 

  IF EXISTS (SELECT * FROM User_Table WHERE  [User_Name] = @User_Name ) 

  SELECT '1'; --user name already exist in database

  ELSE 

  SELECT '0'; --user name does not exist in database

END


Now in your .cs file you need to create a PageMethod which  will call the stored procedure and depend upon the user name availability it will return the string message to the JQuery function for displaying message to the user
So there is the Page method
If you see this method is Webservice method which will return the result and this function you can write in your Register.cs page.

You have to add this [System.Web.Services.WebMethod] attribute to the function which will check the username from the database.

In C# Example

 

[System.Web.Services.WebMethod]
  public static string CheckUserName(string args)
  {

  string returnValue = string.Empty;

  string conString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

  SqlConnection sqlConn = new SqlConnection(conString);

  try
  {

  SqlCommand sqlCmd = new SqlCommand("CheckUserName", sqlConn);

  sqlCmd.CommandType = CommandType.StoredProcedure;

  sqlCmd.Parameters.AddWithValue("@User_Name", args.Trim());

  sqlConn.Open();

   int success = int.Parse((sqlCmd.ExecuteScalar().ToString()));

  if(success == 1) // User Name Not Available
  {
  returnValue = "'" + args + "' is already in use.";
  
  }
  else if(success == 0)//User_Name is available
  {
  returnValue = "Available";
  }

  }
  catch
  {
  //Handle Error
  }
  finally
  {
  sqlConn.Close();
  }
  
  return returnValue;
  }


And in VB.NET Sample

 _
Public Shared Function CheckUserName(ByVal args As String) As String

  Dim returnValue As String = String.Empty

  Dim conString As String = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString

  Dim sqlConn As New SqlConnection(conString)

  Try

  Dim sqlCmd As New SqlCommand("CheckUserName", sqlConn)

  sqlCmd.CommandType = CommandType.StoredProcedure

  sqlCmd.Parameters.AddWithValue("@User_Name", args.Trim())

  sqlConn.Open()

  Dim success As Integer = Integer.Parse((sqlCmd.ExecuteScalar().ToString()))

  If success = 1 Then
  ' User Name Not Available

  returnValue = " '" & args & "' is already in use."
  ElseIf success = 0 Then
  'User_Name is available
  returnValue = "Available"

  End If
  Catch
  'Handle Error
  Finally
  sqlConn.Close()
  End Try

  Return returnValue
End Function



And Now you are done..
Once you run the application you will see an example like this

If the username is available you will see this screen



If user Name is not available you will see this screen



If user has entered less than 5 character you will this see this screen


You can use the above JQuery function to validate any input with Database entry like Email and other stuff. Also if you want to validate on Button click than just change the control id and its event on what you want to validate.

Cheers!!!
Pankaj


 Subscribe to Articles

     

Further Readings:

Responses
Author: Asiri         Company URL: http://www.dotnetspark.com
Posted Date: May 27, 2010

I hope to upload all the cods...please
Author: Asiri         Company URL: http://www.dotnetspark.com
Posted Date: May 27, 2010

Hello, and good day
I found this lesson http://www.dotnetspark.com/kb/1278-check-username-availability-from-database.aspx by Pankaj, and I have downloaded this file but it was incomplete, and dosen't work will so please provide me with all code with database, because the problem in
1- The Database name not mentioned
2- The ConnectionString in web.config also incomplete

<connectionStrings>
<add name="MyConnection" connectionString="Data Source=PANKAJ-PC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=SSPI;" />
</connectionStrings>

so I hope to get complete answer
Author: Pankaj Mishra         Company URL: http://www.dotnetspark.com
Posted Date: May 27, 2010

Hi Asiri,

Check out the answer I gave in this thread

http://www.dotnetspark.com/Forum/1143-i-hope-to-get-all-code-with-database-for.aspx
Author: Balwant         Company URL: http://www.dotnetspark.com
Posted Date: February 09, 2011

how can i access the web method if this is written in another file e.g. data access layer. and jquery code is written in registraion.aspx file.

Author: Pankaj Mishra         Company URL: http://www.dotnetspark.com
Posted Date: February 10, 2011

Hi,

You can add the static class similar to above example. you can add the namespace of the reference class where web method is there to the aspx.cs file.

Thanks
Pankaj
Author: Green Hawk         Company URL: http://www.dotnetspark.com
Posted Date: November 17, 2011

Hi Pankaj,
thank you for your code but it is nt working

Can you please share your full code because it is missing not calling the CheckUserName webservice

i changed only the web.config
please update the download link or send your coe to my email green_hawk_lb@hotmail.com

Is ot possible you add also a button create and let us know if it create button will validate if user unavailable without returning an error code on post back

thanks alot,
Hawk
Author: Pankaj Mishra         Company URL: http://www.dotnetspark.com
Posted Date: November 17, 2011

Hi,

Code is working..you might missed the database part..Please add the Tables in your DataBase.

There is no web service for this project

CheckUserName method uses [System.Web.Services.WebMethod] attribute it is as good as web services for this application

Thanks
Pankaj
Author: SonalGawde         Company URL: http://www.dotnetspark.com
Posted Date: March 29, 2012

Hi,

This code is working fine for page which doesn't not have master page reference. But when i am trying to use same for page which have master page reference it is not working !! Plz guide me what to do ??



Author: stylojack_10         Company URL: http://www.dotnetspark.com
Posted Date: March 22, 2013

Do I need to have image controls on my aspx to display a.png & n.png ???

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend