473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Generating combo box items from a SQL database

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
2 6969
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gary | last post by:
I am working on this database that has a table ( tbl_main ) with the following 3 fields (amongst many fields in the table): Product, PanelType and Thickness Product (text field - combo box...
2
by: Dave McKie | last post by:
Hi all I am trying to Populate a combo box from a selection in an adjacent Listview. The Listview lists as a string the field names of 4 different fields in tblFile. So far I can populate the...
2
by: Jeremy Dillinger | last post by:
I have a program setup to pull data from a database. My database table has things such as (category, Item, price, etc.) In my program I want to have multiple list boxes that will have a pull down...
3
by: TS | last post by:
Hi all, I posted this question here before, but I'm still having a problem with this code. From my windows form, I opened a connection to a SQL database. I need to generate a combo box in my form...
2
by: 5070707 | last post by:
Hi all this question might be more SQL oriented or at least database structure... i created a table in my SQL which contains 2 columns: 1. state 2. country the way i input the data is: State...
2
by: jafarsibtain | last post by:
hi.. I am creating a form where there are two combo Boxes (drop Down) > Program_Combo_box > Course_Code_Combo in the Program Combo Box there are five items in the list > 1. BIT 2.BCA 3....
3
klarae99
by: klarae99 | last post by:
Hello, I am using Access 2003 to design an inventory database. Please be pacient with this question, I am not sure how exactly to explain what is happening concisely but I will do my best. I...
1
by: Josephbupe | last post by:
Sorry, i didn't know exactly what to name my question. What i need is to create a database for storing office items that will include Desktop computers, Laptops, printers, phones, etc. as...
18
by: Richard Penfold | last post by:
I have a form with a subform in my database that I use to manage deliveries. A combo box on the subform displays items of inventory and thier current status. The code behind the combo box is......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.