Attach database to 2 Combo-boxes and then store the data for future use

  • Thread starter Thread starter Rocky48
  • Start date Start date
R

Rocky48

Guest
I am trying to setup a form, which has 2 comboboxes on, each attached to different tables in the database.
Once the user has made a selection in each combobox, I want to then query another table to list all of the items that meet the criteria.
I have so far got this to work with 1 combobox, but am unsure how to add the additional combobox to the code. The button at the moment only displays the output from 1 combobox, but I will change this to save the values in memory and open a new window to display the results of the query.

Here is the code so far:

Imports System.Data.SqlClient
Public Class frmSecond
'Private MyDatAdp As New SqlDataAdapter
'Private MyDataTbl As New DataTable
'Private MyRowPosition As Integer = 0
Private Sub frmSecond_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connectionString As String = Nothing
connectionString = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Dim connection As SqlConnection
Dim command As SqlCommand
Dim sql As String = Nothing
Dim adaptor As New SqlDataAdapter()
Dim ds As New DataSet()
Dim i As Integer = 0
sql = "Select SID, EVENT from Event_Type"
connection = New SqlConnection(connectionString)
Try
connection.Open()
command = New SqlCommand(sql, connection)
adaptor.SelectCommand = command
adaptor.Fill(ds)
adaptor.Dispose()
command.Dispose()
connection.Close()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "SID"
ComboBox1.DisplayMember = "Event"
Catch ex As Exception
MessageBox.Show("Can't open connection")
End Try
connectionString = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Dim sql1 As String = Nothing
Dim ds1 As New DataSet()
sql1 = "Select BID, EVENT_Sub from Sub_Event"
connection = New SqlConnection(connectionString)
Try
connection.Open()
command = New SqlCommand(sql1, connection)
adaptor.SelectCommand = command
adaptor.Fill(ds1)
adaptor.Dispose()
command.Dispose()
connection.Close()
ComboBox2.DataSource = ds1.Tables(0)
ComboBox2.ValueMember = "BID"
ComboBox2.DisplayMember = "Event_Sub"
Catch ex As Exception
MessageBox.Show("Can't open 2nd connection")
End Try

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(ComboBox1.Text + " --" + ComboBox1.DisplayMember)

End Sub
End Class

I tried the above, but it will not connect to the 2nd connection. I tried it with just the one connect string but that did not work either. The exception said that it could not find the PDB file.

Please could someone give me some guidance?

Continue reading...
 
Back
Top