Check User Name Availability-2
-------------------------------------------
<span id="spanAvailability" style="text-align: right;"></span>
---------------------------------------
// to use pagemethods ...scriptmanager--> EnablePageMethods="true"
[WebMethod()]
public static bool IsUserAvailable(string Usernametxt)
{
bool userExists = false;
SqlConnection connUsernameCheck = new SqlConnection();
connUsernameCheck.ConnectionString = ConfigurationManager.ConnectionStrings["conSMM_Practice"].ConnectionString; // utility.ConnectionString;
SqlCommand commandUsernameCheck = new SqlCommand("Select UserName From tbl_User WHERE [UserName] = @Username", connUsernameCheck);
commandUsernameCheck.CommandType = CommandType.Text;
commandUsernameCheck.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Usernametxt;
connUsernameCheck.Open();
SqlDataReader MyReader = commandUsernameCheck.ExecuteReader();
if (MyReader.HasRows)
{
userExists = false;
}
else
{
userExists = true;
}
MyReader.Close();
connUsernameCheck.Close();
return Convert.ToBoolean(userExists);
}
----------------------------------
-------------------------------------------
<script type="text/javascript">---------------
var usernameCheckerTimer;
function usernameChecker(username) {
var spanAvailability = document.getElementById("spanAvailability");
clearTimeout(usernameCheckerTimer);
if (username.length < 1)
spanAvailability.innerHTML = "";
else {
spanAvailability.innerHTML = "<span style='color: #ccc;'>checking...</span>";
usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 750);
}
}
function checkUsernameUsage(username) {
PageMethods.IsUserAvailable(username, OnSucceeded);
}
function OnSucceeded(result, userContext, methodName) {
var span = document.getElementById("spanAvailability");
if (methodName == "IsUserAvailable") {
if (result == true)
span.innerHTML = "<span style='color: DarkGreen;'>Available</span>";
else
span.innerHTML = "<span style='color: Red;'>Unavailable</span>";
}
}
</script>
<asp:TextBox ID="txtUserName" MaxLength="50" Width="150px"
onkeyup="usernameChecker(this.value);" runat="server"></asp:TextBox>
---------------------------------------
// to use pagemethods ...scriptmanager--> EnablePageMethods="true"
[WebMethod()]
public static bool IsUserAvailable(string Usernametxt)
{
bool userExists = false;
SqlConnection connUsernameCheck = new SqlConnection();
connUsernameCheck.ConnectionString = ConfigurationManager.ConnectionStrings["conSMM_Practice"].ConnectionString; // utility.ConnectionString;
SqlCommand commandUsernameCheck = new SqlCommand("Select UserName From tbl_User WHERE [UserName] = @Username", connUsernameCheck);
commandUsernameCheck.CommandType = CommandType.Text;
commandUsernameCheck.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Usernametxt;
connUsernameCheck.Open();
SqlDataReader MyReader = commandUsernameCheck.ExecuteReader();
if (MyReader.HasRows)
{
userExists = false;
}
else
{
userExists = true;
}
MyReader.Close();
connUsernameCheck.Close();
return Convert.ToBoolean(userExists);
}
----------------------------------
0 comments:
Post a Comment