Wednesday, August 5, 2009

check userid already exists or not when register.

hi, we have seen most of the website showing a message "user all ready there please use

something else" like such kind of message,
now how to trace that? there are lot many ways to show such kind of message to user without

redirecting to another page. In this case you can use Either JavaScript / JQuery / Ajax

Here i am using JavaScript to do so. below example will clear you.
on Html panal put this code

<asp:TextBox ID="CusID" MaxLength="15" TabIndex="1" runat="Server"></asp:TextBox>
<asp:Button ID="Submit" TabIndex="25" Text="Submit" runat="Server" OnClick="Submit_Click" />


and on Codebehind paste this


SqlDataReader dr;
SqlCommand cmd = new SqlCommand();
String Msg;

protected void Submit_Click(object sender, EventArgs e)
{
cmd.CommandText = "select id from loginstat where cusid=@cusid";
cmd.Parameters.Add(new SqlParameter("@cusid",CusID.Text));
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
Msg = "<script language=javascript>alert('User ID allready Exists '); </script>";
ClientScript.RegisterStartupScript(this.GetType(), "Msg", Msg);
dr.Close();
}
else
{
dr.Close();
string StrSql = "insert into loginstat(id) values(@id)";
cmd.Parameters.Add(new SqlParameter("@id", CusID.Text));
cmd.CommandText = StrSql;
cmd.ExecuteNonQuery();
dr.Close();
}
dr.Close();
}


Like this way also you can check and cusid not found on database then it will insert that recrod into database.

1 comment: