The button I have checks what is in the two username and password texts boxes in the table username and password columns and if they are both present it signs them in. I can't seem to figure how to set the TeacherID to a variable or let alone find out what it is so I can tell what user has signed in.
(I KNOW MY CODE IS VULNERABLE TO SQL INJECTION I WILL DEAL WITH IT LATER)
I would very much appreciate any help you could give me!
Here is my attempt to setting the TEACHER ID to an integer called x:
private void TeacherLoginButton_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
int x = 0;
SqlCommand command10 = new SqlCommand(@"SELECT [TeacherID], [Username], [Password] FROM TeacherDetails WHERE ( @x = [TeacherID] AND [Username]='" + this.usernameTlogin.Text + "' AND [Password]= '" + this.passwordTlogin.Text +"');", connect);
command10.Parameters.AddWithValue(@"x", x );
SqlDataReader reader;
reader = command10.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if( count == 1)
{
MessageBox.Show("Usename and password is correct" + x.ToString());
this.Hide();
TeacherDashboardForm TeacherDashboard = new TeacherDashboardForm();
TeacherDashboard.Show();
}
else if (count > 1)
{
MessageBox.Show("BEEP BOOP ERROR");
}
else
{
MessageBox.Show("Username or password is incorrect");
}
This is my code before I attempted to find what the ID was:
private void TeacherLoginButton_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
SqlConnection connect = new SqlConnection(connectionString);
connect.Open();
SqlCommand command10 = new SqlCommand("SELECT [Username], [Password] FROM TeacherDetails WHERE ([Username]='" + this.usernameTlogin.Text + "' AND [Password]= '" + this.passwordTlogin.Text +"');", connect);
SqlDataReader reader;
reader = command10.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if( count == 1)
{
MessageBox.Show("Username and password is correct");
this.Hide();
TeacherDashboardForm TeacherDashboard = new TeacherDashboardForm();
TeacherDashboard.Show();
}
else if (count > 1)
{
MessageBox.Show("BEEP BOOP ERROR");
}
else
{
MessageBox.Show("Username or password is incorrect");
}
Here is my table:
CREATE TABLE [dbo].[TeacherDetails] (
[TeacherID] INT IDENTITY (1, 1) NOT NULL,
[First Name] NVARCHAR (50) NULL,
[Last Name] NVARCHAR (50) NULL,
[Title] NVARCHAR (50) NULL,
[Username] NVARCHAR (50) NULL,
[Password] NVARCHAR (50) NULL,
[Email Address] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([TeacherID] ASC)
);