title: How to convert DataSet to DataTable
body: <p>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. </p>
  
  <p>So far I have figured out how to retrieve the data and fill them in a dataset for display purpose (which is working perfectly)...</p>
  
  <pre><code>        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);
  </code></pre>
  
  <p>and how to save them in a CSV file with the help of following code from: <a href="http://www.evontech.com/login/topic/1983.html" rel="noreferrer">http://www.evontech.com/login/topic/1983.html</a></p>
  
  <pre><code>    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 &lt; 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();
      }
  </code></pre>
  
  <p>Now my problem is how do I convert this <code>DataSet</code> to <code>DataTable</code>? I have tried the way described here with <strong>NO</strong> luck: <a href="http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx" rel="noreferrer">http://www.ezineasp.net/post/ASP-Net-C-sharp-Convert-DataSet-to-DataTable.aspx</a></p>
  
  <p>Can anyone help me?</p>