Click here to Skip to main content
11,709,844 members (50,293 online)
Rate this: bad
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 12:40pm
ThePhantomUpvoter at 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 at 1-Apr-13 19:07pm
Can you please guide how should i proceed.
ThePhantomUpvoter at 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
0 Sergey Alexandrovich Kryukov 524
1 OriginalGriff 250
2 Peter Leow 229
3 Maciej Los 185
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,037
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web03 | 2.8.150819.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