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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Madhu KRank #1,514 · 1.1K reputationPosted Nov 16, 2010, 9:45 PM EST
if (TextBox1.Text == reader["UserName"].ToString())
{
Response.Write("Username exists");
break;
}
else
{
Label1.Text= "Username does not exists";
}
}
Madhu KRank #1,514 · 1.1K reputationPosted Nov 15, 2010, 6:40 PM EST
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();
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 16, 2010, 5:05 AM EST
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 KRank #1,514 · 1.1K reputationPosted Nov 16, 2010, 8:26 PM EST
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 NayakRank #47 · 31.5K reputationPosted Nov 16, 2010, 9:00 PM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 17, 2010, 3:26 AM EST