3-tier

3tier Architecture for DatagridView


I have a DatagridView and i want to populate it with the contents of a database. I know it can be done through DataAdapter, dataset and Fill/Update commands and all. But what I want to know is, how to write it in a 3tier architecture. I mean, what will be the commands in the Presentation layer, Business layer and Data layer. I am new born baby for 3tier architecturre. And not able to get it right.Thanks.


Solution

  • After googling it for a while and implementing some of my techniques, I came upto this:

    UILayer:

    private void FillData(object sender, EventArgs e)
    {
       BusinessObject bo = new BusinessObject();
       Datatable dt = new Datatable();
       dt = bo.getTable();
       datagridview.DataSource = dt;
    }
    

    BusinessLayer:

    public DataTable getTable()
    {
       DataLayer dl = new DataLayer();
       DataTable dt = new DataTable();
       dt = dl.getTable();
    
       if(dt == null || dt.HasErrors == true)
       {
          MessageBox.Show("Datable has Errors or is Null");
          return
       }
       return dt;
    }
    

    DataLayer:

    public DataTable getTable()
    {
       SqlConnection con = new SqlConnection(connectionString);
       string myCommand = "Select empId, empDesignation from Employees";
       con.Open();
       SqlDataAdapter sda = new SqlDataAdapter(myCommand, con);
       DataTable dt = new DataTable();
       sda.Fill(dt);
       return dt;
    }
    

    Hope it helps.