Generating combo box items from a SQL database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
From my windows form, I opened a connection to a SQL database. Now I need to
generate a combo box from a SELECT statement pointing to the last name column
in the SQL tables. I am stuck with the code I should use to generate this
combo box. I used to use ADO to open a connection to SQL in VB6 then use Do
until EOF - Loop to loop through the recodset and the cbo.additems to place
the result of the recordset in the combo box. With .Net I am lost.The code I
used to open a connection to the SQL database in the .net form is the
following:-

Private Sub ConnectSQL_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ConnectSQL.Click
Dim SQLEV As SqlClient.SqlConnection = New SqlClient.SqlConnection
SQLEV.ConnectionString = "Persist Security Info=true;Integrated
Security=false;User
ID=sa;Password=demento;database=SSC;server=USEGNYDC3\CLIENT_DB"
SQLEV.Open()
Dim SQLLine As String
SQLLine = "SELECT last_name FROM qmf_cint"
Dim objcmd As New SqlClient.SqlCommand(SQLLine, SQLEV)

What should I do next?
 
Hi,

Here are 2 methods. I prefer to bind the combobox to a dataset.

Dim conn As SqlConnection

Dim strConn As String

Dim drCustomer As SqlDataReader

Dim daCustomer As SqlDataAdapter

Dim cmd As SqlCommand

Dim ds As New DataSet

strConn = "Server = " & Environment.MachineName & ";"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

cmd = New SqlCommand("Select * from Customers", conn)

conn.Open()

drCustomer = cmd.ExecuteReader

ComboBox1.Items.Clear()

Do While drCustomer.Read

ComboBox1.Items.Add(drCustomer.Item("CustomerID").ToString)

Loop

conn.Close()

daCustomer = New SqlDataAdapter("Select * from Customers", conn)

daCustomer.Fill(ds, "Customers")

ComboBox2.DataSource = ds.Tables("Customers")

ComboBox2.DisplayMember = "CustomerID"



Ken

----------------------------

Hi all,
From my windows form, I opened a connection to a SQL database. Now I need to
generate a combo box from a SELECT statement pointing to the last name
column
in the SQL tables. I am stuck with the code I should use to generate this
combo box. I used to use ADO to open a connection to SQL in VB6 then use Do
until EOF - Loop to loop through the recodset and the cbo.additems to place
the result of the recordset in the combo box. With .Net I am lost.The code I
used to open a connection to the SQL database in the .net form is the
following:-

Private Sub ConnectSQL_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ConnectSQL.Click
Dim SQLEV As SqlClient.SqlConnection = New SqlClient.SqlConnection
SQLEV.ConnectionString = "Persist Security Info=true;Integrated
Security=false;User
ID=sa;Password=demento;database=SSC;server=USEGNYDC3\CLIENT_DB"
SQLEV.Open()
Dim SQLLine As String
SQLLine = "SELECT last_name FROM qmf_cint"
Dim objcmd As New SqlClient.SqlCommand(SQLLine, SQLEV)

What should I do next?
 
Thanks for your reply. I tried the code you sent me, it didn't return an
error message when I ran the form and clicked on the command button but it
didn't generate the contents of the combo box either. Below is the code.
Anything there prohibts the generation of the contents in the combo box?
Another thing; how can I include the first name as well in the combo box
beside the last name

Private Sub ConnectSQL_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ConnectSQL.Click
Dim SQLEV As SqlClient.SqlConnection = New SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim ClientName As SqlClient.SqlDataReader

SQLEV.ConnectionString = "Persist Security Info=true;Integrated
Security=false;User
ID=sa;Password=XXXX;database=databasename;server=servername"
cmd = New SqlClient.SqlCommand("SELECT * FROM qmf_cint", SQLEV)
SQLEV.Open()
ClientName = cmd.ExecuteReader
cbo_ChildName.Items.Clear()
Do While ClientName.Read
cbo_ChildName.Items.Add(ClientName.Item("Last_Name").ToString)
Loop
SQLEV.Close()
 
Back
Top