locked
How to handle SqlDatareader on N-tier application ? RRS feed

  • Question

  • I am confuse where can i make my code to n-tier:
    While learning n-tier i know now how to insert,delete,update.
    But now i am confused how to deal with sqldatareader to bind data on listbox and combo box:

    This code works on my presentation layer but dont know how to convert it to layers as DataAccess,BusinessObject,BusinessLogic.

    FormLoad
    {
    getlistview();
    cboStatus();
    }

    #region "fill listview"
           public void GetlistView()
            {

                int i = 0;

                SqlConnection sqlcon = new SqlConnection(connStr);
               lstBF.Items.Clear();
                SqlCommand sqlcom = new SqlCommand("sp_LoadNew", sqlcon);
                SqlDataReader dr;
                lstBF.Items.Clear();
                sqlcon.Open();
                dr = sqlcom.ExecuteReader();

                while (dr.Read())
                {

                    lstBF.Items.Add(dr["SerialNumber"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["PartNumber"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["StatusDescription"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["CustomerName"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["DateCreated"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["CreatedBy"].ToString());
                    lstBF.Items[i].SubItems.Add(dr["ModifiedBy"].ToString());


                    i = i + 1;
                }

                if (sqlcon.State == ConnectionState.Open) sqlcon.Close();
            }



            #endregion
            #region "ListviewChange"
            private void lstBF_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (lstBF.SelectedItems.Count == 1)
                {

                  txtSerialNumber.Text = lstBF.SelectedItems[0].Text;
                   txtPartNumber.Text  = lstBF.SelectedItems[0].SubItems[1].Text;
                   lblStatus.Text  = lstBF.SelectedItems[0].SubItems[2].Text;
                  lblcustomer.Text = lstBF.SelectedItems[0].SubItems[3].Text;
                 lblModifiedBy.Text  = lstBF.SelectedItems[0].SubItems[6].Text;

                }
                else
                {
                }
            }
            #endregion
     #region "FILL combo"
            public void cboStatus()
            {
              
                try
                {
                  SqlConnection conn = new SqlConnection(connStr);
                    SqlCommand sqlcom = new SqlCommand("sp_loadStatus",conn);
                    SqlDataReader dr = null;
                    conn.Open();
                    dr = sqlcom.ExecuteReader();
                    cmbStatus.Items.Clear();
                    while (dr.Read())
                    {
                         cmbStatus.Items.Add((dr["StatusDescription"]));
                    }

                    if (conn.State == ConnectionState.Open) conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Occurred:" + ex);
                }
                finally
                {
                 
                }
            }

            #endregion
    Friday, July 16, 2010 12:46 AM

Answers

  • It is all depends upon which technology you are using on your business layer - WCF, Web Service etc. Normally the business layer and the presentation layer are written on same technology (but it can vary depending up requirement) and therefore you can pass your object reference - the same you can do using datareader. 
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. My Blog: http://ashishkhandelwal.arkutil.com
    Friday, July 16, 2010 7:11 AM

All replies

  • It is all depends upon which technology you are using on your business layer - WCF, Web Service etc. Normally the business layer and the presentation layer are written on same technology (but it can vary depending up requirement) and therefore you can pass your object reference - the same you can do using datareader. 
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. My Blog: http://ashishkhandelwal.arkutil.com
    Friday, July 16, 2010 7:11 AM
  •  
    "Crimsonland" wrote in message news:00e6c1dd-7ed7-4bc3-bb1a-7587c219431f...
    I am confuse where can i make my code to n-tier:
    While learning n-tier i know now how to insert,delete,update.
    But now i am confused how to deal with sqldatareader to bind data on listbox and combo box:

    This code works on my presentation layer but dont know how to convert it to layers as DataAccess,BusinessObject,BusinessLogic.

     
    my response ----------------------------------------------------------------------------------------------------------------------------
     
    Sorry if this post went to another thread. The MS NNTP Bridge software is some kind of a joke when it comes to using it with Thunderbird. It does what it wants to do.
     
    1) You need to understand OOP programming, which is used in n-tier.

    What is Object-oriented-programming?

    (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs.

    The key concepts of OOP are the following:
    Class
    Object
    Instance
    Method
    Message passing
    Inheritance
    Abstraction
    Encapsulation
    Polymorphism
    Decoupling

    http://en.wikipedia.org/wiki/Object-oriented_programming


    2) You need to understand design patterns, which is used in n-tier.

    What are design patterns?

    Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.

    http://www.developer.com/design/article.php/1502691
    http://www.dofactory.com/Patterns/Patterns.aspx


    3) You need to understand Model View Presenter, which can be used by both Windows desktop and Web based UI(s) in n-tier.


    What is Model –View- Presenter?

    MVP is a software pattern considered a derivative of the Model-view-controller -- a design pattern.

    http://en.wikipedia.org/wiki/Model_View_Presenter


    You should take note on the word (object) is being used in the links above.

    The shows in the link below will show how to use MVP from a Web from and Windows form perspective.

    You should take note on how the objects are used to bind to controls with objects pass to/from MVP and how the n-tier layers are used.

    UI
    MVP
    Business Logic layer
    Service layer
    Data access layer


    MODEL-VIEW-PRESENTER

    http://www.polymorphicpodcast.com/

    click 'Shows'

    click 'Design Patterns Bootcamp: Model View * Patterns*

    view parts  1-5


    Friday, July 16, 2010 9:07 AM
  • Thanks Sorry for posting duplicate... :)
    Monday, July 19, 2010 12:51 AM