Nat Webb
Nat Webb

Reputation: 708

Column "name" does not belong to table

I have data that comes back from a BLL (outside my control) as a DataSet. I know that it only takes up one row of the relevant table, so I'm accessing it like so: _appData = myDataSet.Tables["TableName"].Rows[0]. I then access individual data points by calling that row with the column name: _appData["ColumnName"].ToString().

This works fine for most of my data, but I get a "column does not belong to table" error for a column that I know exists. Here's the error breaking in debug mode:

break in debug

As you can see the column is question is called "hadDataCompromise." Here's the column showing up when I break and dig into the DataSet -> Table -> Column:

enter image description here

And for good measure, I tried accessing the column in the Immediate Window, using the exact same syntax I use in my actual code, which works perfectly, returning the expected value:

enter image description here

Any clues why this might be happening? None of the similar questions seem to apply to my problem (most of them involve typos or extraneous quotation marks or brackets). And again, the code works fine for most columns, but randomly breaks on this one. Thanks!

Upvotes: 2

Views: 11169

Answers (1)

C4d
C4d

Reputation: 3272

There is a trailing space dude.

enter image description here

private string Retrieve(string columnName)
{
    return _appData[columnName.Trim()].ToString();
}

The .Trim() function is fine but you should rather check the source your passed name is coming from.

Upvotes: 8

Related Questions