Thursday, March 26, 2009

CSharp: How to fix following C# error in Visual Studio: Cannot apply indexing with [] to an expression of type 'System.Data.DataTable'

Error:
Cannot apply indexing with [] to an expression of type 'System.Data.DataTable'

Cause:
string variable = Table[0][1].ToString();

Solution:
DataTable Table;string variable = Table.Rows[index][column-index].ToString();

Example:

DataTable Table;
string variable = Table.Rows[0][1].ToString();

The above error happens when you try to index the datatable without first accessing the "Rows" property. You must first access the "Rows" property and then provide the index.

No comments: