Datagrid.databind error when dataset XML contains attributes...

R

ringo

I've come accross an interesting problem populating an asp.net datagrid. I am trying to bind
XML data to a datagrid as I've done in countless other applications, the only difference this
time being that the XML I'm getting has attributes within the elements which I need to bind
to the grid. Example XML (I need to populate the datagrid with the address, name, and
CityStateZip elements):

<properties>
<record display="properties" no="1">
<grp display="">
<Address display="Street Address">906 MAIN ST</Address>
<CityStateZip display="Address 2">RALEIGH, NC 55555</CityStateZip>
<Name display="Name">SMITH JOHN E</Name>
</grp>
</record>
</properties>

My code is as follows (strXML is a string containing the XML above, dgProperty
is the datagrid):

Dim rdrXML As System.IO.StringReader
Dim dsResults As New DataSet()

rdrXML = New System.IO.StringReader(strXML)
dsResults.ReadXml(rdrXML)

dgProperty.DataSource = dsResults.Tables("grp")
dgProperty.DataBind()

On the last statement, the application throws the error:

A field or property with the name 'Name' was not found on the selected datasource.

dsResults.Tables("grp").Columns.Contains("Name") returns true, so I know it's in there.
Furthermore, if I remove the 'display' attributes from the elements to be bound, as in:

<properties>
<record display="properties" no="1">
<grp display="">
<Address>906 MAIN ST</Address>
<CityStateZip>RALEIGH, NC 55555</CityStateZip>
<Name>SMITH JOHN E</Name>
</grp>
</record>
</properties>

everything works just fine. After many hours of searching for an answer I'm at the
end of my rope. Any suggestions are much appreciated!
 
T

Travis Murray

Ringo,

Are you sure this is true:
dsResults.Tables("grp").Columns.Contains("Name") returns true, so I know
it's in there.

Based on the XML you provided below, that should return false.

Your data is nested so much that the below XML (assuming this was the whole
file) would create the following tables:

record
grp
Address
CityStateZip
Name

The grp table only contains grp_Id, display, and record_Id.

If you take the "display" attribute out of your Name element it will pull
the Name into the grp table instead of creating a separate Name table.

Travis Murray
MCSD, MCT
Artiem Consulting, Inc
http://www.artiem.com
 
R

ringo

Thanks for your response.

Travis said:
Ringo,

Are you sure this is true:



it's in there.

Based on the XML you provided below, that should return false.

I'm positive this is true; I just tried it again. Since sometimes certain fields
are not included with the XML, part of the error checking code I snipped out is:

If dsResults.Tables("grp").Columns.Contains("Name") = False Then
dsResults.Tables("grp").Columns.Add("Name")
End If

which would then allow the XML to bind to the grid without the <Name> element
being present (and just be blank in the grid). Since it returns True, the Column
is not added to the dataset, and the error is thrown.
Your data is nested so much that the below XML (assuming this was the whole
file) would create the following tables:

record
grp
Address
CityStateZip
Name

The grp table only contains grp_Id, display, and record_Id.

If you take the "display" attribute out of your Name element it will pull
the Name into the grp table instead of creating a separate Name table.

Since I can't control the format of the XML coming in, how could I bind the
data I need to the grid?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top