473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Reference datatable row without for loop

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();

}


Aug 18 '06 #1
6 13477
DataRow row = dataTable.Rows[0];
string name = row["Name"].ToString(), id = row["ID"].ToString();

Marc
Aug 18 '06 #2
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" <tk*****@company.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
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();

}


Aug 18 '06 #3

"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 )" wrote:
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" <tk*****@company.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
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();

}




Aug 18 '06 #4
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
Aug 18 '06 #5
Hi,
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:83**********************************@microsof t.com...
>
"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.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 18 '06 #6
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


"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:83**********************************@microsof t.com...

"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.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JMCN | last post by:
help! i'm caught in the endless of "compile error message: do without loop." i thought that i closed all of my statments but it appears not. does anyone know why my structure is incorrect? what...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
48
by: Yahooooooooo | last post by:
no gotos no loops how is it possbile..... Asked in interview ... Regards MA
0
by: aboobackerpm | last post by:
i am using a binded dgv with datatable my problem is how i can update the changes in a cell of a dgv to datatable without move to next cell my dgv's allow addnew property is false because of...
5
by: defn noob | last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ...
2
by: nomaneagle | last post by:
ok this is the thing I have right now which is working quite well beside its a bit slow: Public Function GetList() As List(Of SalesOrder) Try Dim list As New List(Of...
0
by: nomaneagle | last post by:
ok this is the thing I have right now which is working quite well beside its a bit slow: Public Function GetList() As List(Of SalesOrder) Try Dim list As New List(Of SalesOrder) Dim ds As...
0
by: kumardharanik | last post by:
i need to fill the datatable(datagridview) without using the database.. Here is my code.. But i cant able to fill the datatable [CODE} Public Sub CreateDatatable() dtable = New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.