473,387 Members | 1,575 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,387 software developers and data experts.

Advanced Help for Combo box Needed

Hi! I'm designing an application that produces invoices. I have a combobox
that is populated with the Name of every client in the database. What I
would like to do is to have the combobox to display the name, and to have
the value set to the ID of the record...

Like in HTML where you have something like: <option value="ID_NUMBER(primary
key)">CLIENT_NAME</option>

so, let's say the user selects "Client #2" in the combo box, then I would be
able to call the address of this client using the ID... SELECT * FROM
tbl_clients WHERE id_client = bombobox.selectedValue ...

I don't know if I'm making myself clear, I will be around, so ask anything
if I'm not enough clear. Thank y'all, this really apprecioated!

Jean Christophe Avard!
Nov 21 '05 #1
6 1582
Jean Christophe Avard,

Retrieve the client name and ID from the database and store the data in a
datatable.

Assign the datatable to the combobox'x DataSource.

Assign the client name to the combobox's DisplayMember.

Assign the client ID to the combobox's ValueMember.

In the combobox'x SelectedIndexChanged event, retrieve the ID from the
combobox's SelectedValue property.

Kerry Moorman


"Jean Christophe Avard" wrote:
Hi! I'm designing an application that produces invoices. I have a combobox
that is populated with the Name of every client in the database. What I
would like to do is to have the combobox to display the name, and to have
the value set to the ID of the record...

Like in HTML where you have something like: <option value="ID_NUMBER(primary
key)">CLIENT_NAME</option>

so, let's say the user selects "Client #2" in the combo box, then I would be
able to call the address of this client using the ID... SELECT * FROM
tbl_clients WHERE id_client = bombobox.selectedValue ...

I don't know if I'm making myself clear, I will be around, so ask anything
if I'm not enough clear. Thank y'all, this really apprecioated!

Jean Christophe Avard!

Nov 21 '05 #2
Thank you man, but I have another q7uestion... Why do I get an error with
this:

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue

I also tried

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue.toString()

And it always give me an error

"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for string "SELECT * FROM
tbl_clients WHERE " and type 'DataRowView'."

What is the way to go!?

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:<8D**********************************@microso ft.com>...
Jean Christophe Avard, Retrieve the client name and ID from the database and store the data in a datatable. Assign the datatable to the combobox'x DataSource. Assign the client name to the combobox's DisplayMember. Assign the client ID to the combobox's ValueMember. In the combobox'x SelectedIndexChanged event, retrieve the ID from the combobox's SelectedValue property. Kerry Moorman "Jean Christophe Avard" wrote:

Hi! I'm designing an application that produces invoices. I have a
combobox that is populated with the Name of every client in the database. What I would like to do is to have the combobox to display the name, and to
have the value set to the ID of the record... Like in HTML where you have something like: <option
value="ID_NUMBER(primary key)">CLIENT_NAME</option> so, let's say the user selects "Client #2" in the combo box, then I
would be able to call the address of this client using the ID... SELECT * FROM tbl_clients WHERE id_client = bombobox.selectedValue ... I don't know if I'm making myself clear, I will be around, so ask
anything if I'm not enough clear. Thank y'all, this really apprecioated! Jean Christophe Avard

Nov 21 '05 #3
Jean Christophe Avard,

It looks like the combobox's ValueMember property has not been correctly set.

Be sure that the column name you supply to the ValueMember property is
spelled exactly as in the datatable, including case.

You might post your code where you retrieve the datatable and then use it as
the combobox's data source, etc.

Kerry Moorman
"Jean Christophe Avard" wrote:
Thank you man, but I have another q7uestion... Why do I get an error with
this:

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue

I also tried

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue.toString()

And it always give me an error

"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for string "SELECT * FROM
tbl_clients WHERE " and type 'DataRowView'."

What is the way to go!?

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:<8D**********************************@microso ft.com>...
Jean Christophe Avard,


Retrieve the client name and ID from the database and store the data in a

datatable.


Assign the datatable to the combobox'x DataSource.


Assign the client name to the combobox's DisplayMember.


Assign the client ID to the combobox's ValueMember.


In the combobox'x SelectedIndexChanged event, retrieve the ID from the

combobox's SelectedValue property.


Kerry Moorman





"Jean Christophe Avard" wrote:


