简体   繁体   中英

conditional statement in sql error in C# [offTopic]

I am trying to create a login system by using parameterised sql and cross-table query.

This is my code for my login in C#:

private void userloigin() {

    using (MySqlConnection connection = new MySqlConnection("datasource=**;port=***;database=***;username=***;password=***;")) {

        MySqlCommand cmd = new MySqlCommand("SELECT * FROM student, teacher WHERE (student.Username = @userName AND student.Password = @passWord) OR (teacher.username = @teacherUser AND teacher.password = @teacherPass);");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.Parameters.AddWithValue("@userName", textboxUsername.Text);
        cmd.Parameters.AddWithValue("@passWord", texBoxPassword.Text);
        cmd.Parameters.AddWithValue("@teacherUser", textboxUsername.Text);
        cmd.Parameters.AddWithValue("@teacherPass", texBoxPassword.Text);
        connection.Open();
        MySqlDataReader DBReader;
        cmd.Connection = connection;
        DBReader = cmd.ExecuteReader();
        int check = 0;
        while (DBReader.Read()) {
            var teacherLogin = DBReader.GetString("username");
            var teacherPass = DBReader.GetString("password");
            var studentLogin = DBReader.GetString("Username");
            var studentPass = DBReader.GetString("Password");

            if (teacherLogin == textboxUsername.Text && teacherPass == texBoxPassword.Text) {
                MessageBox.Show("Login Successfull!");
                this.Hide();
                Registration ss = new Registration();
                ss.Show();
            } else if (studentLogin == texBoxPassword.Text && studentPass == texBoxPassword.Text) {
                MessageBox.Show(" Login Successfull");

                this.Hide();
                Test ss = new Test();
                ss.Show();
            } else {
                MessageBox.Show("Username or Password Incorrect.  Try Again please");
            }
        }   
    }
}

Since username and password is in BOTH tables 'teacher' and 'student'. The login will allow the student to login BUT not the teacher. Which is understandable. However when I do:

tbl_name.col_name

I get an error saying:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in MySql.Data

Additional information: Could not find specified column in results: teacher.username

 var teacherLogin = DBReader.GetString("teacher.username");
 var teacherPass = DBReader.GetString("teacher.password");
 var studentLogin = DBReader.GetString("student.Username");
 var studentPass = DBReader.GetString("student.Password");

QUESTION:

How can I amend my code so that if the 'teacher' signs in then take the teacher to the registration form.

Same way, if a student signs in then take the student to the test form?

What am I doing wrong?

     using (MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;database=project;username=root;password=Greenford123;"))
        {
            MySqlCommand cmd = new MySqlCommand("SELECT student.studentUsername, student.studentPassword, teacher.teacherUsername, teacher.teacherPassword FROM student, teacher WHERE (student.studentUsername = @userName AND student.studentPassword = @passWord) OR (teacher.teacherUsername = @teacherUser AND teacher.teacherPassword = @teacherPass);");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("@userName", textboxUsername.Text);
            cmd.Parameters.AddWithValue("@passWord", texBoxPassword.Text);
            cmd.Parameters.AddWithValue("@teacherUser", textboxUsername.Text);
            cmd.Parameters.AddWithValue("@teacherPass", texBoxPassword.Text);
            connection.Open();
            MySqlDataReader DBReader;
            cmd.Connection = connection;
            DBReader = cmd.ExecuteReader();

            while (DBReader.Read())
            {
                var teacherLogin = DBReader.GetString("teacherUsername");
                var teacherPass = DBReader.GetString("teacherPassword");
                var studentLogin = DBReader.GetString("studentUsername");
                var studentPass = DBReader.GetString("studentPassword");

                if (teacherLogin == textboxUsername.Text && teacherPass == texBoxPassword.Text)
                {
                    MessageBox.Show("Login Successfull!");

                    this.Hide();
                    Registration ss = new Registration();
                    ss.Show();
                }
                else if (studentLogin == texBoxPassword.Text && studentPass == texBoxPassword.Text)
                {
                    MessageBox.Show(" Login Successfull");

                    this.Hide();
                    Test ss = new Test();
                    ss.Show();
                }
                else
                {
                    MessageBox.Show("Username or Password Incorrect.  Try Again please");
                }

            }

EDIT: its working now BUT it only allows the teacher access to the system. If the student tries to login it will say

wrong username or password. try again please

Error is because of this

else if (studentLogin == texBoxPassword.Text && studentPass == texBoxPassword.Text)

Where studentLogin == texBoxPassword.Text hence the

wrong username or password.

Correct this instead with

else if (studentLogin == textboxUsername.Text && studentPass == texBoxPassword.Text)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM