I am getting InvalidCastException when i am trying to use this code.
I am not been able to resolve how to use Field.
Plz help me out
DataTable _Transaction= new DataTable();
float NetAmount=0;
//Records inserted into Table
for (int i = 0; i < _Transaction.Rows.Count; i++)
{
NetAmount += _Transaction.Rows[i].Field<object>("ItemAmount");
}
EDIT I actually want to retrieve value at specified columnName and Row no from a dataTable.
_Transaction is a name of table here
You’ll need to cast or convert
_Transaction.Rows[i].Field<object>("ItemAmount")to the same type asNetAmountso that the+=operater will be operating on a single type.For example, if
NetAmountis adecimaland the field is also a decimal useField<decimal>instead ofField<object>. If the field is not a decimal, useConvert.ToDecimalon it.