Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to access data in a DataTable?
Message
From
19/05/2005 20:18:17
 
 
To
19/05/2005 20:07:13
General information
Forum:
ASP.NET
Category:
ADO.NET
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP4
Database:
MS SQL Server
Miscellaneous
Thread ID:
01016070
Message ID:
01016075
Views:
8
This message has been marked as the solution to the initial question of the thread.
Al,

You almost got it right. Try this:
SomeType someValue = (SomeType)dt.Rows[SomeRowNumber][SomeColumnName or Index];
Note that you have to do an explicit cast (since the DataRowColumn is merely an object). Also, beware of DBNull.Value. The above statement will crash if that column contains DBNull.Value. There are ways around this (typically by checking first before assigning).

~~Bonnie


>VS 2003:
>
>I think this question is absurdly basic but I've spent half a day in an unsuccessful search for the answer.
>
// Suppose I've defined and filled a DataSet, ds, with a single DataTable Table1:
>// I should be able to do something like
>DataTable dt = ds.Tables["Table1"];
>
>// Now that I have a reference to the table, how do I read values from its rows/columns?
>// I'd expect to be able to do something like
>SomeType someValue = dt.Rows[SomeRowNumber].Columns[SomeColumnName or Index].Value
>
>// but Intellisense is showing nothing of the sort.
I could find no code samples on the Web. Literally all I could find in MSDN under "Viewing Data in a Table" was "You can access the contents of a DataTable using the Rows and Columns collections of the DataTable."
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform