Reputation: 31
How do i make it if (count == 1) for it to do the same stuff it does at the moment, but instead of hiding, to close and open the other form (e.x form2)?
I've tried this.Close(); but it ended up closing the program before the code for opening the new form would run.
private void button1_Click(object sender, EventArgs e)
{
try
{
string myConnection = "datasource=localhost;port=3306;username=dolfin;password=quack";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from database1.logins where username='" + this.username_txt.Text + "' and password='" + this.password_txt.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Username and password is correct.");
this.Hide();
Form2 f2= new Form2();
f2.ShowDialog();
}
else if (count > 1)
{
MessageBox.Show("Duplicate Username and Password... Access Denied!");
}
else
MessageBox.Show("Username and password is not correct... please try again.");
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Upvotes: 0
Views: 583
Reputation: 203816
You should be making a change like this in your program.cs
file, because currently your first form is your applications main form; it cannot be closed without ending the application, but that's easy enough to change.
Instead of only creating one form and using that as the entire applications main form, create your login form, start a message loop for it, when it ends (namely, when the form is closed) you can look at a property that you have set for it (either the one I used, or a custom property) and based on that result, you can conditionally create a new form and start a whole new message loop for that form, where that second form is now the new main form:
Form1 first = new Form1();
Application.Run(first);
if (first.DialogResult == DialogResult.Yes)
{
Form2 second = new Form2();
Application.Run(second);
}
Upvotes: 2