Ian
Ian

Reputation: 4909

Field name not in DataSet

Edit: 1) Name != name, this isn't the problem. The code works 99.9999% of the time and only fails in production occassionally (Never under lab conditions >_<) We are deploying debugging code which should explain what is going on, but until we can do that, just looking for if anyone has seen anything similar.

Edit: 2) Just for information. To test, I've run the following code

...
var name = row["Name"].ToString().ToLower();
var name2 = row["name"].ToString().ToLower();
...

And this actually works fine, suggesting, in framework 4 at least, row[xx] is not case sensitive. Either way, it's not the problem I'm seeing unfortunately. Wish it was :)

Here's a simple snippet of code which is causing us problems. It isn't just this bit of code, it's actually any code that looks like this. The common part is row["name"] throwing an exception claiming that "name" is not a column of table Table.

var command = new SqlCommand("SELECT Name FROM Table1");
DataSet result = helper.ExecuteQuery(command); 
if (result == null || result.Tables.Count != 1)
{
    return;
}
foreach (DataRow row in result.Tables[0].Rows)
{
    var name = row["Name"].ToString().ToLower();                
}

helper.ExecuteQuery returns a DataSet,

As you can see we know the result is a dataset, i.e. it's not null and it contains one table.

We know it has rows because we're in the foreach loop. We know Name exists as a column because the sql statement that is run is always pretty much hard coded into there with no ambiguity or cleverness going on.

Any thoughts (related to the problem?) There are some work arounds, using the int indexer, most of these queries return one or two columns only. Check for the columns first, all this sort of thing, but I have never encountered this problem in more years than I can remember.

Upvotes: 0

Views: 1207

Answers (2)

dgarbacz
dgarbacz

Reputation: 962

When referencing by the column name, the case actually does matter.

row["Name"] is different than row["name"]

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can try with name in lower

 var name = row["name"].ToString().ToLower(); //name in lower

Upvotes: 1

Related Questions