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?
gbmcneilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
ValueMember and DisplayMember should be column names:

ValueMember = "RecNo"
DisplayMember = "Description"

Bob
Fernando SotoRetiredCommented:
This is the problem. There are two tables

Table 0 has two columns
column 0 is named dataroot_ID
column 1 is named generated

Table 1 has three column
column 0 is named RecNo
column 1 is named Status
column 2 is named Description

Therefore table 0 has no object called RecNo and you get the error Object reference not set to an instance of an object.

To correct the problem make the following changes
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gbmcneilAuthor Commented:
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.




Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

gbmcneilAuthor Commented:
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.

gbmcneilAuthor Commented:
Excuse me Fernando. Spelled you name incorrectly. My apologies.
Fernando SotoRetiredCommented:
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
gbmcneilAuthor Commented:
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.
Fernando SotoRetiredCommented:
I am glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.