Hi! I'm designing an application that produces invoices. I have a
combobox that is populated with the Name of every client in the database. What I would like to do is to have the combobox to display the name, and to
have the value set to the ID of the record... Like in HTML where you have something like: <option
value="ID_NUMBER(primary key)">CLIENT_NAME</option> so, let's say the user selects "Client #2" in the combo box, then I
would be able to call the address of this client using the ID... SELECT * FROM tbl_clients WHERE id_client = bombobox.selectedValue ... I don't know if I'm making myself clear, I will be around, so ask
anything if I'm not enough clear. Thank y'all, this really apprecioated! Jean Christophe Avard


Nov 21 '05 #4
Jean,

Try using an ampersand & instead of the +.

Rgds, Phil
"Jean Christophe Avard" <NO***@M.BITCH> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thank you man, but I have another q7uestion... Why do I get an error with
this:

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue

I also tried

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue.toString()

And it always give me an error

"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for string "SELECT * FROM
tbl_clients WHERE " and type 'DataRowView'."

What is the way to go!?

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:<8D**********************************@microso ft.com>...
Jean Christophe Avard,


Retrieve the client name and ID from the database and store the data in a

datatable.


Assign the datatable to the combobox'x DataSource.


Assign the client name to the combobox's DisplayMember.


Assign the client ID to the combobox's ValueMember.


In the combobox'x SelectedIndexChanged event, retrieve the ID from the

combobox's SelectedValue property.


Kerry Moorman





"Jean Christophe Avard" wrote:


> Hi! I'm designing an application that produces invoices. I have a
> combobox > that is populated with the Name of every client in the database. What I > would like to do is to have the combobox to display the name, and to
> have > the value set to the ID of the record... > > Like in HTML where you have something like: <option
> value="ID_NUMBER(primary > key)">CLIENT_NAME</option> > > so, let's say the user selects "Client #2" in the combo box, then I
> would be > able to call the address of this client using the ID... SELECT * FROM > tbl_clients WHERE id_client = bombobox.selectedValue ... > > I don't know if I'm making myself clear, I will be around, so ask
> anything > if I'm not enough clear. Thank y'all, this really apprecioated! > > Jean Christophe Avard


Nov 21 '05 #5
the cboxClient.SelectedValue.tostring returns System.DataRowView so I still
have an error.... I also used & instead of + and its still the same...
anyone!?
this is the code in the load event of the form, where I populate the
combobox

Private Sub frmQuotation_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' create a connection

Dim cn As SqlConnection = New SqlConnection

' open connection

cn.ConnectionString = strConn

' create sql query

Dim sql As String

If newQuotationType = "client" Then

sql = "SELECT id_client, first_name, last_name FROM tbl_clients ORDER BY
last_name, first_name"

ElseIf newQuotationType = "supplier" Then

sql = "SELECT id_supplier, name FROM tbl_suppliers ORDER BY name"

End If

' create a data adapter

Dim da As SqlDataAdapter = New SqlDataAdapter(sql, cn)

' create a dataset

Dim ds As DataSet = New DataSet

' add table to the data set

ds.Tables.Add(New DataTable("clients"))

' fill the data adapter

da.Fill(ds.Tables("clients"))

' create a datarow

Dim dr As DataRow

' populate combo box

cboxClient.DataSource = ds.Tables("clients")

If newQuotationType = "client" Then

cboxClient.DisplayMember = "last_name"

cboxClient.ValueMember = "id_client"

Else

cboxClient.DisplayMember = "name"

cboxClient.ValueMember = "id_supplier"

End If

cn.Dispose()

End Sub


This is the code I use in the selectedValueChanged event:

Private Sub cboxClient_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboxClient.SelectedIndexChanged

Dim conn As SqlConnection = New SqlConnection

' connection string

conn.ConnectionString = strConn

Dim sql As String

If newQuotationType = "client" Then

sql = "SELECT * FROM tbl_clients WHERE id_client= " &
cboxClient.SelectedValue

ElseIf newQuotationType = "supplier" Then

sql = "SELECT * FROM tbl_suppliers WHERE id_supplier " &
cboxClient.SelectedValue

End If

MessageBox.Show(sql)

' create a new data adapter

Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)

' create a new dataset

Dim ds As DataSet = New DataSet

ds.Tables.Add(New DataTable("address"))

da.Fill(ds.Tables("address"))

Dim address As String = ds.Tables("address").Rows(0)("address").ToString +
vbNewLine

Dim city As String = ds.Tables("address").Rows(0)("city").ToString + " (" +
ds.Tables("address").Rows(0)("state").ToString + ")" + vbNewLine

Dim country As String = ds.Tables("address").Rows(0)("country").ToString + "
" + ds.Tables("address").Rows(0)("zipcode").ToString + vbNewLine

Dim phone As String = ds.Tables("address").Rows(0)("phone").ToString +
vbNewLine

