Link to home
Start Free TrialLog in
Avatar of gbmcneil
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-microsoft-com:officedata" _  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  _  
      xsi:noNamespaceSchemaLocation="MarketCapitalization.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.Clear()

        'Get dataset
        Dim ds As New System.Data.DataSet
        ds.ReadXml("..\dat\MarketCapitalization.xml")

        'Load into combobox control
        Me.cmbMarketCap.DataSource = ds.Tables(0).DefaultView
        Me.cmbMarketCap.ValueMember = ds.Tables(0).Columns("RecNo").ToString
        Me.cmbMarketCap.DisplayMember = ds.Tables(0).Columns("Description").ToString

The app is reporting an error when it hits the ValueMember assignment line.

It says - "An unhandled exception of type 'System.NullReferenceException' occurred. Additional information: Object reference not set to an instance of an object."

I'm stumped. Any thoughts?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

ValueMember and DisplayMember should be column names:

ValueMember = "RecNo"
DisplayMember = "Description"

Bob
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gbmcneil
gbmcneil

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.ValueMember = "RecNo"
        Me.cmbMarketCap.DisplayMember = "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.




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.ValueMember = ds.Tables(1).Columns("RecNo").ToString
        Me.cmbMarketCap.DisplayMember = ds.Tables(1).Columns("Description").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.

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).DefaultView
Me.cmbMarketCap.ValueMember = ds.Tables(table).Columns("RecNo").ToString
Me.cmbMarketCap.DisplayMember = ds.Tables(table).Columns("Description").ToString

Or

Me.cmbMarketCap.DataSource = ds.Tables("MarketCapitalization").DefaultView
Me.cmbMarketCap.ValueMember = ds.Tables("MarketCapitalization").Columns("RecNo").ToString
Me.cmbMarketCap.DisplayMember = ds.Tables("MarketCapitalization").Columns("Description").ToString

I like the first version, less to type

Have a good day
Fernando
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.