Click here to Skip to main content
Rate this: bad
good
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)
        try
        {
            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)
        {
            MessageBox.Show(err.Message.ToString());
        }
 
    }
 

 
////////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
Comments
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 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2014
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