Solved

Accessing data table column name

Posted on 2014-10-08
3
510 Views
Last Modified: 2014-11-18
Hi,

l have a simple loop that loops through all the rows in a data table.  At any point within this loop I need to access the column name?

Can't seem to figure out how this can be done?

Any suggestions?
0
Comment
Question by:andyw27
3 Comments
 
LVL 3

Expert Comment

by:jcrozier21
ID: 40370041
I need more information about your environment. Is this a VB.NET app? DAO? C? MS Access? Using a particular activex control?
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40370083
Not clear about the question, but if you want to get the name of the column you can access it at any time in the loop by using the column.columnname property. Now te way you access it is almost the same in C# and vb.net ..

From http://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

VB
Private Sub PrintColumnNames(dataSet As DataSet)
    Dim table As DataTable
    Dim column As DataColumn 

    ' For each DataTable, print the ColumnName. 
    For Each table in dataSet.Tables
        For Each column in table.Columns
        Console.WriteLine(column.ColumnName)
        Next 
    Next 
End Sub 

Open in new window


C#
private void PrintColumnNames(DataSet dataSet)
{
    // For each DataTable, print the ColumnName. 
    foreach(DataTable table in dataSet.Tables)
    {
        foreach(DataColumn column in table.Columns)
        {
            Console.WriteLine(column.ColumnName);
        }
    }
}

Open in new window

0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 40370086
Well, it depends how do you approach database.
With DataTable you have ColumnName property for each DataColumn
So using DataColumnCollection you could do something like
foreach(DataColumn column in MyDataTable.Columns)
{
    Console.WriteLine(column.ColumnName);
}

Open in new window


If you're using (Sql)DataReader just use GetName method, i.e.

var reader = cmd.ExecuteReader();
for(int i=0;i<reader.FieldCount;i++)
{
   Console.WriteLine(reader.GetName(i));
}

Open in new window

HTH

Ivo Stoykov
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question