Hi all,
how to check username exist in database.
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Madhu KPosted Nov 17, 2010, 2:45 AM
if (TextBox1.Text == reader["UserName"].ToString())
{
Response.Write("Username exists");
break;
}
else
{
Label1.Text= "Username does not exists";
}
}
Satyapriya NayakPosted Nov 17, 2010, 8:26 AM
Satyapriya NayakPosted Nov 17, 2010, 2:00 AM
Madhu KPosted Nov 17, 2010, 1:26 AM
The above statement enters while loop when it is true ie when there are records. So when there are no records while loop is not executed and hence you dont get any message in the loop.
Change the statement to str = "select UserName from Login"; and use break in the loop so that once username exists it comes out of loop instead of checking all the records.
if (TextBox1.Text == reader["UserName"].ToString())
{
Response.Write("Username exists");
break;
}
else
{
Response.Write("Username doesnotexists");
}
Satyapriya NayakPosted Nov 16, 2010, 10:05 AM
But in the below code if user is present it shows the Username exists ,but when user is not there it doesnot shows any message,whats wrong in the program.
con = new SqlConnection(connStr);
con.Open();
str = "select UserName from Login where UserName='"+TextBox1.Text +"' ";
cmd = new SqlCommand(str, con);
SqlDataReader reader=cmd.ExecuteReader();
while (reader.Read())
{
if (TextBox1.Text == reader["UserName"].ToString())
{
Response.Write("Username exists");
}
else
{
Response.Write("Username doesnotexists");
}
}
con.Close();
reader.Close();
Madhu KPosted Nov 15, 2010, 11:40 PM
SqlCommand SqlCmd1 = new SqlCommand();
SqlCmd1.CommandType = CommandType.Text;
SqlCmd1.Connection = SqlCnn;
SqlCmd1.CommandText = "select username from users where ";
SqlCnn.Open();
SqlDataReader Sqldr = SqlCmd1.ExecuteReader();
while (Sqldr.Read())
{
//username is value to check..
if(username==Sqldr["username"].ToString())
{
Response.Write("Username exists");
break;
}
}
Sqldr.Close();
SqlCnn.Close();