Karlx Swanovski
Karlx Swanovski

Reputation: 2989

While and IF in data reader

How I will combine the while and if in data reader? I tried this but in while DR1.Read it does not gives me all the result

if(DR1.Read())
{
while(DR1.Read())
{
    flowLayoutPanel1.Controls.Add(label);
}
}
else
    MessageBox.Show("No results found")

Upvotes: 4

Views: 2456

Answers (3)

Andrey Morozov
Andrey Morozov

Reputation: 7979

Try this:

   if (DR1.HasRows)
   {
      while (DR1.Read())
      {
         flowLayoutPanel1.Controls.Add(label);
      }
   }
   else
        MessageBox.Show("No results found");

Upvotes: 3

xanatos
xanatos

Reputation: 111920

Technically:

if(DR1.Read())
{
    do
    {
        flowLayoutPanel1.Controls.Add(label);
    }
    while(DR1.Read())
}
else
    MessageBox.Show("No results found")

you can put the while at the end, because the if(DR1.Read()) already loads the first row if present.

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166486

How about using a bool?

Something like

bool read = false;
while (DR1.Read())
{
    read = true;
}
if (!read)
    MessageBox.Show("No results found");

Upvotes: 1

Related Questions