Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ArgumentException Column <ColumnName> does not belong to table

Tags:

.net

.net-3.5

I am having a problem when accessing a DataColumn value in datatable with its name the System.ArgumentException Column <ColumnName> does not belong to table occurs for some of the columns. However the column exists in the database but with a different case. I thought DataTable column names are case insensitive. Any 1 got any idea why I am getting this. On a different machine this code is working fine. I don't think it has any thing to do with SQL Coallation but in this case the coallation are same on both SQL servers.

vpg_awardtype = row["vpg_awardtype"];
vpg_eventcount = row["vpg_eventcount"];

If i change it to the following then it's working:

vpg_awardtype = row["Vpg_AwardType"];
vpg_eventcount = row["Vpg_EventCount"];
like image 347
S M Kamran Avatar asked Feb 24 '23 04:02

S M Kamran


1 Answers

I've resolved it. Basically when a DataTable contains a simillar column name whose name is only different in Case i.e AwardType and awardType are same column names but only different in spelling case. If such a collision occur inside a DataTable This makes the access to all DataTable columns as case sensitive.

In my case there was some joins applied by a developer to get the data in the datatable. However the number of columns were not filtered and unfortunately in 2 different tables one of the column name was simillar but written with different case. I wasn't able to notice that because the datatable was huge. Finally I found this out the hard way. Hence aliasing one of the column name worked for me.

like image 56
S M Kamran Avatar answered Apr 20 '23 00:04

S M Kamran