Angelica Angelica - 3 months ago 7
MySQL Question

Show user info in the next form after Login

I need help, My problem is I want to show the user information in the next form like When I Login in the Starting form :

Starting form

Login Form

This is my code for Logging In :

private void btnOK_Click(object sender, EventArgs e)
{
Boolean exist = false;
MainForm mf = new MainForm();
if (txtUser.Text == "" || txtPass.Text == "")
{
MessageBox.Show("Please Input Logon ID and Password", "ERROR", MessageBoxButtons.RetryCancel);

}

else
{
conn = koneksyon.getConnect();
conn.Open();
cmd = new SqlCommand("select Username,Password from Staff where Username = '" + txtUser.Text + "' AND Password = '" + txtPass.Text + "'", conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
exist = true;

txtUser.Text = dr[0].ToString();
txtPass.Text = dr[1].ToString();

}
if (exist == true)
{
mf.Show();
this.Hide();

}
else
{
MessageBox.Show("INCORRECT Logon ID or password", "INVALID", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUser.Clear();
txtPass.Clear();
txtUser.Focus();
}
conn.Close();
dr.Dispose();
cmd.Dispose();
}
}


Now my Problem is I want to display this when I enter

Username = admin , Password = 123456 it will go to next form and show this

Form2

This is the information when I Login, How can I do this? I dont have any codes, for showing, need help! Thanks

Answer

In MainForm constructor, you must put some variable between Parenthesis. i.e. in the MainForm use this:

public MainForm(string _staffID, string _firstName, string _lastName)
{
     InitializeComponent();

     txtStaffID.Text = _staffID;
     txtFisrtName.Text = _firstName;
     txtLastName.Text = _lastName;
}

Now, when you want show MainForm from another form, use this code:

string StaffID = "Fill Yourself as you want";
string FisrtName = "Fill Yourself as you want";
string LastName = "Fill Yourself as you want";
MainForm mf = new MainForm(StaffID, FisrtName, LastName);
mf.Show();

I hope this is helpful.