I have successfully checked that there are values in the database.
Here is the code:
if (!reader.Read())
{
MessageBox.Show(" Can not find user!");
reader.Close();
}
else
{
int count = 0;
while (reader.Read())
{
string user = (string)reader.GetString(0);
string name = (string)reader.GetString(1);
int roll = (int)reader.GetInt32(2);
string phone = (string)reader.GetString(3);
string address = (string)reader.GetString(4);
string birthofdate = (string)reader.GetString(5);
label1.Text = "" + roll;
label2.Text = name;
label3.Text = birthofdate;
label4.Text = "" + phone;
label5.Text = address;
}
}
But the problem is that the while loop is skipped anyway.
Replace
if (!reader.Read())
with
if (!reader.HasRows)
I would just have the loop so you call Read in one place, and check count afterwards:
int count = 0;
while (reader.Read())
{
...
}
if (count == 0)
{
MessageBox.Show(" Can not find user!");
}
I've removed the reader.Close() call - so long as your reader is in a using statement (which it should be) it'll be closed automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With