Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:10:16+00:00 2026-05-15T18:10:16+00:00

I am confuse where can i make my code to n-tier: While learning n-tier

  • 0

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;
        }
    }
    #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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:10:17+00:00Added an answer on May 15, 2026 at 6:10 pm

    If you want to have a nice, clean separation, here’s what you should do:

    • never ever pass something like a SqlDataReader or any other database-dependant object up from your data layer – encapsulate everything in your data layer, and from there on up, use your own domain model (classes)

    • the data layer should turn your database requests into objects of your domain model. You can definitely do that by hand – but it’s a lot of boring and error prone code to do all the DataReader, read each row, convert to object kind of stuff – here, a tool called an OR mapper (object-relational mapper) can help tremendously, since it does all of this for you – more or less for free. Check out SubSonic, Linq-to-SQL and quite a few more out there.

    • for things like combobox lookup lists, you would typically design a “view model”, e.g. a class for that “view” (or webform, or winform) that will hold the data that this view is supposed to a) show, and b) needs for its job. Typically, such a “view model” is just another class – no magic about it. It will contain one or several of your domain model classes (the actual data you want to show), and one or several lookup lists that contain the possible values for all the dropdowns etc.

    With this approach, you should be fine and well on track to a good solid design, and by using an ORM, you can save yourself a ton of boring code and concentrate on the more interesting parts of your app.

    Update:
    Sample for binding your combo box:

    • create a class for your lookup values, typically something like:

      public class StatusCode
      {
           public int ID { get; set; } 
           public string Description { get; set; }
      }
      
    • have a method in your data layer to retrieve all values from your StatusCode table into a List<StatusCode>

      public List<StatusCode> GetAllStatusCodes();
      
    • have your combo box in the UI bound to that list:

      cbxStatusCode.DataSource = statusCodeList;
      cbxStatusCode.DisplayMember = "Description";
      cbxStatusCode.ValueMember = "ID";
      

      Note: this is slightly different depending on whether you use Winforms or ASP.NET webforms.

    There you have it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can correctly setup up a windows hook, but I get confused by the
I am a little confused by the multitude of ways in which you can
I'm a bit confused here. Microsoft as far as I can tell claims that
I am confused between the term file modification time and file changed time. Can
I'm a little confused about how the standard library will behave now that Python
I am learning some good code practice that's why i was going through some
Can you explain what's going on with my code here? I'm not sure if
In front of me some different Technologies and I'm confused between them. GNU make,
I've read that compound primary keys will confuse the hell out of typical ORM
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.