By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,810 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,810 IT Pros & Developers. It's quick & easy.

Generating combo box items from a SQL database

P: n/a
TS
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=USEGNYD C3\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?

--
TS
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

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

"TS" <TS@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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=USEGNYD C3\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?

--
TS
Nov 21 '05 #2

P: n/a
TS
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=s ervername"
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()

"Ken Tucker [MVP]" wrote:
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

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

"TS" <TS@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
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=USEGNYD C3\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?

--
TS

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.