private void button1_Click(object sender, EventArgs e) { if (check() && DBConnect()) { if (VoterradioButton.Text == "Voter" && VoterradioButton.Checked) { strsql = string.Format("select count (*) from Voter where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon); try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); LoginInfo.CurrentUser.UserName = UserNametextBox.Text; HomePage HomePage = new HomePage(); this.Hide(); HomePage.ShowDialog(this); } else { MessageBox.Show("Login failed"); } } catch { MessageBox.Show("Sql error"); } } else if (AuditorradioButton.Text == "Auditor" && AuditorradioButton.Checked) { strsql = string.Format("select count (*) from Auditor where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon); try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); LoginInfo.CurrentUser.UserName = UserNametextBox.Text; AuditorChoose auditorMenu = new AuditorChoose(); this.Hide(); auditorMenu.ShowDialog(this); } else { MessageBox.Show("Login failed"); } } catch { MessageBox.Show("Sql error"); } } else if (AdminradioButton.Text == "Admin" && AdminradioButton.Checked) { strsql = string.Format("select count (*) from Admin where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon); try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); LoginInfo.CurrentUser.UserName = UserNametextBox.Text; AdminMenu adminmenu = new AdminMenu(); this.Hide(); adminmenu.ShowDialog(this); } else { MessageBox.Show("Login failed"); } } catch { MessageBox.Show("Sql error"); } } } }
private void button1_Click(object sender, EventArgs e) { if (check() && DBConnect()) //check the database connection { if (VoterradioButton.Text == "Voter" && VoterradioButton.Checked) //check the text is correct { strsql = string.Format("select count (*) from Voter where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon);//Specify the SQL statement to execute try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); //if username and password are correct, show successful login in the messagebox LoginInfo.CurrentUser.UserName = UserNametextBox.Text;//check the text correct HomePage HomePage = new HomePage(); this.Hide(); HomePage.ShowDialog(this); //Interface conversion function } else { MessageBox.Show("Login failed");//show results } } catch { MessageBox.Show("Sql error");//show results } } else if (AuditorradioButton.Text == "Auditor" && AuditorradioButton.Checked)//check the text are correct { strsql = string.Format("select count (*) from Auditor where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon); //Specify the SQL statement to execute try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); LoginInfo.CurrentUser.UserName = UserNametextBox.Text; AuditorChoose auditorMenu = new AuditorChoose(); this.Hide(); auditorMenu.ShowDialog(this); } else { MessageBox.Show("Login failed"); } } catch { MessageBox.Show("Sql error"); } } else if (AdminradioButton.Text == "Admin" && AdminradioButton.Checked) { strsql = string.Format("select count (*) from Admin where Name = '{0}' and Password = '******'", UserNametextBox.Text, PasswordBox.Text); command = new SqlCommand(strsql, mycon); //Specify the SQL statement to execute try { int result = Convert.ToInt32(command.ExecuteScalar()); if (result > 0) { MessageBox.Show("successful login"); LoginInfo.CurrentUser.UserName = UserNametextBox.Text; AdminMenu adminmenu = new AdminMenu(); this.Hide(); adminmenu.ShowDialog(this); } else { MessageBox.Show("Login failed"); } } catch { MessageBox.Show("Sql error"); } } } }