I am iterating a DataTable in my C# code. I try to get the contents using of a column named "columnName" of row named "row" using -
object value = row["ColumnName"];
I get this error -
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Column 'FULL_COUNT' does not belong to table . at System.Data.DataRow.GetDataColumn(String columnName)
How is this possible ? My SQL query/result set has a column by that name and the query even runs in management studio.
How do I fix this error ?
Solution 1 Add(new DataColumn("@Id", typeof(int))); DataRow dr = dt. NewRow(); dr["Id"] = 666; You will get the same error. DataTable dt = new DataTable(); dt.
So Table, tr, td belongs to table tags in HTML and form does not belong.
I am guessing your code is iteration supposed to be something like this
DataTable table = new DataTable();
foreach (DataRow row in table.Rows) {
foreach (DataColumn col in table.Columns) {
object value = row[col.ColumnName];
}
}
If this is the case, row["ColumnName"]
in each iteration looks for the same column with name ColumnName
which obviously does not exists in your table.
The correct way is row[ColumnName]
or row[col.ColumnName]
in iteration above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With