Search code examples
c#if-statementwhile-loopdatareader

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")

Solution

  • Try this:

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