Helpful Information
 
 
Category: .Net Development
Combo box (display member) VB.Net

I am developing an application in vb.net and oracle as my database. my goal is to display the county names (display member) to the user while store county id (value member) in the combo box. when I run my code I get county id displayed in the combo box rather than the county names.

When I tried debugging my code I found out the display member and the value member get assigned the same value i.e ID.

What could possibly be wrong??


code
====================================


Dim Connect As OleDbConnection
Dim DA_County As OleDbDataAdapter
Dim DS_County As New DataSet()
Dim DT_County As New DataTable()
Dim gConnectString as string

gConnectString = "Provider=MSDAORA;" & _
"Data Source=dd;" & _
"User ID=dd; " & _
"Password=dd"; "

Connect = New OleDbConnection(gConnectString)
Connect.Open()

DA_County = New OleDbDataAdapter("Select CountyName Name, CountyNo ID from ipt.tblcounty order by CountyName", Connect)
DA_County.Fill(DS_County)
DT_County = DS_County.Tables(0)

cboCty.ValueMember = "ID"
cboCty.DataSource = DT_County
cboCty.DisplayMember = "Name"

Connect.Close()
Connect = Nothing

Originally posted by harshil_p
I am developing an application in vb.net and oracle as my database. my goal is to display the county names (display member) to the user while store county id (value member) in the combo box. when I run my code I get county id displayed in the combo box rather than the county names.

When I tried debugging my code I found out the display member and the value member get assigned the same value i.e ID.

What could possibly be wrong??


code
====================================


Dim Connect As OleDbConnection
Dim DA_County As OleDbDataAdapter
Dim DS_County As New DataSet()
Dim DT_County As New DataTable()
Dim gConnectString as string

gConnectString = "Provider=MSDAORA;" & _
"Data Source=dd;" & _
"User ID=dd; " & _
"Password=dd"; "

Connect = New OleDbConnection(gConnectString)
Connect.Open()

DA_County = New OleDbDataAdapter("Select CountyName Name, CountyNo ID from ipt.tblcounty order by CountyName", Connect)
DA_County.Fill(DS_County)
DT_County = DS_County.Tables(0)

cboCty.ValueMember = "ID"
cboCty.DataSource = DT_County
cboCty.DisplayMember = "Name"

Connect.Close()
Connect = Nothing

First, drop the DataTable. When you populate the DataSet, the DataTable is created automatically. You just have to assign a name for it.

Try this...



Dim Connect As OleDbConnection
Dim DA_County As OleDbDataAdapter
Dim DS_County As New DataSet()
Dim gConnectString as string

gConnectString = "Provider=MSDAORA;" & _
"Data Source=dd;" & _
"User ID=dd; " & _
"Password=dd"; "

Connect = New OleDbConnection(gConnectString)
Connect.Open()

DA_County = New OleDbDataAdapter("Select CountyName, CountyNo from ipt.tblcounty order by CountyName", Connect)
DA_County.Fill(DS_County,"County")

cboCty.DataSource = DS_County.Tables("County")
cboCty.ValueMember = "CountyNo"
cboCty.DisplayMember = "CountyName"

Connect.Close()
Connect = Nothing

Let me know if this works. If not, we can try something else.

I tried using your code in my application, it errors out with the following message. It still shows up the county # in the combo box.

Thanks


Error Message:
===========================================
Could not bind to the new display member. Parameter name: newDisplayMember.

Originally posted by harshil_p
I tried using your code in my application, it errors out with the following message. It still shows up the county # in the combo box.

Thanks


Error Message:
===========================================
Could not bind to the new display member. Parameter name: newDisplayMember.
What is "errors out"?????

When I said errors out, I meant that the application produces an exception. The application does not crash since I used try and catch in my code to trap and display this error message.

The net result after running your code is that I still get the county names instead of county #.

I am really surprised why this is happening because my code which I had posted as a question works fine if I connect to a SQL Server database. It would somehow not work for Oracle. I dont know why??

I would like to correct my statement in my earlier response.

IT SHOULD HAVE BEEN:
The net result after running your code is that I still get the county # instead of county name.

Originally posted by harshil_p
When I said errors out, I meant that the application produces an exception. The application does not crash since I used try and catch in my code to trap and display this error message.

The net result after running your code is that I still get the county names instead of county #.

I am really surprised why this is happening because my code which I had posted as a question works fine if I connect to a SQL Server database. It would somehow not work for Oracle. I dont know why??
Ahh ha! Technically, the code is correct. What you're doing is fine, but it's one of those little annoyances that you must deal with. The annoyance here is the Oracle database. :o Ok... it should display the county names, but keep in mind that the ID is hidden. Do you want to show both the ID and name in the dropdown box? If so, you'll have to change your query. If this is what you want to do, I'll type it out.

Herb

Heck.. I'll just do it..



To show both of them in the dropdownlist box, just do this

DA_County = New OleDbDataAdapter("Select CountyNo, (CountyNo + ' ' + CountyName) as TheName From ipt.tblcounty order by CountyName", Connect)
DA_County.Fill(DS_County,"County")

cboCty.DataSource = DS_County.Tables("County")
cboCty.ValueMember = "CountyNo"
cboCty.DisplayMember = "TheName"

HI VBGOD,

I am having somewhat same problem with populating combo box. Though my combo box is showing correct data that I need but when I run the code I get this exception error message:

system.ArgumentException: Could not bind the new display member.
Parameter Name: newDisplayMeber.................


My Code:

Dim oraCntrDS As DataSet
oraCntrDS = New DataSet("CntrID")
oraCntrAdapter.Fill(oraCntrDS)

cmbCntrId.DataSource = oraCntrDS.Tables("Center")
cmbCntrId.ValueMember = "Center_ID"
cmbCntrId.DisplayMember = "Center_ID"


The error is generated at

cmbCntrId.ValueMember = "Center_ID"

I would appreciate your help.

Thanks

Sometimes the order of the display member and the value member matters.

Try this:

Dim oraCntrDS As DataSet
oraCntrDS = New DataSet("CntrID")
oraCntrAdapter.Fill(oraCntrDS)

cmbCntrId.DisplayMember = "Center_ID"
cmbCntrId.ValueMember = "Center_ID"
cmbCntrId.DataSource = oraCntrDS.Tables("Center")

Sometimes the order of the display member and the value member matters.

Try this:

Dim oraCntrDS As DataSet
oraCntrDS = New DataSet("CntrID")
oraCntrAdapter.Fill(oraCntrDS)

cmbCntrId.DisplayMember = "Center_ID"
cmbCntrId.ValueMember = "Center_ID"
cmbCntrId.DataSource = oraCntrDS.Tables("Center")


I understand this is only problem with the word ID. if u rename the column name. and it works

thanks
sree










privacy (GDPR)