By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,731 Members | 829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,731 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"ringo" <rgp@ringosoft*NoSpam*.com> wrote in message
news:pn***********************@twister.southeast.r r.com... 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!

Nov 18 '05 #2

P: n/a
Thanks for your response.

Travis Murray wrote:
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.


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?

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

"ringo" <rgp@ringosoft*NoSpam*.com> wrote in message
news:pn***********************@twister.southeast.r r.com...
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!


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.