Introduction
This code snippet explains how to check the user name that end user is entering already exist in database or not if it exist then message will show. In this entire process page is not refreshing.
I am using JOSN and ajax as in the following example:
- <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
- <script type = "text/javascript">
- function ShowAvailability() {
- $.ajax({
- type: "POST",
- url: "CheckUserAvalibleusingJOSN.aspx/CheckUserName",
- data:
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: OnSuccess,
- failure: function (response) {
- alert(response);
- }
- });
- }
-
- function OnSuccess(response) {
- var mesg = $("#mesg")[0];
- switch (response.d) {
- case "true":
- mesg.style.color = "green";
- mesg.innerHTML = "Available";
- break;
- case "false":
- mesg.style.color = "red";
- mesg.innerHTML = "Not Available";
- break;
- case "error":
- mesg.style.color = "red";
- mesg.innerHTML = "Error occured";
- break;
- }
- }
-
- function OnChange(txt) {
- $("#mesg")[0].innerHTML = "";
- ShowAvailability();//hide this function from here if we want to check avability by using button click
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div >
- UserName : <asp:TextBox ID="txtUserName" runat="server" onkeyup = "OnChange(this)"></asp:TextBox>
- <%--<input id="btnCheck" type="button" value="Show Availability" onclick = "ShowAvailability()" />--%>
- <br />
- <span id = "mesg"></span>
- </div>
- </form>
- </body>
Code
- [System.Web.Services.WebMethod]
- public static string CheckUserName(string userName)
- {
- string returnValue = string.Empty;
- try
- {
- string consString = ConfigurationManager.ConnectionStrings["manish_dbCS"].ConnectionString;
- SqlConnection conn = new SqlConnection(consString);
- SqlCommand cmd = new SqlCommand("spx_CheckUserAvailability", conn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@UserName", userName.Trim());
- conn.Open();
- returnValue = cmd.ExecuteScalar().ToString();
- conn.Close();
- }
- catch
- {
- returnValue = "error";
- }
- return returnValue;
- }
SQL Query
- USE [manish_db]
- GO
- /****** Object: StoredProcedure [dbo].[spx_CheckUserAvailability] Script Date: 07/19/2014 02:17:13 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER PROCEDURE [dbo].[spx_CheckUserAvailability]
- @UserName VARCHAR(50)
- AS
- BEGIN
- SET NOCOUNT ON;
- IF NOT EXISTS
- (SELECT UserName FROM dbo.UserDetails
- WHERE UserName = @UserName
- )
- SELECT 'true'
- ELSE
- SELECT 'false'
- END
Note: If we use web services just add webservices and replace the page path with *.asmx file path.
Code
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
-
- [System.Web.Script.Services.ScriptService]
- public class CheckUserAvalible : System.Web.Services.WebService
- {
- [WebMethod]
- public string CheckUserName(string userName)
- {
- string returnValue = string.Empty;
- try
- {
- string consString = ConfigurationManager.ConnectionStrings["manish_dbCS"].ConnectionString;
- SqlConnection conn = new SqlConnection(consString);
- SqlCommand cmd = new SqlCommand("spx_CheckUserAvailability", conn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@UserName", userName.Trim());
- conn.Open();
- returnValue = cmd.ExecuteScalar().ToString();
- conn.Close();
- }
- catch
- {
- returnValue = "error";
- }
- return returnValue;
- }
- }
Summary
In this illustration we came to learn about a user name checking without page refresh by using JOSN and AJEX . Please put your valuable comments about this code snippet.