Hello,
I had used the code below in website settings and worked fine. However, since I changed it to a web application the web methods totally stopped working. In the code below I am attempting to check I the username has been used but no luck with the web method.
If the username is taken, I am using a span “Username_Check” to display “Username already taken.". For example is there a unique url routing I have to use in global.asax? In a google search some people suggest to use PageMethods.set_path but I never used this before.
Could you tell me what the problem is with this code, please? Thank you for your help in advance.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.CheckUsername
@Username VARCHAR(255)
AS
BEGIN
SET NOCOUNT ON;
IF NOT EXISTS
(SELECT* FROM T_Profile
WHERE Email = @Username
)
SELECT 'true'
ELSE
SELECT 'false'
END
GO
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<scripttype = "text/javascript" >
window.onload = function Test() {
PageMethods.CheckUserName(document.getElementById("<%=txtUsername.ClientID%>").value, Username_OnSuccess);
}
function UsernameAvailability()
{
PageMethods.CheckUserName(document.getElementById("<%=txtUsername.ClientID%>").value, Username_OnSuccess);
}
function Username_OnSuccess(response)
{
var mesg = document.getElementById("Username_Check");
if (document.getElementById("<%=txtUsername.ClientID%>").value.length > 0)
{
switch (response)
{
case "true":
mesg.style.color = "green";
mesg.innerHTML = "Available";
break;
case "false":
mesg.style.color = "red";
mesg.innerHTML = "Username already taken.";
break;
}
}
else
{
mesg.innerHTML = "";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUsername" onChange="UsernameAvailability()" onkeyup="UsernameAvailability()"
ondrop="return false;" onpaste="return false;" runat="server" Text="maria" />
<span id="Username_Check"></span>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
[System.Web.Services.WebMethod()]
publicstaticstringCheckUserName(stringuserName)
{
stringreturnValue = string.Empty;
try
{
stringconsString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection conn = newSqlConnection(consString);
SqlCommand cmd = newSqlCommand("CheckUsername", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", userName.Trim());
conn.Open();
returnValue = cmd.ExecuteScalar().ToString();
conn.Close();
}
catch (Exception ex)
{
returnValue = "error";
}
returnreturnValue;
}