Hi
i am currently creating a login screen for my project.The logical error with this code is that it does not display the message "invalid password" although i have written a code for it.Can anyone please tell me whats wrong with this code.Thanks in advance

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GOGREEN
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Left = label1.Left - 4;
            if (label1.Left < 0)
            {
                label1.Left = 400;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please enter your user name");
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please enter a relevent password");
            }
            Login l = new Login();
            l.Focus();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=MIRFATH-PC;Initial Catalog=GOGREEN;Integrated Security=True";
            progressBar1.Value = 10;
            try
            {
                con.Open();
                
                progressBar1.Value = 30;
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            SqlCommand com = new SqlCommand("Select * from Login_File where UserName=@una and Password=@p", con);
            com.Parameters.AddWithValue("@una", textBox1.Text);
            com.Parameters.AddWithValue("@p", textBox2.Text);
            SqlDataReader dr = com.ExecuteReader();
            progressBar1.Value = 50;
            try
            {
                while (dr.Read())
                {

                    if (dr.HasRows == true)
                    {
                        progressBar1.Value = 100;
                        MessageBox.Show("Welcome to GOGREEN SALES ORDER PROCESSING SYSTEM! You have sucessfully loged in!");
                        Main_Menu m = new Main_Menu();
                        m.Show();

                    }
                    else
                    {
                        MessageBox.Show("Access Denied", "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    
                }
            }

            catch (Exception)
            {
                if (dr.HasRows == false)
                {
                    MessageBox.Show("Access Denied", "Login Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            con.Close();

            

        }
    }
}

Additional Information
User Name:Mirfath
Password:m123
the above information is retrieved from the database

Recommended Answers

All 6 Replies

Are you referring to the exception code or the else for the dr.read()? Your catch block has an if loop in it which means if the catch block gets triggered it is not guaranteed to display that message.

You don't break out of the function if either of the text boxes are empty. As it is you alert the user that the password or login name is missing but then continue through the rest of the logging in procedure.
Try/catch blocks are expensive to use and you have 2, one of which is for nothing more than opening the connection. Combine the two. And use a finally() block to include the conn.close() if you want keep it down there. That will avoid it getting missed if your code gets more complicated (redirects inside the try block for example - the connection would not get closed in that case).

"invalid password" or "Access denied..." is what you think is not displaying on the screen's message box.?
the problem may be in the Catch block, and dr.HasRows() might contain rows, and that the while loop is throwing an exception, if condition inside catch returns false and there is no else condition to handle it,
the best way you can check it is using the debug option, or write an else condition and run the code.
if both these doesn't work, get back... also let us know where the "invalid password" code is available.

"invalid password" or "Access denied..." is what you think is not displaying on the screen's message box.?
the problem may be in the Catch block, and dr.HasRows() might contain rows, and that the while loop is throwing an exception, if condition inside catch returns false and there is no else condition to handle it,
the best way you can check it is using the debug option, or write an else condition and run the code.
if both these doesn't work, get back... also let us know where the "invalid password" code is available.

oops,sorry considering about the 'invalid password' part,i meant that the access was denied the user had entered either a wrong username and password.

But i still did not get what you two was trying to explain to me! please can u be more clearer thanks!!

When there was line 66 executed there might have been an exception, and if your data row had rows inside, the if condition in the catch block would have returned false, and hence the code inside if condition will not execute, n this might be the reason the message box was not shown...
debug the application, you will get to know what is happening.

thanks i kind of got what your trying to explain.However,even though i comment the catch block and the if block the code is not detecting the invalid username and password.i have added a progress bar and it just gets stuck there! can you please tell me how to detect the invalid username and password.thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.