C# - get value of a column in a DataRow

  • Thread starter Thread starter Chet
  • Start date Start date
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.
 
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).
 
foreach(DataRow row in ds.Tables[0].Rows)
Console.WriteLine(((int)row["COMPANY_TICKET_ID"]).ToString());
 
Back
Top