Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How do I display a error message if login fail for windows form

$
0
0

I have a form with username, password and login button. I have three tables called doctor, nurse and admin.

I want to display error message if login is unsuccessful when button is clicked.

If login is successful, it will pop up a message saying login successful and when you click OK, it will load the loading bar.

Below is my login button code. When nothing is enter on username or password and when the login button is clicked, it will display the error message (Invalid username or password) no problem. If i login with correct username and password and login button is clicked, it will display (login successful) ok no problem here, but after i click OK, the (Invalid username or password) message still comes out. What's the problem?

        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;";
            strCommandtext += "SELECT windowsUsername, windowsPassword from WINDOWSADMIN WHERE windowsUsername=@aname AND windowsPassword=@apwd";
            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);
            cmd.Parameters.AddWithValue("@aname", textUsername.Text);
            cmd.Parameters.AddWithValue("@apwd", 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)
                    {
                        timer1.Enabled = true;
                        return;
                    }                                     
                } 
                reader.NextResult();
                while (reader.Read()) //For Nurse
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        timer2.Enabled = true;
                        return;
                    }
                }

                reader.NextResult();
                while (reader.Read()) //For Admin
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        timer3.Enabled = true;
                        return;
                    }
                }

             
            
                //STEP 5: close connection
                reader.Close();
            }
            catch (SqlException ex)
            {
                
            }
            finally
            {
                MessageBox.Show("Invalid username or password");
                //STEP 5: close connection
                myConnect.Close();
            }
        }


Viewing all articles
Browse latest Browse all 12583

Trending Articles