Tuesday, March 9, 2010
Username availability
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="UserName" runat="server" CssClass="txt" onkeyup="usernameChecker(this.value);" />
<span id="spanAvailability"></span>
<script type="text/javascript">
var usernameCheckerTimer;
var spanAvailability = $get("spanAvailability");
function usernameChecker(username)
{
clearTimeout(usernameCheckerTimer);
if (username.length == 0)
spanAvailability.innerHTML = "";
else
{
spanAvailability.innerHTML = "<span style='color: #ccc;'>checking...</span>";
usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 750);
}
}
function checkUsernameUsage(username)
{
// initiate the ajax pagemethod call
// upon completion, the OnSucceded callback will be executed
PageMethods.IsUserAvailable(username, OnSucceeded);
}
// Callback function invoked on successful completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName == "IsUserAvailable")
{
if (result == true)
spanAvailability.innerHTML = "<span style='color: DarkGreen;'>Available</span>";
else
spanAvailability.innerHTML = "<span style='color: Red;'>Unavailable</span>";
}
}
</script>
</div>
--------------------------------------------------
Sql
-----
CREATE PROCEDURE CheckUserName( @User_Name VARCHAR(50) )
AS
BEGIN
IF EXISTS (SELECT * FROM TLOGIN WHERE USERNAME = @User_Name )
SELECT '1'; --user name already exist in database
ELSE
SELECT '0'; --user name does not exist in database
END
.aspx.cs
----------
using System.Web.Services;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static bool IsUserAvailable(string username)
{
string returnValue = string.Empty;
string conString = "Password=iclip;Persist Security Info=True;User ID=iclip;Initial Catalog=ebiz;Data Source=purush";
SqlConnection sqlConn = new SqlConnection(conString);
try
{
SqlCommand sqlCmd = new SqlCommand("CheckUserName", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@User_Name", username.Trim());
sqlConn.Open();
int success = int.Parse((sqlCmd.ExecuteScalar().ToString()));
if (success == 1) // User Name Not Available
{
//returnValue = "<img src='Images/no.jpeg'><font color='#cc0000'><b>'" + username + "'</b> is already in use.</font></img>";
return false;
}
else//User_Name is available
{
return true;
//returnValue = "Available";
}
}
catch
{
return false;
}
finally
{
sqlConn.Close();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment