I've tried the methods from

http://stackoverflow.com/questions/11498308/getting-values-from-two-or-more-forms-c-sharp
http://stackoverflow.com/questions/3552169/sharing-a-variable-between-two-winforms
http://stackoverflow.com/questions/10675872/get-value-from-parentform?rq=1
and
http://stackoverflow.com/questions/14504210/passing-username-to-form-but-username-returns-null-c-sharp?rq=1

But i couldnt get mine to work as i will get some random argument error.

the two forms
From the picture , i just want the username on Login form to be put into label1 on User form.

But also be able for other fClick Hereorms to use later as well.

Can someone help with this?

Login Form Code :

public partial class Login : Form
    {
        UserForm _userform = new UserForm();
        Admin _Adminform = new Admin();

        public Login()
        {
            InitializeComponent();
        }

        private void loginscs_Click(object sender, EventArgs e)
        {
            try
            {
                string userNameText = txtUser.Text;
                string passwordText = txtPass.Text;
                string isAdmin = "yes";
                string isNotAdmin = "no";
                if (!(string.IsNullOrEmpty(txtUser.Text)) && !(string.IsNullOrEmpty(txtPass.Text)))
                {
                    SqlConnection SCScon = new SqlConnection();
                    SCScon.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    SqlCommand cmd = new SqlCommand("SELECT ISNULL(SCSID, '') AS SCSID, ISNULL(SCSPass,'') AS SCSPass, ISNULL(isAdmin,'') AS isAdmin FROM SCSID WHERE SCSID='" + txtUser.Text + "' and SCSPass='" + txtPass.Text + "'", SCScon);
                    SCScon.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isAdmin))
                        {
                            MessageBox.Show("Hello " + txtUser.Text, "Admin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            _Adminform.Show();
                            this.Hide();
                        }
                        else if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isNotAdmin))
                        {
                            MessageBox.Show("Welcome " + txtUser.Text, "User");
                            _userform.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Wrong ID/Pass");
                    }
                    SCScon.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("error2" + ex);
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private bool CompareStrings(string string1, string string2)
        {
            return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
        }
    }

Recommended Answers

All 2 Replies

You could save login info etc. in the settings of your application.

Or another way of soing is to create a class say Contants.cs and store your information into that using properties that would implement get and set

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.