3
\$\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?

\$\endgroup\$
2
\$\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.

\$\endgroup\$
  • \$\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\$ – user3106823 Dec 16 '13 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\$ – Sajad Deyargaroo Dec 16 '13 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.

\$\endgroup\$
  • 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\$ – svick Dec 16 '13 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.