Question

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")
Was it helpful?

Solution

Try this:

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

OTHER TIPS

How about using a bool?

Something like

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top