In login form, When I login as Jack which exist in the DOCTOR table, it will go to page_two. I want to pop up a access denied message if nurse button 1 or nurse button 2 is clicked since Jack is not a nurse but a doctor. Then for the opposite, if I login as Mary, which exist in the NURSE table, it will go to page_two. I want to pop up a access denied message when doctor button 1 or doctor button 2 is clicked since Mary is not a doctor but a nurse.
The button names for Page_two is btnDoctor1, btnDoctor2, btnNurse1 and btnNurse2
**//login Form codes**
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace GRP_02_03_SACP
{
public partial class page_one : Form
{
public page_one()
{
InitializeComponent();
}
private void page_one_Load(object sender, EventArgs e)
{
}
private void btnLogin_Click(object sender, EventArgs e)
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
// Add a WHERE Clause to SQL statement
strCommandtext += " WHERE dUsername=@dname AND dPassword=@dpwd;";
strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
cmd.Parameters.AddWithValue("@dname", textUsername.Text);
cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
cmd.Parameters.AddWithValue("@nname", textUsername.Text);
cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);
try
{
// STEP 3: open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: Access Data
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) //For Doctor
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
page_two form = new page_two();
form.Show();
return;
}
}
reader.NextResult();
while (reader.Read()) //For Nurse
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
page_two form = new page_two();
form.Show();
return;
}
}
//STEP 5: close connection
reader.Close();
MessageBox.Show("Invalid username or password");
}
catch (SqlException ex)
{
}
finally
{
//STEP 5: close connection
myConnect.Close();
}
}
}
}
//Page_two codes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GRP_02_03_SACP
{
public partial class page_two : Form
{
public page_two()
{
InitializeComponent();
}
private void btnDoctor1_Click(object sender, EventArgs e)
{
}
private void btnDoctor2_Click(object sender, EventArgs e)
{
}
private void btnNurse1_Click(object sender, EventArgs e)
{
}
private void btnNurse2_Click(object sender, EventArgs e)
{
}
}
}
Add This To your Code :
public Int JobPosition;
Here you will Define the position for each.
Change the Code on your btnLogin_Click
to this :
private void btnLogin_Click(object sender, EventArgs e)
{
//retrieve connection information info from App.config
string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
//STEP 1: Create connection
SqlConnection myConnect = new SqlConnection(strConnectionString);
//STEP 2: Create command
string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
// Add a WHERE Clause to SQL statement
strCommandtext += " WHERE dUsername=@dname AND dPassword=@dpwd;";
strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
cmd.Parameters.AddWithValue("@dname", textUsername.Text);
cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
cmd.Parameters.AddWithValue("@nname", textUsername.Text);
cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);
try
{
// STEP 3: open connection and retrieve data by calling ExecuteReader
myConnect.Open();
// STEP 4: Access Data
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) //For Doctor
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
JobPosition = 1; //Doctor
page_two form = new page_two(JobPosition);
form.Show();
return;
}
}
reader.NextResult();
while (reader.Read()) //For Nurse
{
if (MessageBox.Show("Login Successful") == DialogResult.OK)
{
JobPosition = 2; //Nurse
page_two form = new page_two(JobPosition);
form.Show();
return;
}
}
//STEP 5: close connection
reader.Close();
MessageBox.Show("Invalid username or password");
}
catch (SqlException ex)
{
}
finally
{
//STEP 5: close connection
myConnect.Close();
}
}
On Page_two
Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GRP_02_03_SACP
{
public partial class page_two : Form
{
private Int JopPosition;
public page_two()
{
InitializeComponent();
}
public page_two(Int _Position)
{
InitializeComponent();
JopPosition = _Position;
}
private void btnDoctor1_Click(object sender, EventArgs e)
{
}
private void btnDoctor2_Click(object sender, EventArgs e)
{
}
private void btnNurse1_Click(object sender, EventArgs e)
{
if (JopPosition == 1)
{
MessageBox.Show("access denied");
}
}
private void btnNurse2_Click(object sender, EventArgs e)
{
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.