4
\$\begingroup\$

I'm new to C# and OO programing. I have an aspx page with 3 lists of checkboxes and I would like to generate them from the DB. how should I structure my code?

Here is an example of what I did so far:

  1. ASPX page

    <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
    <asp:CheckBoxList ID="CheckBoxList2" runat="server"></asp:CheckBoxList>
    <asp:CheckBoxList ID="CheckBoxList3" runat="server"></asp:CheckBoxList>
    
  2. ASPX Code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
     if (!IsPostBack)
     {
     GenerateCheckBoxList1();
     GenerateCheckBoxList2();
     } 
    }
    private void GenerateCheckBoxList1()
    {
     CBLists cbl = new CBLists();
     CheckBoxList1.DataSource = cbl.GetCheckBoxList1();
     CheckBoxList1.DataTextField = "name";
     CheckBoxList1.DataValueField = "id";
     CheckBoxList1.DataBind();
    }
    private void GenerateCheckBoxList2()
    {
     CBLists cbl = new CBLists();
     CheckBoxList2.DataSource = cbl.GetCheckBoxList2();
     CheckBoxList2.DataTextField = "country";
     CheckBoxList2.DataValueField = "country_id";
     CheckBoxList2.DataBind();
    }
    
  3. a class file to handle the connection to the db and return the DataSet:

    public class CBLists
    {
     private SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString);
    public DataSet GetCheckBoxList1()
    {
     try
     {
     DataSet ds = new DataSet();
     string cmdstr = "select * from table1 where id<>999 order by id";
     SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
     adp.Fill(ds);
     return ds;
     }
     catch (Exception ex)
     {
     return null;
     }
     finally
     {
     conn.Close();
     }
    }
    public DataSet GetCheckBoxList2()
    {
     try
     {
     DataSet ds = new DataSet();
     string cmdstr = "select * from table2";
     SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
     adp.Fill(ds);
     return ds;
     }
     catch (Exception ex)
     {
     return null;
     }
     finally
     {
     conn.Close();
     }
    }
    

Is this the "correct" way to do it? multiple methods in a separate file?

svick
24.5k4 gold badges53 silver badges89 bronze badges
asked Dec 16, 2013 at 10:30
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

In 3-Layer design, we have below three layers

Presentation Layer: In this case your aspx and code behind files.

Business Layer: It seems this is missing in your project. You may include this for various purposes e.g., filtering items based on different scenarios, logged in user, etc.

Data Layer: This layer interacts with data store, external services, etc. CBLists class in this case.

Also, in OO programming class names are singular so CBLists should be changed to CBList.

answered Dec 16, 2013 at 14:24
\$\endgroup\$
2
  • \$\begingroup\$ thank you for your explanation. what about the use of multiple methods for retrieving the dataset? is this the correct way or should I use one generic method? \$\endgroup\$ Commented Dec 16, 2013 at 15:17
  • \$\begingroup\$ I think it depends on requirements, if there are multiple methods required that is okay but if same functionality can be provided using some generic method, it is better. Also, have you looked into Entity Framework, which allows to access data from database in OO form. \$\endgroup\$ Commented Dec 16, 2013 at 17:36
-1
\$\begingroup\$

In general for OO programming you separate out code into objects and each object focuses on doing one thing and doing it well.

Each object should then generally have it's own source file in your code tree.

answered Dec 16, 2013 at 10:50
\$\endgroup\$
1
  • 2
    \$\begingroup\$ That's general advice, but you should be reviewing this specific code. Does the posted code follow that advice? If not, how would you improve it? \$\endgroup\$ Commented Dec 16, 2013 at 11:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.