Beispiel #1
0
 public Main(myInfo infoFromLogin)
 {
     InitializeComponent();
     userInfo = infoFromLogin;
     // Example of use
     lblMessage.Text = userInfo.loginMessage;
 }
Beispiel #2
0
        private void BtnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection();

            myConnection.ConnectionString = connectionString;
            try
            {
                // OPEN THE CONNECTION
                myConnection.Open();
                // PERFORM THE COMMAND
                SqlCommand myCommand = new SqlCommand();
                myCommand.CommandText = String.Format(
                    "SELECT * FROM Login WHERE UserId ='{0}' AND Password='******'",
                    txtUsername.Text.Trim().Replace("\"", "").Replace(";", ""),
                    txtPassword.Text.Trim().Replace("\"", "").Replace(";", "")
                    );
                myCommand.Connection = myConnection;

                SqlDataReader myDataReader = myCommand.ExecuteReader();
                if (myDataReader.Read())
                {
                    // Set the data you want to pass
                    myInfo infoFromLogin = new myInfo();
                    infoFromLogin.loginMessage = "Welcome!! You are " + txtUsername.Text;

                    // Pass it on!
                    Main newForm = new Main(infoFromLogin);
                    newForm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Not found!");
                }
            }
            catch (Exception ex)
            {
                // CATCH ANY ERRORS AND DEBUG
                MessageBox.Show(ex.Message);
            }
            finally
            {
                // ALWAYS CLOSE THE CONNECTION
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
            }
        }