I decided to start another thread based on the answers I received in this thread:
Asp.Net: returning the reader from the class
I was returning the reader, but the participants suggested I better return the data set instead, and also try to separate the data access level from the presentation level.
This is what I still have: // class methods
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");
}
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;
}
// on my ascx page, I call the method as follows:
protected void Page_Load(object sender, EventArgs e)
{
MyClass DB = new MyClass();
DataTable dt = DB.GetSuppliers().Tables["SuppliersList"];
foreach (DataRow row in dt.Rows)
{
this.supplierslist.InnerHtml += Server.HtmlEncode(row["Address"].ToString()) + "<br/>";
this.supplierslist.InnerHtml += "<b>Tel: </b>" + Server.HtmlEncode(row["Telephone"].ToString()) + "<p/>";
}
}
}
Would anyone like to suggest improvements?
Is my loop a "data level" or a "presentation level", should the loop be inside the class, and I just return the formatted string set in the dataset?
Thanks for the great advice.