Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reading a row in a DataSet
Message
From
19/09/2003 10:31:34
 
 
To
19/09/2003 09:36:42
Jerry Tovar
Dana Corporation Dana It
Maumee, Ohio, United States
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00830470
Message ID:
00830655
Views:
20
My example showed two ways of doing it when you want a string. The second way, using a cast, is how you'd do it if you didn't want the value as a string.
int myvar = (int)ds.Tables["mytable"].Rows[0]["myfield"];
Not to further confuse you, but if you used Typed DataSets, instead of the regular DataSets, these would be strongly typed and you wouldn't need to cast. You can generate the Typed DataSets by right-clicking on the .xsd in your project and choosing the "generate" option. The syntax for accessing the Typed DataSets is a bit "cleaner", although you can still access a Typed DataSet in the conventional manner as well.
// Typed DataSet example:
int myvar = ds.MyTable[0].myfield;
~~Bonnie



>Another question, what if the field I want is a numeric and I want the numeric returned? How would I do this?
>
>int myvar = ds.Tables["mytable"].Rows[0]["myfield"].???;
>
>
>
>
>>Jerry,
>>
>>You don't need to jump through quite so many hoops, in fact your example loops through every row, and you said you only wanted the first row. Anyway, assuming you want it in a string (as in your example), you'd do this:
>>
>>string myvar = ds.Tables["mytable"].Rows[0]["myfield"].ToString();
>>// -or-
>>string myvar = (string)ds.Tables["mytable"].Rows[0]["myfield"];
>>
>>
>>~~Bonnie
>>
>>
>>>Using .Net 2003, how can I easily read the first row of a DataSet?
>>>
>>>Here's how I currently read the first row in a DataSet. This works, but I assume there is a better way.
>>>
>>>//ds contains my DataSet.
>>>string myvar = "";
>>>DataTable dt = ds.Tables["mytable"];
>>>foreach (DataRow DRow in dt.Rows)
>>>{
>>>myvar = DRow["myfield"].ToString();
>>>}
>>>
>>>Thanks,
>>>
>>>Jerry
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