I have a problem with my C# code.
I need validating the user name, I used an if-condition inside the while loop but the thing is that even when the username and password are incorrect, it not executes any statement.
I have tested the query of authentication in database and the output is correct.
I've tried using these three different solutions without success.
Here is my code :
Solution #1
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
int count = reader.GetInt32(0);
if (count > 0)
{
Response.Write("Welcome!");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('no data.');", true);
}
}
}
Solution #2
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (reader.HasRows)
{
Response.Write("Welcome!");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('no data.');", true);
}
}
}
Solution #3
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (!String.IsNullOrEmpty(reader[0].ToString()))
{
Response.Write("Welcome!");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('no data.');", true);
}
}
}
None of your solutions are valid. You seem to think that your while
loop is always executed, however this is not the case. When your sql query returns 0 rows you never enter the while(reader.Read())
loop.
A simple approach, based on your second solution, could be something like the following:
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
Response.Write("Welcome!");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('no data.');", true);
}
}
Note how there is no while
loop involved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.