Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreach dataRow in dataTable.Rows syntax? 2

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I'm getting a build error in the below code:

Code:
foreach (DataRow dataRow in dataTable.Rows)
{
    string start = dataRow.Field<0>;
    string pattern = dataRow.Field<1>;
}

The error says "invalid expression term" and underlines the semi-colons. I've tried to Convert.ToString(dataRow.Field<0>);, dataRow.Field<0>.ToString();, etc.

If I replace the angle-brackets with standard brackets I get a different type of error. I've googled this but I'm not sure what I'm doing wrong.

I know the values to be returned will be strings, I know the DataTable connection works...but in any event it's not even building due to whatever's wrong with my syntax.

This is with VS2008. Thanks.
 
brackets, not angels
Code:
foreach (DataRow dataRow in dataTable.Rows)
{
    string start = dataRow.Field[0];
    string pattern = dataRow.Field[1];
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks, but as I said I tried brackets. The error I get with brackets is:

"Cannot apply indexing with [] to an expression of type 'method group'
 
woops, we all missed it
Code:
foreach (DataRow dataRow in dataTable.Rows)
{
    string start = dataRow[0];
    string pattern = dataRow[1];
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
.Try something along these lines:

Code:
foreach (DataRow dataRow in dataTable.Rows)
{
    string start = dataRow.Field<String>(0);
    string pattern = dataRow.Field<String>(1);
}

May work, not tested.
 
Both of these syntaxes worked:

Code:
string start = dataRow.Field<String>(0);
string pattern = dataRow.Field<String>(1);

Code:
string start = dataRow[0].ToString();
string pattern = dataRow[1].ToString();

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top