Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DataSet to DataTable

Tags:

html

c#

sql

asp.net

I am retrieving data from a SQL table so I can display the result on the page as a HTML table. Later I need to be able to save that table as a CSV file.

So far I have figured out how to retrieve the data and fill them in a dataset for display purpose (which is working perfectly)...

        string selectQuery = "SELECT Name, ProductNumber, ListPrice FROM Poduction.Product";          // Establish the connection to the SQL database          SqlConnection conn = ConnectionManager.GetConnection();         conn.Open();          // Connect to the SQL database using the above query to get all the data from table.         SqlDataAdapter myCommand = new SqlDataAdapter(selectQuery, conn);          // Create and fill a DataSet.         DataSet ds = new DataSet();         myCommand.Fill(ds); 

and how to save them in a CSV file with the help of following code from: http://www.evontech.com/login/topic/1983.html

    private void exportDataTableToCsv(DataTable formattedDataTable, string filename)     {        DataTable toExcel = formattedDataTable.Copy();        HttpContext context = HttpContext.Current;        context.Response.Clear();         foreach (DataColumn column in toExcel.Columns)        {           context.Response.Write(column.ColumnName + ",");        }         context.Response.Write(Environment.NewLine);        foreach (DataRow row in toExcel.Rows)        {           for (int i = 0; i < toExcel.Columns.Count; i++)           {              context.Response.Write(row.ToString().Replace(",", string.Empty) + ",");           }            context.Response.Write(Environment.NewLine);        }         context.Response.ContentType = "text/csv";        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");        context.Response.End();     } 

Now my problem is how do I convert this DataSet to DataTable? I have tried the way described here with NO luck: http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx

Can anyone help me?

like image 965
AlwaysANovice Avatar asked Jan 24 '12 07:01

AlwaysANovice


People also ask

How to convert DataSet to DataTable c#?

DataSet ds; DataTable dt= ds. Tables[0];

What is difference between DataSet and DataTable?

DataSet comprises one or many dataset tables which have the in-memory feature. DataTable holds a single or unit database table that has an in-memory feature. DataSet is formed collectively of datatables. DataTable is composed of multiple rows and columns to have better access to data.


2 Answers

A DataSet already contains DataTables. You can just use:

DataTable firstTable = dataSet.Tables[0]; 

or by name:

DataTable customerTable = dataSet.Tables["Customer"]; 

Note that you should have using statements for your SQL code, to ensure the connection is disposed properly:

using (SqlConnection conn = ...) {     // Code here... } 
like image 102
Jon Skeet Avatar answered Sep 21 '22 04:09

Jon Skeet


DataSet is collection of DataTables.... you can get the datatable from DataSet as below.

//here ds is dataset DatTable dt = ds.Table[0]; /// table of dataset 
like image 44
Pranay Rana Avatar answered Sep 24 '22 04:09

Pranay Rana