gbmcneil
asked on
Simple Combobox Load from XML File
Hi gang:
I'm working in VB.NET trying to load an idiot-level combobox from an XML file. You'd think it would be easy.
The list of options to appear was preped in Access and saved to an XML file. So, the format is straight forward and shouldn't have an errors. There was a schema file output at the sametime (although I am not including it as I don't feel its germane.).
The XML file data looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-micr osoft-com: officedata " _
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _
xsi:noNamespaceSchemaLocat ion="Marke tCapitaliz ation.xsd" generated="2005-07- _
02T10:13:43">
<MarketCapitalization>
<RecNo>1</RecNo>
<Status>Active</Status>
<Description>Less than $1 Million</Description>
</MarketCapitalization>
<MarketCapitalization>
<RecNo>2</RecNo>
<Status>Active</Status>
<Description>$1 to $4.9 Million</Description>
</MarketCapitalization>
</dataroot>
My research of this site says the code to load these two options should look something like what's below. I am trying to load a combo called cmbMarketCap with two market cap ranges, regardless of what their "Status" value stored in the XML file.
Imports System.XML
Imports System.Data
...
'Clear target combobox to be on safe side
Me.cmbMarketCap.Items.Clea r()
'Get dataset
Dim ds As New System.Data.DataSet
ds.ReadXml("..\dat\MarketC apitalizat ion.xml")
'Load into combobox control
Me.cmbMarketCap.DataSource = ds.Tables(0).DefaultView
Me.cmbMarketCap.ValueMembe r = ds.Tables(0).Columns("RecN o").ToStri ng
Me.cmbMarketCap.DisplayMem ber = ds.Tables(0).Columns("Desc ription"). ToString
The app is reporting an error when it hits the ValueMember assignment line.
It says - "An unhandled exception of type 'System.NullReferenceExcep tion' occurred. Additional information: Object reference not set to an instance of an object."
I'm stumped. Any thoughts?
I'm working in VB.NET trying to load an idiot-level combobox from an XML file. You'd think it would be easy.
The list of options to appear was preped in Access and saved to an XML file. So, the format is straight forward and shouldn't have an errors. There was a schema file output at the sametime (although I am not including it as I don't feel its germane.).
The XML file data looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-micr
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _
xsi:noNamespaceSchemaLocat
02T10:13:43">
<MarketCapitalization>
<RecNo>1</RecNo>
<Status>Active</Status>
<Description>Less than $1 Million</Description>
</MarketCapitalization>
<MarketCapitalization>
<RecNo>2</RecNo>
<Status>Active</Status>
<Description>$1 to $4.9 Million</Description>
</MarketCapitalization>
</dataroot>
My research of this site says the code to load these two options should look something like what's below. I am trying to load a combo called cmbMarketCap with two market cap ranges, regardless of what their "Status" value stored in the XML file.
Imports System.XML
Imports System.Data
...
'Clear target combobox to be on safe side
Me.cmbMarketCap.Items.Clea
'Get dataset
Dim ds As New System.Data.DataSet
ds.ReadXml("..\dat\MarketC
'Load into combobox control
Me.cmbMarketCap.DataSource
Me.cmbMarketCap.ValueMembe
Me.cmbMarketCap.DisplayMem
The app is reporting an error when it hits the ValueMember assignment line.
It says - "An unhandled exception of type 'System.NullReferenceExcep
I'm stumped. Any thoughts?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello LearnedOne:
Thanks for responding. With your changes the new code to load the combobox became:
'Load into combobox control
Me.cmbMarketCap.DataSource = ds.Tables(0).DefaultView
Me.cmbMarketCap.ValueMembe r = "RecNo"
Me.cmbMarketCap.DisplayMem ber = "Description"
When I ran it, the following error message appeared:
"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Could not bind to the new display member."
I assume that the changes indicated were what you wanted me to do.
Thanks for responding. With your changes the new code to load the combobox became:
'Load into combobox control
Me.cmbMarketCap.DataSource
Me.cmbMarketCap.ValueMembe
Me.cmbMarketCap.DisplayMem
When I ran it, the following error message appeared:
"An unhandled exception of type 'System.ArgumentException'
Additional information: Could not bind to the new display member."
I assume that the changes indicated were what you wanted me to do.
ASKER
Ok FernandoSoto:
You're on.
I changed my combo box load routine to what you suggested (i.e., "Tables(1)"):
'Load into combobox control
Me.cmbMarketCap.DataSource = ds.Tables(1).DefaultView
Me.cmbMarketCap.ValueMembe r = ds.Tables(1).Columns("RecN o").ToStri ng
Me.cmbMarketCap.DisplayMem ber = ds.Tables(1).Columns("Desc ription"). ToString
Voila. It worked!
No need to respond further, but why the heck would Microsoft have MS Access create two tables in export mode (that is, when exporting to the file extension XML? This doesn't make sense.
Also, if I were to modify the exported XML table so that it read:
<?xml version="1.0" encoding="UTF-8"?>
<MarketCapitalization>
<RecNo>1</RecNo>
<Status>Active</Status>
<Description>Less than $1 Million</Description>
</MarketCapitalization>
<MarketCapitalization>
<RecNo>2</RecNo>
<Status>Active</Status>
<Description>$1 to $4.9 Million</Description>
</MarketCapitalization>
would this correct the problem such that I could use the original ds.Tables(0) reference? I suspect it would.
Thanks, Feranado.
You're on.
I changed my combo box load routine to what you suggested (i.e., "Tables(1)"):
'Load into combobox control
Me.cmbMarketCap.DataSource
Me.cmbMarketCap.ValueMembe
Me.cmbMarketCap.DisplayMem
Voila. It worked!
No need to respond further, but why the heck would Microsoft have MS Access create two tables in export mode (that is, when exporting to the file extension XML? This doesn't make sense.
Also, if I were to modify the exported XML table so that it read:
<?xml version="1.0" encoding="UTF-8"?>
<MarketCapitalization>
<RecNo>1</RecNo>
<Status>Active</Status>
<Description>Less than $1 Million</Description>
</MarketCapitalization>
<MarketCapitalization>
<RecNo>2</RecNo>
<Status>Active</Status>
<Description>$1 to $4.9 Million</Description>
</MarketCapitalization>
would this correct the problem such that I could use the original ds.Tables(0) reference? I suspect it would.
Thanks, Feranado.
ASKER
Excuse me Fernando. Spelled you name incorrectly. My apologies.
Hi gbmcneil;
Actually the two lines are needed to meet XML format standards. But what you can do is reference the table number by name like so
Dim table As String = "MarketCapitalization"
Me.cmbMarketCap.DataSource = ds.Tables(table).DefaultVi ew
Me.cmbMarketCap.ValueMembe r = ds.Tables(table).Columns(" RecNo").To String
Me.cmbMarketCap.DisplayMem ber = ds.Tables(table).Columns(" Descriptio n").ToStri ng
Or
Me.cmbMarketCap.DataSource = ds.Tables("MarketCapitaliz ation").De faultView
Me.cmbMarketCap.ValueMembe r = ds.Tables("MarketCapitaliz ation").Co lumns("Rec No").ToStr ing
Me.cmbMarketCap.DisplayMem ber = ds.Tables("MarketCapitaliz ation").Co lumns("Des cription") .ToString
I like the first version, less to type
Have a good day
Fernando
Actually the two lines are needed to meet XML format standards. But what you can do is reference the table number by name like so
Dim table As String = "MarketCapitalization"
Me.cmbMarketCap.DataSource
Me.cmbMarketCap.ValueMembe
Me.cmbMarketCap.DisplayMem
Or
Me.cmbMarketCap.DataSource
Me.cmbMarketCap.ValueMembe
Me.cmbMarketCap.DisplayMem
I like the first version, less to type
Have a good day
Fernando
ASKER
Many thanks Fernando. You're a champ. I would not have figured that out. I previously spent 2 days looking for an answer to this issue.
I am glad I could help.
ValueMember = "RecNo"
DisplayMember = "Description"
Bob