0

I need to work with username on any other form in the application. I did the following login form but I am not sure how can I send useraname to the others forms. Any tips?

    Thread th;

    public void openMainForm(object obj)
    {
        Application.Run(new MainForm());
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection conLogin = new SqlConnection(conString);
        SqlCommand sqlLogin = new SqlCommand(sqlString,conLogin);
        SqlDataReader dr;

        conLogin.Open();
        
        dr = sqlLogin.ExecuteReader();
        while (dr.Read())
        {
            if (txtUser.Text.Equals(dr[0].ToString()) && txtPassword.Text.Equals(dr[1].ToString()))
            {

                this.Close();
                th = new Thread(openMainForm);
                th.SetApartmentState(ApartmentState.STA);
                th.Start();

            }
            else
            {
                MessageBox.Show("Invalid Data", "Invalid Username/Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Clear();
                txtPassword.Clear();
                txtUsername.Focus();
            }
        }

        conLogin.Dispose();
    }
  • Add a UserModel, and share that between the Forms. – Pedro Rodrigues Jan 18 '22 at 12:35
  • PLEASE use something like [Microsoft Identity](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity). And at least [Entity Framework](https://learn.microsoft.com/en-us/ef/) or such. Else you'll just make a big security vulnerability. – JHBonarius Jan 18 '22 at 12:37
  • Does this answer your question? [Passing data between forms](https://stackoverflow.com/questions/4587952/passing-data-between-forms) – Crowcoder Jan 18 '22 at 12:42
  • You shouldn't create a new form on a different thread, just open a new Form. And as already mentioned, this looks highly insecure. If the app can access the database and the passwords are in plain text then it would be trivial for a smart user to query the database to determine valid logins. – Crowcoder Jan 18 '22 at 12:46

1 Answers1

0

In the case of WinFormApp in your Program.cs Class you can use public Static property to send it in other forms....program.cs is a static class where the Main function begins :

    static class Program
    {
        public static string userName = "";
    }

after logged in :

         if (txtUser.Text.Equals(dr[0].ToString()) && txtPassword.Text.Equals(dr[1].ToString()))
        {

            this.Close();
            th = new Thread(openMainForm);
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
            Program.userName=txtUser.Text;//this make the variable global 
        }

and to use it in another form you should use it like this for Ex :

  private void Form_Load(object sender, EventArgs e)
        {
            lbl_welcome.Text += " " + Program.userName;
          
           
        }
Hady Salah
  • 13
  • 5