Solved

Accessing data table column name

Posted on 2014-10-08
3
441 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now