Reference datatable row without for loop

T

Tim Kelley

If I have a datatable with 1 record only, can I reference the data in the
row without a for loop? I can get to the data by using the following code,
but it would be nice if I didn't have to use the For loop.

foreach (DataRow row in dataTable.Rows)
{

string name = row["Name"].ToString();

string id = row["ID"].ToString();

}
 
M

Marc Gravell

DataRow row = dataTable.Rows[0];
string name = row["Name"].ToString(), id = row["ID"].ToString();

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You could do dataTable.Rows[0]["ID"].ToString();

Of course, a table that you know for sure will always have only one record
is not a table but a record, you better use another structure to hold this
info.
 
G

Guest

"Of course, a table that you know for sure will always have only one record
is not a table but a record, you better use another structure to hold this
info. "

?-- What' wrong with using a DataTable that you know only has one DataRow?
I do this all the time.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You could do dataTable.Rows[0]["ID"].ToString();

Of course, a table that you know for sure will always have only one record
is not a table but a record, you better use another structure to hold this
info.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Tim Kelley said:
If I have a datatable with 1 record only, can I reference the data in the
row without a for loop? I can get to the data by using the following
code, but it would be nice if I didn't have to use the For loop.

foreach (DataRow row in dataTable.Rows)
{

string name = row["Name"].ToString();

string id = row["ID"].ToString();

}
 
M

Marc Gravell

No; it's still a table with one record ;-p

I seem to be one of the minority who just don't tend to use DataTable etc;
but if I did, I would have no issue having a 1-row table, if only to keep
related parts of the system simple and consistent.

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Peter Bromberg said:
"Of course, a table that you know for sure will always have only one
record
is not a table but a record, you better use another structure to hold this
info. "

?-- What' wrong with using a DataTable that you know only has one DataRow?
I do this all the time.
Peter

IMO this is usually used when you are keeping config info or when you have
an unique instance of some piece of data.

In both cases is more clear to define a class with properties to access the
info instead of using a DataTable.

Now the OP did not state that this is the case so my suggestion may not
apply in this particular case.
 
G

Guest

Actually , I often get a row of information from a database that may be
user-specific, and I take the one DataRow from the resulting DataTable and
stick it in Session. Working with a DataRow is quite intuitive and it's a
pretty lightweight object IMHO. So you could do

string firstName=(string)((DataRow)Session["myRow"])["FirstName"];

-- for example.
I don't particularly feel compelled to create a special object for this
stuff when I can get at it easily as above, especially if i don't need to
access it often.

Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 

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