c# How to validate type of the user?

144 views Asked by At

I have a table tbl_user where it has the columns of tbl_user_username,tbl_user_password,tbl_user_type. What I need is that how can i specifically say if the type associated with the username equals to something open some form. For example if the username= "john" and the john has the type of "Admin" open Admin panel. How can I validate the user type? This how I did so far. Thanks in advance.

private void button1Lg_Click(object sender, EventArgs e)
    {
        bool res = login_check(textBox1U_Name.Text, textBox2U_Password.Text);

        if(res)
        {
            MessageBox.Show("Welcome " + textBox1U_Name.Text);
        }

        else
        {
            MessageBox.Show("Invalid Login");
        }
    }

public bool login_check(string username, string password)
    {
        using (MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.ConnectionString))
        {
            conn.Open();
            string sql = "SELECT tbl_user_username, tbl_user_password, tbl_user_type WHERE tbl_user_username=@username, tbl_user_password=@password AND tbl_user_type=@type";
            MySqlCommand cmd = new MySqlCommand(sql,conn);
            cmd.Parameters.AddWithValue("uname", username);
            cmd.Parameters.AddWithValue("upass", password);
            bool result = cmd.ExecuteReader().HasRows;
            conn.Close();
            return false;
        }
    }
0

There are 0 answers