How do i show my data from a table in database with specific position to a combobox? example is this Example
Only with the President Position candidates will appear on combobox1 from table1 Only with the VPresident Position candidates will appear on combobox2 from table1 and so on...
im stuck with this on combobox1 which is called prescombo code
Private Sub prescombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles prescombo.SelectedIndexChanged
Try
Dim query As String = "SELECT * FROM Candidates WHERE Position = President"
Using con As SqlConnection = New SqlConnection("Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
Dim datapter As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim datable As DataTable = New DataTable()
datapter.Fill(datable)
prescombo.DisplayMember = "CandidateName"
prescombo.ValueMember = "CandidateID"
If datable.Rows.Count > 0 Then
If datable.Rows(3)("Position") = "President" Then
presparty.Text = datable.Rows(3).Item("Position").ToString
End If
End If
End Using
End Using
Catch ex As Exception
End Try
End Sub
This is what i tried based on my logic and its not working
CodePudding user response:
If you're filtering the data using a WHERE
clause in the query then there's no need to check the records in the result set. Just populate the DataTable
and bind it.
Dim query = "SELECT * FROM Candidates WHERE Position = 'President'"
Dim connectionString = "Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True"
Using adapter As New SqlDataAdapter(query, connectionString)
Dim table As New DataTable
adapter.Fill(table)
With prescombo
.DisplayMember = "CandidateName"
.ValueMember = "CandidateID"
.DataSource = table
Endf With
End Using