April 20, 2016 00:10 by
Peter
In this tutorial, I will show you how to Check Record Availability using JOSN and jQuery. I will check user name available or not a table without refreshing the page using JOSN in ASP.NET. The followind code will 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: '{userName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
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)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[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;
}
}
I Hope it works for you! Happy coding.
HostForLIFE.eu ASP.NET Core 1.0 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.