Dim fax As String = ds.Tables("address").Rows(0)("fax").ToString

rtxtAddress.Text = address + city + country + phone + fax



"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
Jean Christophe Avard,

It looks like the combobox's ValueMember property has not been correctly
set.

Be sure that the column name you supply to the ValueMember property is
spelled exactly as in the datatable, including case.

You might post your code where you retrieve the datatable and then use it
as
the combobox's data source, etc.

Kerry Moorman
"Jean Christophe Avard" wrote:
Thank you man, but I have another q7uestion... Why do I get an error with
this:

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue

I also tried

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue.toString()

And it always give me an error

"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for string "SELECT * FROM
tbl_clients WHERE " and type 'DataRowView'."

What is the way to go!?

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:<8D**********************************@microso ft.com>...
> Jean Christophe Avard,

>

> Retrieve the client name and ID from the database and store the data in
> a

> datatable.

>

> Assign the datatable to the combobox'x DataSource.

>

> Assign the client name to the combobox's DisplayMember.

>

> Assign the client ID to the combobox's ValueMember.

>

> In the combobox'x SelectedIndexChanged event, retrieve the ID from the

> combobox's SelectedValue property.

>

> Kerry Moorman

>

>

>

>

> "Jean Christophe Avard" wrote:

>

> > Hi! I'm designing an application that produces invoices. I have a
> > combobox

> > that is populated with the Name of every client in the database. What
> > I

> > would like to do is to have the combobox to display the name, and to
> > have

> > the value set to the ID of the record...

> >

> > Like in HTML where you have something like: <option
> > value="ID_NUMBER(primary

> > key)">CLIENT_NAME</option>

> >

> > so, let's say the user selects "Client #2" in the combo box, then I
> > would be

> > able to call the address of this client using the ID... SELECT * FROM

> > tbl_clients WHERE id_client = bombobox.selectedValue ...

> >

> > I don't know if I'm making myself clear, I will be around, so ask
> > anything

> > if I'm not enough clear. Thank y'all, this really apprecioated!

> >

> > Jean Christophe Avard


Nov 21 '05 #6
Jean Christophe Avard,

First assign a value to the combobox's Displaymember.

Next, assign a value to the combobox's Valuemember.

Then assign a value to the combobox's Datasource.

The order of these steps seems to matter.

Kerry Moorman
"Jean Christophe Avard" wrote:
the cboxClient.SelectedValue.tostring returns System.DataRowView so I still
have an error.... I also used & instead of + and its still the same...
anyone!?
this is the code in the load event of the form, where I populate the
combobox

Private Sub frmQuotation_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' create a connection

Dim cn As SqlConnection = New SqlConnection

' open connection

cn.ConnectionString = strConn

' create sql query

Dim sql As String

If newQuotationType = "client" Then

sql = "SELECT id_client, first_name, last_name FROM tbl_clients ORDER BY
last_name, first_name"

ElseIf newQuotationType = "supplier" Then

sql = "SELECT id_supplier, name FROM tbl_suppliers ORDER BY name"

End If

' create a data adapter

Dim da As SqlDataAdapter = New SqlDataAdapter(sql, cn)

' create a dataset

Dim ds As DataSet = New DataSet

' add table to the data set

ds.Tables.Add(New DataTable("clients"))

' fill the data adapter

da.Fill(ds.Tables("clients"))

' create a datarow

Dim dr As DataRow

' populate combo box

cboxClient.DataSource = ds.Tables("clients")

If newQuotationType = "client" Then

cboxClient.DisplayMember = "last_name"

cboxClient.ValueMember = "id_client"

Else

cboxClient.DisplayMember = "name"

cboxClient.ValueMember = "id_supplier"

End If

cn.Dispose()

End Sub


This is the code I use in the selectedValueChanged event:

Private Sub cboxClient_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboxClient.SelectedIndexChanged

Dim conn As SqlConnection = New SqlConnection

' connection string

conn.ConnectionString = strConn

Dim sql As String

If newQuotationType = "client" Then

sql = "SELECT * FROM tbl_clients WHERE id_client= " &
cboxClient.SelectedValue

ElseIf newQuotationType = "supplier" Then

sql = "SELECT * FROM tbl_suppliers WHERE id_supplier " &
cboxClient.SelectedValue

End If

MessageBox.Show(sql)

' create a new data adapter

Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)

' create a new dataset

Dim ds As DataSet = New DataSet

ds.Tables.Add(New DataTable("address"))

da.Fill(ds.Tables("address"))

