Hi, I am trying to do a three tier architecture of windows application using C#.

Main layout: Inserting Values into a datagridview from two textboxes.

Problem : I have 3 layers in my application UI,Business,Data Layers.(form1.cs,Bal.cs,Dal.cs)

I have created separate c# code files for each, the question is how can I use the textbox or any other controls from form1.cs to dal.cs is there any way doing that.

I am learning how to work with tiered architecture please provide me some help.

Thank you.

Rather than go into great detail in providing the answer here I'll let others who've provided the answer in the past do my talking for me :twisted:

Below is a code snippet (recommended read for this topic) and a couple of related threads that may help you out:

Snippet (Thanks Ryshad)
Link containing links to other threads that will help

Hope these help solve your issue :) Please remember to mark your thread solved once the issue is resolved. Also, if these don't solve your issue, please provide more info as to what the problem is so we can be more specific with our help.

EDIT: Passing info between layers is similar to passing between forms. Please note the main part of the above snippet/links being that an 'instance' of each layer needs to exist in order to reference components between them.

Edited 6 Years Ago by Lusiphur: n/a

Hi, and thank you Lusiphur, I have only one form but I have 2 code files other than that of the form itself. My primary intention is to access data from a database using a data layer , later data layer communicates with business layer ..like that it goes on.

If I am creating a code file(c# to make windows forms) I cannot use my textbox item from the form in the code file...please Help...If u r not getting please tell me I can explain..

As Lusipher pointed out, passing data between your layers is similar to passingit between forms. You need to instantiate an instance of each class and access methods in that class.
Each layer should expose public methods that the layer above will use. So your data access layer will have internal methods that retrieve data and an exposed method (public or internal) which returns that data.
Something like:

public class DataAccess
{
    private string Data;
    
    //private method to get data from current data source
    private void RetreiveData()
    {
        Data = "Some Data";
    }
 
    //exposed method to pass data out to next layer
    public string GetData()
    {
        //use internal method to retreive the data
        RetreiveData();

        //return the data to the calling object
        return Data;
    }
}

public class BusinessLayer
{
    public void GetData()
    {
        //create instance of DataAccess class
        DataAccess dataObject = new DataAccess();

        //call GetData method of current DataAccess instance
        //returned data is stored in local variable
        string retreivedData = dataObject.GetData();
    }
}

The beauty of an n-tier design is that it is modular. My very simple example sets the value of its data object in code; i could rewrite the RetreiveData method to get the data from a file or the internet and as long as the data was in the same format the BusinessLayer would not need to be altered in any way.

Hope this helps to highlight some of the inter-class communication you'll need

Thank You Ryshad Can you Explain that with a textbox it will be more clear for me.

Do you mean that you aren't sure how to display the data on your presentation layer?
Your presentation layer will create an isntance of the BusinessLayer the same way that the BusinessLayer created an instance of the DataAccess layer. Your BusinessLayer should expose a public method which returns the value you wish to display on the presentation layer. Your BusinessLayer should not have any knoweldge of what the PresentationLayer does with the data, if you put any reference to your TextBox in your BusinessLayer then you have broken the encapsulation that makes n-tier work.

In my example, the BusinessLayer retrieves the data from the DataAccess layer and stores it in a variable called retreivedData. At that stage you can do whatever you like with the data; in the BusinessLayer you would apply your business logic to it, in the presentation layer you would display it. If you don't know how to display a string in a textbox then i think you may be trying to run before you can walk.

Edited 6 Years Ago by Geekitygeek: n/a

Hi all Now I understand that there is no Direct flow between UI and data Access layer.

Thank You all For supporting me.

Take care

Hi all Now I understand that there is no Direct flow between UI and data Access layer.

Thank You all For supporting me.

Take care

Sorry, I may not have explained myself very clearly. Let me try again.
There is data flow between every layer in your n-tier system, thats how it works. The data is passed up and down the layers like a chain. What you don't want is one layer directly affecting another layer...in OO it is bad design to have any class directly manipulating another class. The correct way is to pass data back and forth via exposed methods and properties and in an n-tier system, each level can generally only talk to the level directly above and below it.

If it helps, try to think of it as a system of message pipes; The UI doesn't know what happens inside the business logic layer and it has no knowledge of the data access layer at all. All the UI sees is the message pipes between it and the business logic layer. It knows what it can send down them and what to expect back in return. These are the exposed public methods.

So if your UI thread wants to display something in its textbox, first it gets an instance of your business layer and asks the business layer for the data. The business layer in turn gets an instance of your data access layer and asks it for the data it needs. The data access layer sends the data back to the business layer, which then performs any logic needed on it before in turn passing it back to the UI.
All the UI sees is the message it sends out saying "I want this data" and the data coming back from the Logic Layer. Likewise, the logic layer only sees a request for data and sends the data back, it has no knowledge of what the UI does with the data once it gets it.

Comments
Great example! Y'know... what I SHOULD have said but was too tired/lazy to type :P
Finally I Did It A three Tier Architecture: Implemented Insert Command

Data access Layer:

public class DAL

{

    public DAL()

    {      

       // constructor for data access layer                   

    }

    public int InsertData(int tab_id,string tab_name)

    {

        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\rahulv\\My Documents\\db5.mdb;Persist Security Info=False");

        conn.Open();

        OleDbCommand cmd = conn.CreateCommand();

        cmd.CommandText = "INSERT INTO tab(tab_id,tab_name)Values(@id,@name)";

        try

        {

            cmd.Parameters.AddWithValue("@tab_id", tab_id);

            cmd.Parameters.AddWithValue("@tab_name", tab_name);

            return cmd.ExecuteNonQuery();

        }

        catch

        {

            throw;

        }

        finally

        {

            cmd.Dispose();

            conn.Close();

            conn.Dispose();

        }

    }

 

}  

Business Layer:
public class BAL
{
    public BAL()
    {
        // constructor for business layer

    }
    public int InsertData(int tab_id, string tab_name)
    {
        DAL dal = new DAL();
        try
        {
            return dal.InsertData(tab_id, tab_name);
        }
        catch
        {
            throw;
        }
        finally
        {
            dal = null;
        }



    }

}       

User Interface:
namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        OleDbCommandBuilder bder;
        OleDbConnection conn;
        OleDbCommand cmd;
        OleDbDataAdapter da;
        DataTable dt;
        DataSet ds;
        string qry;
        DataRow dr;

        public Form1()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            addrecords(sender,e);
            textBox1.Text = "";
            textBox2.Text = "";
            populate(sender, e);
        }
        protected void addrecords(object sender, EventArgs e)
        {
            int res=0;
            BAL bal=new BAL();
            int tab_id=int.Parse(textBox1.Text);
            string tab_name=textBox2.Text;
            try
            {
                res=bal.InsertData(tab_id,tab_name);
            
            if(res>0)
            {
                MessageBox.Show("Inserted");
            }
            else
            {
                MessageBox.Show("Duplicates");
            }
            }
            catch(Exception ee)
            {
                MessageBox.Show("ID Should not be Similar");
            }
            finally
            {
                bal=null;
            
           }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            populate(sender, e);

            
        }
        protected void populate(object sender, EventArgs e)
        {
            conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\rahulv\\My Documents\\db5.mdb;Persist Security Info=False");
            da = new OleDbDataAdapter();
            qry = "SELECT * FROM tab";
            cmd = new OleDbCommand(qry, conn);
            da.SelectCommand = cmd;
            dt = new DataTable();
            conn.Open();
            da.Fill(dt);
            conn.Close();
            this.dataGridView1.DataSource = dt;
        }
    }
    }
This question has already been answered. Start a new discussion instead.