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

Generating Combo Box items from an SQL Databse

P: n/a
TS
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()
--
TS
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"TS" <TS@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
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()
--
TS

Nov 21 '05 #2

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


"Bernie Yaeger" wrote:
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
"TS" <TS@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
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()
--
TS


Nov 21 '05 #3

P: n/a
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("Firs t_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

"TS" <TS@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
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.


"Bernie Yaeger" wrote:
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
"TS" <TS@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
> 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()
>
>
> --
> TS


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.