Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column abc does not belong to table?

Tags:

c#

.net

datatable

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 ?

like image 965
Steam Avatar asked Jan 07 '14 02:01

Steam


People also ask

How do you handle column does not belong to table in C#?

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.

Which does not belong to create a table in HTML?

So Table, tr, td belongs to table tags in HTML and form does not belong.


1 Answers

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

like image 92
AaA Avatar answered Oct 03 '22 00:10

AaA