Column abc does not belong to table?

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


Try this, make sure your column name is same

    DataTable dt = new DataTable();
    dt.Columns.Add("abc", typeof(string));
    
    DataRow dr = dt.NewRow();
    dr["abc"]="";

I had a similar issue on my c# code, using a dataset which I had successfully initialized and populated with data from the DB.

So my return set was:

data = new Byte[0];

data = (Byte[])(dataset.Tables[0].Rows[0]["systemLogo_img"]);

Of course the error was in t finding the column 'systemLogo_img'.

I noted that you simply do NOT have to call /qualify the column name. So the correction is:

data = new Byte[0];

data = (Byte[])(dataset.Tables[0].Rows[0].ItemArray[0]);

Simply put: use "ItemArray" at position.

Thanks


Do not write Gridview column names instead of Database column names.

dataGridViewEmployeeClass.Rows[n].Cells[0].Value = item["write the Database Column names"].ToString();

Tags:

C#

.Net

Datatable