Generating Combo Box items from an SQL Databse

G

Guest

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 from a SELECT statement that selects the last
name column
from an SQL table. I wrote the following code to do that, but when I run
this code it doesn't do anything!! It doesn't even return an error message to
let me know what's going on!!
All I need is generating this combo box from this table's column in SQL. I
am new to .net. Please help

This is the code I used:
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=yyy;server=zzzz"
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()
 
B

Bernie Yaeger

Hi TS,

Two ideas:
1. Change the order from
cmd = New SqlClient.SqlCommand("SELECT * FROM qmf_cint", SQLEV)
SQLEV.Open()
to
SQLEV.Open()
cmd = New SqlClient.SqlCommand("SELECT * FROM qmf_cint", SQLEV)
2. Do it this way, which I find much easier:
Dim oconn As New SqlConnection(globalconnectionstring)

Dim dabranches As New SqlDataAdapter("select brname from branches order by
brname", oconn)

Dim dsbranches As New DataSet("dsbranches")

dabranches.Fill(dsbranches, "dsbranches")

oconn.ConnectionString = globalconnectionstring

' you don't really need the connectionstring for this, as a dataset opens
its own

oconn.Open()

Dim r As DataRow

For Each r In dsbranches.Tables("dsbranches").Rows

cbo.Items.Add(r("brname"))

Next

HTH,

Bernie Yaeger
 
G

Guest

I can't express how much I am thankful to you. It worked. One more thing, if
I want to include last name as well as first name in the combo box, what
change I have to make in the code?
Currently the code reads as:
cbo_ChildName.Items.Add(ClientName.Item("Last_Name").ToString)

Thanks again for all your help.
 
B

Bernie Yaeger

Hi TS,

You're very welcome.

Here's the change:
this
cbo_ChildName.Items.Add(ClientName.Item("Last_Name").ToString)

changes to this
cbo_ChildName.Items.Add(Trim(ClientName.Item("First_Name").ToString) & " " &
ClientName.Item("Last_Name").ToString)

Notice that I put first name first and trimmed it so that it is no longer
than the letters in the first name; then I added one space; then the last
name.

HTH,

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top