help rewriting this bit of code
-
hi everyone I'm trying to not only make my cod emore readable but also reusable so i'm trying to rewrite the following bit of logic;
protected void Page_Load(object sender, EventArgs e)
{
clsDB = new clsDB();
GetSalesmanDesc();if (!IsPostBack) Populate\_ddlCustomers(); } public void GetSalesmanDesc() { salesmanList = new List(); try { salesmanList = new List(); con = new SqlConnection(strConnString); SqlCommand com; com = new SqlCommand(); com.CommandType = CommandType.StoredProcedure; com.CommandText = "GetSalesmanDesc"; com.CommandTimeout = 300; // try for 5 mins com.Connection = con; com.Connection.Open(); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) { Salesman salesman = new Salesman(); salesman.SalesmanID = reader\["SalesmanCode"\] as String; salesman.SalesmanName = reader\["Salesman"\] as String; salesmanList.Add(salesman); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { con.Close(); } }
I would really like to try and keep the SQL actions separate from my main procedure so instead of the above I am tryign something like this:
public void GetSalesmanDesc()
{
salesmanList = new List();
clsDB.getSalesmanDesc();
}the code in clsDB.getSalesmanDesc() is:
public bool getSalesmanDesc() { com = new SqlCommand(); com.CommandTimeout = 300; // try for 5 mins com.CommandText = "GetSalesmanDesc"; com.Connection = sqlCon; com.CommandType = CommandType.StoredProcedure; da.SelectCommand = com; try { sqlCon.Open(); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) {
-
hi everyone I'm trying to not only make my cod emore readable but also reusable so i'm trying to rewrite the following bit of logic;
protected void Page_Load(object sender, EventArgs e)
{
clsDB = new clsDB();
GetSalesmanDesc();if (!IsPostBack) Populate\_ddlCustomers(); } public void GetSalesmanDesc() { salesmanList = new List(); try { salesmanList = new List(); con = new SqlConnection(strConnString); SqlCommand com; com = new SqlCommand(); com.CommandType = CommandType.StoredProcedure; com.CommandText = "GetSalesmanDesc"; com.CommandTimeout = 300; // try for 5 mins com.Connection = con; com.Connection.Open(); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) { Salesman salesman = new Salesman(); salesman.SalesmanID = reader\["SalesmanCode"\] as String; salesman.SalesmanName = reader\["Salesman"\] as String; salesmanList.Add(salesman); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { con.Close(); } }
I would really like to try and keep the SQL actions separate from my main procedure so instead of the above I am tryign something like this:
public void GetSalesmanDesc()
{
salesmanList = new List();
clsDB.getSalesmanDesc();
}the code in clsDB.getSalesmanDesc() is:
public bool getSalesmanDesc() { com = new SqlCommand(); com.CommandTimeout = 300; // try for 5 mins com.CommandText = "GetSalesmanDesc"; com.Connection = sqlCon; com.CommandType = CommandType.StoredProcedure; da.SelectCommand = com; try { sqlCon.Open(); SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) {
Your impulse to separate data access and processing is a good one. It's hard to know what level of advice to offer here – depending on the application anything up to a full blown entity management framework might be the right answer. Try passing salesmanList to the data access method you just pulled out, or having that method create and return the list. Also, there's probably a one line Linq query you can run against the database to create this list (I'll leave that to one of our Linq gurus).
-
Your impulse to separate data access and processing is a good one. It's hard to know what level of advice to offer here – depending on the application anything up to a full blown entity management framework might be the right answer. Try passing salesmanList to the data access method you just pulled out, or having that method create and return the list. Also, there's probably a one line Linq query you can run against the database to create this list (I'll leave that to one of our Linq gurus).
the application itself is a 1 page web app to allow a user to manage product/Salesman data in a gridview, nothing to special. I could rewrite the above bit of code t populate a dataset and use that to populate the ddlSalesman, I'm just wondering how I would do it using the exisiting List that I have created?