Getting a Single Value From a DataSet

This seems like such a silly question, but I haven't found the answer yet.

How do I get a single value from a DataSet? Like, if the DataSet has a table "TableX" which has a field/column "ValueX", then how do I get "ValueX" for a given row? I've only been using DataSets to bind whole columns and stuff thus far.
10/15/2003 9:03:21 PM 91979 articles. 4 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 53

If you are returning a 1x1 result (e.g. SELECT Count(*) FROM Table1), then you should look at using the .ExecuteScalar() method instead of using a DataSet.  You can find it on all of the providers (SqlCommand, OleDbCommand).

Incidentally, have you checked out the Microsoft Data Application Block? This will often get you down to one line of code for a statement like above.
Ryan Dunn
The Book
LDAP Programming Help
10/15/2003 9:10:20 PM
It's actually a whole row worth of stuff assigned to various controls. I typically use an SqlDataReader for the task, but I'm worried about maintaining a forward-only structure for my current needs. There HAS to be a simple way to just grab a piece of data!

PS-Cool link. Looks handy.
10/15/2003 9:22:46 PM
Why are you worried about using a SqlDataReader? Just open it as late as possible, and close it as soon as possible.
This posting is provided "AS IS" with no warranties, and confers no rights.
10/15/2003 9:44:37 PM
Well, I'm not sure of the order I will be calling the fields, and I don't know whether I'll be able to access fieldX if I already move past it to fieldZ. Can I?

I've run into problems binding an SqlDataReader to multiple controls before because of the forward-only nature of the reader (I had two drop downs which both had the same "color" data). I'm worried that I'll run into the same situation with what I'm doing now.
In a nutshell, I want my controls' binds to be independant of any sort of required sequencing such that rearranging the binds or adding an out of sequence control won't break the system.
Regardless, it would be nice to know how to access a single value of a DataSet.
10/15/2003 10:25:04 PM
Just so you know :)

ds.tables(0).rows(0).item(0).tostring - return the first record in the first row of the first table.
Brian O'Connell (MCAD) -
10/15/2003 10:44:00 PM