bool result = va3.Equals(var1);
bool result2 = va4.Equals(var2);
if (result && result2)
{
Response.Redirect("ok.aspx");
}
else
Response.Write("wrongpass");
Try to understand what you have done here
You are using
while (read.Read())
it will check your condition with each of records.
But on First Case it will Redirect a page with your if/else condition out of List of Records.
That's why your condition is going to fail....
Rather wise Kartik Solution is good to check your condition on query time.
Else If We will modify your code it will may be like this
bool IsMatch=false;
while (read.Read())
{
name1 = read["name"].ToString();
password1 = read["password"].ToString();
string var1 = Convert.ToString(name1);
string var2 = Convert.ToString(password1);
string va3 = Convert.ToString(username.Text);
string va4 = Convert.ToString(passwordtx.Text);
bool result = va3.Equals(var1);
bool result2 = va4.Equals(var2);
if (result && result2)
{IsMatch=true;break;}
}
read.Close();
conn.Close();
if (IsMatch)
{
Response.Redirect("ok.aspx");
}
else
Response.Write("wrongpass");