how to call value of one form to another form

Hi I am a new bie to c#


i am designing a c# window application.i have a login form. so how can i access thin login name or login id in all other forms. as their is nothing called as session or cache in windows applicaton.

please suggest a suitable solutions.

Waiting for the reply

Thanking you


Answer this question

how to call value of one form to another form

  • Dietz

    if possible can u send me some code so that i can do it properly

    i am not able to do it...

    please help me if u can..


    Thanking you



  • JCJCJC

    well to make life easier, if you are going to be reusing the value on various classes, I would suggest to use a static class and set/get values from here.

    public static class Session

    {

    private static int loginID = 0;

    public static int TheLoginID

    {

    get { loginID = value; }

    set { return loginID; }

    }

    }

    so now from your other classes, all you need to do is this:

    Session.TheLoginID = 1; //you are SETTING the value

    MessageBox.Show(Session.TheLoginID); //you are GETTING the value

    that's all you need to do



  • kenny125

    Very Much Thanks for your help.

    i really appreciate for your help.

    Once Again thanks for your help.


  • Phonics3k

    because you are creating a new instance of a form/class therefore everything is reset to default values. you need the existing instance and pass this instance to the other form via constructor for example, as shown in the link supplied earlier.. I also dont understand why you are creating another instance of your login form when you are already in the login form and doing nothing with it :-)

    Best practice would be to just only pass the values that are needed to be past than a form object itself - the whole process is the same, only difference being you are passing a different object, that's all. So instead of passing a form, pass the login id.

    Take a look at the link and read it carefully :-)



  • Grayson Peddie


    plz can u convert this code to vb.net


  • Nonu_k

    hi this is the form where i am setting a value and below is the code



    private void button1_Click(object sender, System.EventArgs e)
    {
    if(txtLoginName.Text=="" || txtLoginName.Text==null)
    {
    MessageBox.Show("Login Name Cannot be Blank");
    txtLoginName.Focus();
    }
    else if(txtPassword.Text=="" || txtPassword.Text==null)
    {
    MessageBox.Show("Password Cannot be Blank");
    txtPassword.Focus();
    }
    else
    {
    try
    {
    clsdb.OpenConnection();
    string str="select loginid,loginname,loginpassword from tbllogin where loginname='"+txtLoginName.Text+"' and loginpassword='"+txtPassword.Text+"'";
    dr=clsdb.GetReader(str);
    if(!dr.HasRows)
    {
    MessageBox.Show("Invalid UserName or Password!.Please Contact Your Admin");
    txtLoginName.Text="";
    txtPassword.Text="";
    txtLoginName.Focus();
    }
    else
    {
    while(dr.Read())
    {
    login.LoginId=Convert.ToInt32(dr["loginid"].ToString());
    anil=Convert.ToString(login.LoginId);
    login.LoginName=dr["loginname"].ToString();
    }
    mainFrm mainfrm=new mainFrm();
    Login log=new Login();
    mainfrm.Show();
    log.Hide();
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show("Error :"+ex.Message);
    }
    finally
    {
    clsdb.CloseConnection();
    }
    }

    this is the code for the property class

    public class logincls
    {
    private string loginname=string.Empty;
    private string access=string.Empty;
    private int loginid;
    public logincls()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    public string LoginName
    {
    get
    {
    return loginname;
    }
    set
    {
    this.loginname=value;
    }
    }
    public int LoginId
    {
    get
    {
    return loginid;
    }
    set
    {
    this.loginid=value;
    }
    }
    public string Authority
    {
    get
    {
    return access;
    }
    set
    {
    this.access=value;
    }
    }
    .

    Now i want to access the code from the above form to another form...
    might be more than one form
    how can i do it.

    say the other form is vendor and distributor and so on...

    Thanks

  • vtb

    again, everything is in the link supplied. All you need to do is just follow it and replace it with your values :-)

    Please write down here your main form code and your other form code and ill see what I can do. Even better if you can explain where you want to values to go and ill make a sample of it. Which form will have the values Which form will the values go to



  • Andreas Asterlund

    thanks for the reply



    But when i make an property and then when i try to access this property in another file it deosnt give me any value;


    for ex

    on the login form

    i do
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(txtLoginName.Text=="" || txtLoginName.Text==null)
    {
    MessageBox.Show("Login Name Cannot be Blank");
    txtLoginName.Focus();
    }
    else if(txtPassword.Text=="" || txtPassword.Text==null)
    {
    MessageBox.Show("Password Cannot be Blank");
    txtPassword.Focus();
    }
    else
    {
    try
    {
    clsdb.OpenConnection();
    string str="select loginid,loginname,loginpassword from tbllogin where loginname='"+txtLoginName.Text+"' and loginpassword='"+txtPassword.Text+"'";
    dr=clsdb.GetReader(str);
    if(!dr.HasRows)
    {
    MessageBox.Show("Invalid UserName or Password!.Please Contact Your Admin");
    txtLoginName.Text="";
    txtPassword.Text="";
    txtLoginName.Focus();
    }
    else
    {
    while(dr.Read())
    {
    login.LoginId=Convert.ToInt32(dr["loginid"].ToString());
    login.LoginName=dr["loginname"].ToString();
    }
    mainFrm mainfrm=new mainFrm();
    Login log=new Login();
    mainfrm.Show();
    log.Hide();
    }
    }

    and on the form i wan to acc ess the value i do the following thing and it gives the value.

    private void Vendor_Load(object sender, System.EventArgs e)
    {
    MessageBox.Show("Login id is:"+login.LoginId.ToString());
    }

    this gives output as

    Login Id is :0

    why
    please reply ASAP

    Thanking you

  • naddyB

    either make a static class to hold the values so any form can access it, without making an instance of this class since its static, or pass a reference of each form to the other forms and access it via that however making sure that the properties are public so you can access the property you want to access.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=729974&SiteID=1



  • how to call value of one form to another form