Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Sir/Madam,

I am uday
In csharp winform application,how i to pass login id to another form,after successfull login?.

My code for login button is like this

C#
if (rowsCount > 0)          // IF USER IS VALID
                      {
                          string userName = txtUserNm.Text;
                          int loggeduserId=Convert.ToInt32(ds.Tables["UserLogin"].Rows[0][0]);

    

                          
                          this.Hide();
                          MDIParent menu = new MDIParent();

                          this.DialogResult = DialogResult.OK;


                      }
                      else
                      {
                          MessageBox.Show("You are not valid user", "alert", MessageBoxButtons.OK, MessageBoxIcon.Error);

                      }



in above code loggeduserId i have to pass other all forms.

how it can achieved? Is their session like concept in c sharp winform?

Thanks in advance.............
Posted

No there is no concept like Session.
You can make a class with public access modifier and in this class use properties to access in other forms...
 
Share this answer
 
No there is no session avaiable in C# but you can use properties to pass information from one form to another.
Also, untidy method is to use static variable.
 
Share this answer
 
v2
simple steps for you::

1.create a property where you want to access the login name
2.create object for the form where you create the property
3.assign value to your property after successfull login
4.there in that page access the property

its simple
 
Share this answer
 
Comments
udusat13 8-Sep-11 8:04am    
Thanks..
But i have use setter getter.

After Successfull login i have set login id.then close the login form.when i try to access that variable again.it shows null value.
This is Pretty Easy enough.

Create a Singleton class like this

C#
public class LoggedInfo
    {
        static LoggedInfo instance = null;

        public string UserId { get; set; }

        public string UserName { get; set; }

        static readonly object padlock = new object();

        LoggedInfo()
        {
        }

        public static LoggedInfo Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new LoggedInfo();
                    }
                    return instance;
                }
            }
        }
    }


You might set this property from your login form like this

C#
LoggedInfo li = LoggedInfo.Instance;
li.UserId = this.textBox1.Text;


And from any form in your project you might get the value of this property like this,

C#
string userId=LoggedInfo.Instance.UserId;
 
Share this answer
 
Comments
Md. Rashim Uddin 9-Sep-11 1:37am    
Please try with that. I was doing that for you. Here we are trying a lot to help the developer community and hence need the support of you people.
Simply, you can create a public/protect static property on to your MDIParent form and then access from any form. Thanks
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900