Looping through multiple records in SQL database

  • Thread starter Thread starter Amr_Aly
  • Start date Start date
A

Amr_Aly

Guest
Hi everyone,

I have this code to check if one record is true or false in SQL database ... it is working fine as expected

Private Sub Availability()
Using conn As New SqlConnection(cs)
conn.Open()
Using cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "select F1
from UnitA
where F1 = 1"

Using rdr As SqlDataReader = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Checkbox1 is in use", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
CheckBox1.Enabled = False

If Not rdr Is Nothing Then
rdr.Close()
End If

Else
MessageBox.Show("You can use Checkbox1", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
CheckBox1.Enabled = True
End If
End Using
End Using
End Using
End Sub

But in fact i have 21 record to check them also (F1,F2,F3,.............To F21),,, the question is How can i check them by this code

I tried to do something like this to only 6 records and it works with me

cmd.CommandText = "select F1,F2,F3,F4,F5,F6
from UnitA
where F1=1 or F2=1 or F3=1 or F4=1 or F5=1 or F6=1"
But i think there is a simple way to deal with 21 (Bit records in my SQL server query) than this way...


I want a way to loop through them .... I tried to search but i didn't understand so i hope anyone to explain me how to do this ..

Thanks


Regards From Egypt

Continue reading...
 
Back
Top