views:

306

answers:

2

I have a database that has four columns like this

level_1, level_2, level_3, level_4

There are also id, name, etc., but only these four are the ones that matter to me at the moment.

I need to know which column, by columnname, has the value 'BOSS'.

For example, level_2 may have the value BOSS. I need to know that one of the four columns has the code BOSS and which column, by column name, it is.

The reason is that I have to later update this row but I never know where the value BOSS will be. It could be in level_4, for example.

I know there is a columnname property in DataSet, but I cannot figure out how to use that here. I cannot post my code because there are many iterations of it, none of which work. I am hoping I am overlooking something obvious.

I am using ASP.NET 2.0 and ODBC.

Thank you for any ideas.

just to clarify - I do not need to know how to get the columnname per se. The columnname property works well for that. I do not know which column will have the value BOSS above. Once I can determine that I will get the columnname property value.

The reason I do not know which column will say BOSS is because there are other values that could be in any of the four so the data entry person has to pick an open field and put in the code - it is an old database application I inherited.

update:

Here is what i came up with. I do not like it but here it is. It works but it feels wrong:

        String[] myValues = new String[5] {"BOSS", "ANOTHERCODE", "ANOTHERCODE", "ANOTHERCODE", "ANOTHERCODE"};
        int x = 0;
            foreach (String level in myValues)
            {
                foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    String myColumnValue = ds.Tables[0].Rows[0][x].ToString();
                    if (myColumnValue == level)
                    { return ds.Tables[0].Columns[x].ColumnName; }
                    x += 1;
                }
                x = 0;
            }
A: 

I am not sure what you need... your question is a bit fuzzy...

If you need to access the column name do like so: DataTable.Columns[columnNumber].ColumnName

Sergio
If it's okay can you tell me why it is fuzzy? Thanks. I'd like to be clearer next time.
johnny
also, thanks, I did mention that I was aware of the.columname property but that it is not the problem. I need to know which column the value will be in and columnname is not consistent. It can be any of the four.
johnny
I was a bit confused by this sentence: "I need to know which column, by columnname, has the value 'BOSS'."I thought it could be BOSS at the column name, not on the value...
Sergio
+1  A: 

Ok, so you have 4 columns that could have the value BOSS. I guess the best way to solve your problem would be to cycle thought these 4 columns until you get the Value BOSS, when you do find it, store the column name on a variable so you can get it back later.

Sergio