Dim address As String = ds.Tables("address").Rows(0)("address").ToString +
vbNewLine

Dim city As String = ds.Tables("address").Rows(0)("city").ToString + " (" +
ds.Tables("address").Rows(0)("state").ToString + ")" + vbNewLine

Dim country As String = ds.Tables("address").Rows(0)("country").ToString + "
" + ds.Tables("address").Rows(0)("zipcode").ToString + vbNewLine

Dim phone As String = ds.Tables("address").Rows(0)("phone").ToString +
vbNewLine

Dim fax As String = ds.Tables("address").Rows(0)("fax").ToString

rtxtAddress.Text = address + city + country + phone + fax



"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
Jean Christophe Avard,

It looks like the combobox's ValueMember property has not been correctly
set.

Be sure that the column name you supply to the ValueMember property is
spelled exactly as in the datatable, including case.

You might post your code where you retrieve the datatable and then use it
as
the combobox's data source, etc.

Kerry Moorman
"Jean Christophe Avard" wrote:
Thank you man, but I have another q7uestion... Why do I get an error with
this:

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue

I also tried

sql = "SELECT * FROM tbl_clients WHERE id_client= " +
cboxClient.SelectedValue.toString()

And it always give me an error

"An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Operator is not valid for string "SELECT * FROM
tbl_clients WHERE " and type 'DataRowView'."

What is the way to go!?

"Kerry Moorman" <Ke**********@discussions.microsoft.com> wrote in message
news:<8D**********************************@microso ft.com>...

> Jean Christophe Avard,

>

> Retrieve the client name and ID from the database and store the data in
> a

> datatable.

>

> Assign the datatable to the combobox'x DataSource.

>

> Assign the client name to the combobox's DisplayMember.

>

> Assign the client ID to the combobox's ValueMember.

>

> In the combobox'x SelectedIndexChanged event, retrieve the ID from the

> combobox's SelectedValue property.

>

> Kerry Moorman

>

>

>

>

> "Jean Christophe Avard" wrote:

>

> > Hi! I'm designing an application that produces invoices. I have a
> > combobox

> > that is populated with the Name of every client in the database. What
> > I

> > would like to do is to have the combobox to display the name, and to
> > have

> > the value set to the ID of the record...

> >

> > Like in HTML where you have something like: <option
> > value="ID_NUMBER(primary

> > key)">CLIENT_NAME</option>

> >

> > so, let's say the user selects "Client #2" in the combo box, then I
> > would be

> > able to call the address of this client using the ID... SELECT * FROM

> > tbl_clients WHERE id_client = bombobox.selectedValue ...

> >

> > I don't know if I'm making myself clear, I will be around, so ask
> > anything

> > if I'm not enough clear. Thank y'all, this really apprecioated!

> >

> > Jean Christophe Avard


Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: athens.gr. | last post by:
VIRTUALIZATION ENGINE CONSOLE A RUNTIME 1.1 - IBM (3 CDs), WEBSPHERE BUSINESS INTEGRATION CONNECT ADVANCED v4.2.2 - IBM, other 16,000 more CDs, (free donge)! No time limitation! ...
0
by: Krisa | last post by:
Hello all, I just discovered something (stop me if you've heard this before....) that was causing me a significant performance hit when opening a form with subforms. To speed up loading the...
4
by: godber | last post by:
I need to populate text boxes for instance with employee information using their unique employee works number selected from a combo box. Can anyone help, I am told thru visual basic this can be...
1
by: Access User | last post by:
Here is my situation: I have two tables linked together in a many-to-many relation through an interum table. It looks like this... Title TitleTopic Topic...
3
by: ILCSP | last post by:
Heello, I'm using Access 2000. I have a form with a combo box that has a query as its row source and it's bound to column 1. This combo box is unbound to a record like the rest of the form...
2
by: Al | last post by:
hi,I am using asp.net(vb) I am trying to create 3 combo boxes which are dependable on each other. when comb1 is selected, it shows the comb2 data based on the selection in combo1 and when combo2...
3
by: Propoflady | last post by:
I am not sure you can do what I want to do but here goes, I have a client database, I have a field for company, first name and last name. What I would like is a combo box I could start type in say...
2
by: docsix | last post by:
I am having trouble populating a combo box in a subform. Currently I have two combo boxes on a single form that works. This is my current setup: Table:Facilities - FacilityID ... FacilityType 1...
30
ADezii
by: ADezii | last post by:
This week’s Tip of the Week will clearly demonstrate how you can dynamically set the Drop Down List Width of a Combo Box to the length of the longest item in its Row Source. The inspiration for this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.