C# - get value of a column in a DataRow

C

Chet

I have a DataRow from a table in a data set. One of the columns has a
name of "COMPANY_TICKET_ID" and I'm trying to get this column's value
with the following:

foreach( DataRow[] row in ds.Tables[ 0 ].Rows )
Console.WriteLine( row[ "COMPANY_TICKET_ID" ].ToString() );

I get an error - cannot implicity convert type 'string' to 'int'.

What am I missing? Thanks.
 
P

Patrick Steele [MVP]

chet_111 said:
I have a DataRow from a table in a data set. One of the columns has a
name of "COMPANY_TICKET_ID" and I'm trying to get this column's value
with the following:

foreach( DataRow[] row in ds.Tables[ 0 ].Rows )
Console.WriteLine( row[ "COMPANY_TICKET_ID" ].ToString() );

I get an error - cannot implicity convert type 'string' to 'int'.

The Rows property returns a DataRowCollection that you are iterating
over with the foreach. However, each item in a DataRowCollection is a
DataRow, not a DataRow[]. Use:

foreach(DataRow row in ds.Tables[0].Rows )

Now that 'row' is a DataRow, you can index it by either index position
(int) or column name (string).
 
N

Nick

foreach(DataRow row in ds.Tables[0].Rows)
Console.WriteLine(((int)row["COMPANY_TICKET_ID"]).ToString());
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top