DataTable.Select

  • Thread starter Thread starter Tae Lee
  • Start date Start date
T

Tae Lee

Could someone please help me? I have searched everywhere
but couldn't find definite answers. I am trying to read
xml file to a dataset and select from it. Here's xml
file:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<data>
<id>1</id>
<name>Tae Lee</name>
</data>
<data>
<id>2</id>
<name>Tae Sung Lee</name>
</data>
</root>

Then here's the code.

Dim ds As New Data.DataSet
ds.ReadXml("data.xml")
Dim dt As DataTable = ds.Tables("data")
Dim str As String = "id = 2"
Dim dr As Data.DataRow = dt.Select(str)
Literal1.Text = dr.Item(1)

The problem is that when you select from datatable, the
result is 1-dimensional array of data row. So I get an
error when I attempt to put array into a row. If you
have any similar experience, please let me know. Thank
you so much.

-Tae Lee
 
Tae Lee said:
Could someone please help me? I have searched everywhere
but couldn't find definite answers. I am trying to read
xml file to a dataset and select from it. Here's xml
file:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<data>
<id>1</id>
<name>Tae Lee</name>
</data>
<data>
<id>2</id>
<name>Tae Sung Lee</name>
</data>
</root>

Then here's the code.

Dim ds As New Data.DataSet
ds.ReadXml("data.xml")
Dim dt As DataTable = ds.Tables("data")
Dim str As String = "id = 2"
Dim dr As Data.DataRow = dt.Select(str)
Literal1.Text = dr.Item(1)

The problem is that when you select from datatable, the
result is 1-dimensional array of data row. So I get an
error when I attempt to put array into a row. If you
have any similar experience, please let me know. Thank
you so much.

-Tae Lee

If you assume that you are always returning exactly one result, then you can
do this:

Dim dr as Data.DataRow = dt.Select(str)(0)

Just make sure you are always getting back exactly one result!

Erik
 
Hi Tae,
Dim str As String = "id = 2"
Dim dr As Data.DataRow = dt.Select(str)
Literal1.Text = dr.Item(1)
It has to be a datarow collection and therefore
Dim str As String = "id = 2"
Dim drcollection() As Data.DataRow = dt.Select(str)
if drcollection.length > 0 then
dim dr as datarow = drcollection(0)
Literal1.Text = dr.Item(1)
'or if you wish literal1.text = drcollection(0)(1)
end ifI hope this helps?
Cor
 
Back
Top