473,408 Members | 2,839 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,408 software developers and data experts.

Generating Combo Box items from an SQL Databse

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

Similar topics

5
by: will eichert | last post by:
Greetings. I have a problem with a combo box incorrectly displaying blank items when returning to a form from a modal form. It's fine when the main form first comes up, but gets messed up when the...
2
by: Nikolay Petrov | last post by:
I have asked a question before some time, and a guy responded to it. His answer raised new questions and I replied them back, but still no answer. I need them urgently so I am to post the whole...
2
by: http://www.visual-basic-data-mining.net/forum | last post by:
Hi... Say i have this string call "data" in Form1, this string contains number "5" value.... how do i pass this string to the Form2 and compare with the combo box items... The combo box ...
2
by: TS | last post by:
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...
1
by: Tgavin8 | last post by:
Hi I am currently attempting to build a databse for a clothing company. The price can differ for this company depending on the size so in the products table I have the sizes listed in a subform...
6
by: =?Utf-8?B?amVmZmVyeQ==?= | last post by:
i need help with a combo box and this same code works on my first tab with a combo box. The error or problem i have is this code causes an index out of range error when i run it on my second combo...
5
by: creative1 | last post by:
Hi, I am designing a data entry JSP form to add payment information. I want to add a combo box on the form that would have list of the compies present in databse. The combo should be populated based...
2
by: mygirl22 | last post by:
Hi, I used this code to created 2 combo boxes General and Specific...and Only show Specific (Combo B) when Combo A is chosen..... What i need now is to know how to assign specific values to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.