Click here to Skip to main content
11,932,461 members (53,474 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.5
Hi i am having a problem in accessing values from Business Access Layer. My code is given below...How do i access values from Business access layer from any form. I am getting null in Form1 when i try to access variables from BAL though it is set as public. Please help me. Thanks in advance.

//Form2 Code      
 GetID p = new GetID();
 private void txtpID_KeyDown(object sender, KeyEventArgs e)
        if(e.KeyCode == Keys.Enter)
            String PatID = txtID.Text.ToString();
            SelectCusBAL thisCus = new SelectCusBAL();
            DataSet ds = thisCus.Find(PatID);
            DataRow row;
            row = ds.Tables[0].Rows[0];
            foreach (DataRow rows in ds.Tables[0].Rows)
                p.CusID = rows["dCode"].ToString(); //Passing values to Business Access Layer

        catch (Exception err)

////////Business Access Layer///////////////////////

public class GetID
private string _cusId;
public GetID()
public string CusID
    get { return this._cusId; }
    set { this._cusId = value; }

/////Accessing Business Layer Values into Form1 ////////////
GetID g = new GetID();      //Instantiating Business Layer
txtID.Text = g.CusID.ToString();    //Form1 textbox control
Posted 1-Apr-13 13:40pm
ThePhantomUpvoter 1-Apr-13 18:54pm
To access the same object with the same values in all of your forms, your BAL needs to be a static class or a singleton. Or you could even instantiate the class in one form and pass it around to the other forms. You are probably getting a null value in form1 because you are instantiating a new instance which gives you a new object not an existing instance.
apurba001 1-Apr-13 19:07pm
Can you please guide how should i proceed.
ThePhantomUpvoter 1-Apr-13 19:10pm
Well you need to decide how you want to do it before someone could guide you. The easiest thing for you would probably be to just pass an existing instance between your forms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100