Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

column name in a strongly typed dataset?

Status
Not open for further replies.

Painkiller

Programmer
May 18, 2001
97
NL
Hi all,

I'm trying to read out the name of a column in a strongly typed dataset, not the value of the column, but the NAME of the column. I am able to read out the name of the table by using the .Tablename property of the typed dataset, however I can't find a similar property for columns. The typed dataset does have a columns collection, where you can access a column by using its name or it's index in the collection, not what I want.

Does anyone know how to read out the column names?

Much appreciated.
 
Hi phinoppix,

Yeah I know that there's a column collection, but you have to supply an index to access a specific column. My question is wether you can access the column names directy, without supplying an index.

Since the columns are defined in a typed dataset I would expect that I could easily read their names out, since you can access the column as a property to read out the respective value, no luck yet though...
 
DataTable.Columns["ColName"]

Code:
for (int i=0; i<datatable.Columns.Length; i++)
{
  string name = datatable.Columns[i].Name.ToString();
  
}
or something close to that? Not tested!

Age is a consequence of experience
 
Maybe I should explain a little more:

When creating a strongly typed datset the columns are added as properties of the specific datatable object and the values can be accessed by reading out the property values:

say you create a typed dataset 'RatingDc', with the datatables Rating and Rating_Type within. The tables then are defined as stronly typed datatables, you can access them by their names instead of using an index in the datatable property.

You fill the dataset with the rating_types from the database. You are then able to read out the values from the first row of the rating_type datatable by accessing the column names directly:

id = RatingDc.Rating_Type[0].ID;
code = RatingDc.Rating_Type[0].CODE;
desc = RatingDc.Rating_Type[0].DESCRIPTION;

etc.

I was hoping that it would be possible to somehow read out the name of the columns in the same way, without using an index or such. Seems like this is not possible...
 
Yep that is what I thought you wanted and I showed you, unless we are still on cross lines? I should say that in you post you are using DT’s and DS’s interchangeably even though you obviously know the difference between them.

Let us clarify this one more time; you want to get the DataTable.Name from the Columns Collection.
Code:
DataSet.DataTable.Columns[0].Name.ToString();
Sorry if I am still misunderstanding you. :(

Age is a consequence of experience
 
ok I have read this again and I have misread this bit (apologies)
Code:
You fill the dataset with the rating_types from the database. You are then able to read out the values from the first row of the rating_type datatable by accessing the column names directly:

The bit about this that is confusing is this: you have column that you don’t want to use an index number and you don’t know its name but you would like to know its name.

There are as far as I am aware only, index (foreach or for loop) to get a column name. what other way could there be? Perhaps you could supply a made up example of what you thought you would be able to do. Hypothetical. In you example you say to read out the columns in the same way but in you example you supply the column name?

Age is a consequence of experience
 
A little more background info:

I'm currently using the microsoft enterprise library, more specifically the data application block. The data application block has a function 'UpdateDataset' that enables you to save modified rows in a dataset to the database. You need to use a commandwrapper (a wrapper for a command object), where you specify which stored procedure will be run and you have to specify parameters for the command object to interface with the stored procedure.

When specifying parameters, you have to specify the source columns for the parameters by entering their specific names (of the columns). The specified columns of the modified rows of the dataset will then be channeled to the strored procedure so that each modified row will be updated.

Which is where my problem comes in. I can hard code a piece of text containing the column name in the commandwrapper, but i figured it would make the code more readible/more maintainible if I could read out the column name and enter that.

A little example:

As is situation:
I enter the column name "LAST_USED_YEAR" as a hard coded text:

objUpdateCommandWrapper.AddInParameter("param_Year", DbType.Int16, "LAST_USED_YEAR", DataRowVersion.Current)

Preferred situation:

objUpdateCommandWrapper.AddInParameter("param_Year", DbType.Int16, SystemDc.tblSYSTEM.LAST_USED_YEAR.ColumnName, DataRowVersion.Current)

Or something like this...(using an endex in this situation doesn't make any sense, at least not to me)

But I gather that this is not possible...
 
Even for typed datasets, there's no way (afaik) you can access any of the column properties without going through the Column collection, hence requires an index for single column. Unless, you have your own typed dataset generator that can add explicit column getters laid outside the column collection. [wink]
my 2 cents
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top