Solved

Accessing data table column name

Posted on 2014-10-08
3
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

687 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