Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. help rewriting this bit of code

help rewriting this bit of code

Scheduled Pinned Locked Moved C#
helpdatabasecom
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pmcm
    wrote on last edited by
    #1

    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())
                {
    
    B 1 Reply Last reply
    0
    • P pmcm

      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())
                  {
      
      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      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).

      P 1 Reply Last reply
      0
      • B BobJanova

        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).

        P Offline
        P Offline
        pmcm
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups