.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Lee Hnetinka

Home >> Forum >> ADO.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to get table column name from dataset table

Posted By: Pratima B     Posted Date: March 23, 2010    Points:2   Category :ADO.Net

I need to know the column name from one of my dataset table? Can i do it from C#?

Author: Lalij Mer             
Posted Date: March 25, 2010     Points: 5   

you can try this code sample.

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Data.SqlClient;

public class MainClass
static abstract SqlConnection con;

static void Main()
con = new SqlConnection("Server=(local)\\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI");

string str = "SELECT ID, FirstName, LastName FROM Employee";
SqlCommand cmd = new SqlCommand(str, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds, "Employee");

// Display the column names
DataColumn dc;
foreach ( dc in ds.Tables(0).Columns) {
Console.Write("{0,15}", dc.ColumnName);

// Add a newline after the column headings

// Display the data for each row. Loop through the rows first.
DataRow dr;
foreach ( dr in ds.Tables(0).Rows) {

// Then loop through the columns for the current row.
int i;
for (i = 1; i <= ds.Tables(0).Columns.Count; i++) {
Console.Write("{0,15}", dr(i - 1));

// Add a line break after every row

More reference you can look here


Please Check my answer if you help....
Thank You...


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend