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"];
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.
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