I have connected a sql database to my windows form application in C# visual studio 2012, the database contains one table with three columns for Username, Password and Role.
Inside the table is data for username and password, there is also two user types in the role column which determine the form you will be directed to when logging in depending on whether your role is an admin or client.
picture of the data in my form
I now have the code for the login form so that it can detect whether a user is an admin or client when logging in but the problem is that i have no username and password validation which displays a messagebox detailing when a user has entered incorrect information.
Could someone please adapt my code so that it displays a messagebox showing that the user has entered an incorrect username or password if they have unsuccessfully tried to log in.
Here is my code below
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Data.mdf;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select Role from Login Where UserName='" + textBox1.Text + "' and Password='" + textBox2.Text + "' ",con);
DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
if(dt.Rows.Count == 1)
{
if (dt.Rows.Count == 1)
{
switch (dt.Rows[0]["Role"] as string)
{
case "Admin":
{
this.Hide();
AdminMenu ss = new AdminMenu();
ss.Show();
break;
}
case "Client":
{
this.Hide();
MenuForm mf = new MenuForm();
mf.Show();
break;
}
default:
{
// ... handle unexpected roles here...
break;
}
}
}
}
}
private void Login_Load(object sender, EventArgs e)
{
}
private void Login_FormClosing(object sender, FormClosingEventArgs e)
{
Application.ExitThread();
}
}
}