Solved

Accessing data table column name

Posted on 2014-10-08
3
525 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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