title: Asp.Net: Returning a DataSet from a Class
body: <p>I've decided to start another thread based on the responses I got in this thread:
  <a href="https://stackoverflow.com/questions/1980404/asp-net-returning-a-reader-from-a-class">Asp.Net: Returning a Reader from a Class</a></p>
  
  <p>I was returning a reader, but members have suggested I'd be better off returning a Dataset instead and also try to seperate the data access tier from the presentation tier.</p>
  
  <p>This is what I have so far:
  //my class methods</p>
  
  <pre><code>   public DataSet GetSuppliers()
      {
          SqlConnection conn = new SqlConnection(connectionString);
          SqlCommand cmd = new SqlCommand("con_spSuppliersList", conn);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@blogid", HttpContext.Current.Request.QueryString["p"]);
  
          return FillDataSet(cmd, "SuppliersList");
      }
  
      //my FillDataSet method
  
      private DataSet FillDataSet(SqlCommand cmd, string tableName)
      {
       SqlConnection conn = new SqlConnection(connectionString);
  
       cmd.Connection = conn;
       SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  
       DataSet ds = new DataSet();
           try
           {
               conn.Open();
               adapter.Fill(ds, tableName);
           }
           finally
           {
               conn.Close();
           }
       return ds;
  
      }
  </code></pre>
  
  <p>// on my ascx page I call the method like so:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
  
      //instantiate our class
      MyClass DB = new MyClass();
  
      // grab the table of data
      DataTable dt = DB.GetSuppliers().Tables["SuppliersList"];
  
      //loop through the results
      foreach (DataRow row in dt.Rows)
      { 
          this.supplierslist.InnerHtml += Server.HtmlEncode(row["Address"].ToString()) + "&lt;br/&gt;";
          this.supplierslist.InnerHtml += "&lt;b&gt;Tel: &lt;/b&gt;" + Server.HtmlEncode(row["Telephone"].ToString()) + "&lt;p/&gt;";
  
      }
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Would anyone like to suggest improvements?</p>
  
  <p>Is my loop 'data tier' or 'presentation tier', should the loop be inside the class and I just return a formatted string instaed of a dataset?</p>
  
  <p>Thanks for all the great advice</p>