Accessing a Row in a typed Dataset

G

Guest

I created a typed dataset from which I am trying to access the data. When I use the following code to access a row
string name =dataset.person[1].firstName
I receive an error System.IndexOutOfRangeException as if there are no rows. Yet when I dump the data to a file I receive (sample of file)
<?xml version="1.0" standalone="yes"?><Dataset1 xmlns="http://tempuri.org/Dataset1.xsd"><Table><accountNo>1</accountNo><firstName>John </firstName><lastName>Smith </lastName><middleInitial></middleInitial></Table><Table><accountNo>2</accountNo><firstName>Michael </firstName><lastName>Kimson </lastName></Table><Table><accountNo>3</accountNo><firstName>Lewis </firstName

What am I doing wrong. How should I access the Rows of data in the dataset. I also tried to loop through the rows with a foreach loop but apparently there is no enumerator for this class
 
F

Felix Wu [MSFT]

Hi George,

What's the table name? "person"?

Have you tried this:

string name =dataSet11.Tables["person"].Rows[1]["firstName"].ToString();
or
string name =dataSet11.Tables[0].Rows[1]["firstName"].ToString();

You can use the following code to loop through the rows:

foreach(DataRow dr in dataSet11.Tables[0].Rows)
{
Console.WriteLine(dr["firstName"].ToString());
}

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
Thread-Topic: Accessing a Row in a typed Dataset
thread-index: AcPrMoSkof0TG5XoQ8GJ/tjKOYEBEw==
X-Tomcat-NG: microsoft.public.dotnet.general
From: "=?Utf-8?B?R2VvcmdlIEdyb2RlbnR6aWs=?=" <[email protected]>
Subject: Accessing a Row in a typed Dataset
Date: Wed, 4 Feb 2004 07:21:10 -0800
Lines: 7
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa07.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.general:123664
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.general

I created a typed dataset from which I am trying to access the data. When
I use the following code to access a row:
string name =dataset.person[1].firstName;
I receive an error System.IndexOutOfRangeException as if there are no rows.
Yet when I dump the data to a file I receive (sample of file):
<?xml version="1.0" standalone="yes"?><Dataset1
xmlns="http://tempuri.org/Dataset1.xsd"><Table><accountNo>1</accountNo><firs
tName>John </firstName><lastName>Smith
</lastName><middleInitial></middleInitial></Table><Table><accountNo>2</accou
ntNo><firstName>Michael </firstName><lastName>Kimson
</lastName></Table><Table><accountNo>3</accountNo><firstName>Lewis
</firstName>

What am I doing wrong. How should I access the Rows of data in the dataset.
I also tried to loop through the rows with a foreach loop but apparently
there is no enumerator for this class.
 
M

Miha Markic [MVP C#]

Hi George

I think your table that has data is named Table and not person.
Do the check - look at the dataset.Tables[0].TableName property.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

George Grodentzik said:
I created a typed dataset from which I am trying to access the data. When
I use the following code to access a row:
string name =dataset.person[1].firstName;
I receive an error System.IndexOutOfRangeException as if there are no
rows. Yet when I dump the data to a file I receive (sample of file):
<?xml version="1.0" standalone="yes"?><Dataset1
xmlns="http://tempuri.org/Dataset1.xsd"><Table><accountNo>1</accountNo><firs
tName>John </firstName><lastName>Smith
</lastName><middleInitial></middleInitial></Table><Table><accountNo>2</accou
ntNo><firstName>Michael </firstName><lastName>Kimson
What am I doing wrong. How should I access the Rows of data in the
dataset. I also tried to loop through the rows with a foreach loop but
apparently there is no enumerator for this class.
 

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

Top