Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

10. Adding Login Functionality in Billing System



In the previous episode, we have successfully designed modern, flat and customized login form for our Billing and Inventory Management system.

In this episode, we will add the actual functionality to validate login based on username, password and user type. After successful login we will display respective Admin Dashboard or User Dashboard based on user type.

So, in order to add this login module in our "BILLING AND INVENTORY MANAGEMENT SYSTEM",we need to create Business Logic Layer (BLL), Data Access Layer (DAL) and button click event for login button click.

Let's complete this tutorial step by step along with the code used

WATCH THE VIDEO FIRST

1. Creating Business Logic Layer (BLL)

Since, we are building the whole application in three-tier architecture, the first step is to create Business Logic Layer. So, we will create loginBLL.cs for our Billing and Inventory Management System which will contain getter setter for username, password and user type.
Please add the following code in loginBLL.cs

        public string username { get; set; }
        public string password { get; set; }
        public string user_type { get; set; }

2. Creating Data Access Layer (DAL)

This is the second step of three-tier architecture of software development, where we create Data Access Layer to contact with database.
For our system we will create loginDAL.cs and create a method to check login. Please add the following code in loginDAL.cs to create login check method

//Static String to Connect Database
        static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

        public bool loginCheck(loginBLL l)
        {
            //Create a boolean variable and set its value to false and return it
            bool isSuccess = false;

            //Connecting To DAtabase
            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                //SQL Query to check login
                string sql = "SELECT * FROM tbl_users WHERE username=@username AND password=@password AND user_type=@user_type";

                //Creating SQL Command to pass value
                SqlCommand cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@username", l.username);
                cmd.Parameters.AddWithValue("@password", l.password);
                cmd.Parameters.AddWithValue("@user_type", l.user_type);

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);

                DataTable dt = new DataTable();

                adapter.Fill(dt);

                //Checking The rows in DataTable
                if(dt.Rows.Count>0)
                {
                    //Login Sucessful
                    isSuccess = true;
                }
                else
                {
                    //Login Failed
                    isSuccess = false;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return isSuccess;
        }

3. Creating Button Click Event

After creating BLL, DAL and UI, we need to create button click event to perform certain functionality.
In our application we will create a button click event to check login. For this, double click on login button of our login form. Then, btnlogin Button Click event will be created.
Now, paste the following code inside the Button Click Event.

            l.username = txtUsername.Text.Trim();
            l.password = txtPassword.Text.Trim();
            l.user_type = cmbUserType.Text.Trim();

            //Checking the login credentials
            bool sucess = dal.loginCheck(l);
            if(sucess==true)
            {
                //Login Successfull
                MessageBox.Show("Login Successful.");
                loggedIn = l.username;
                //Need to open Respective Forms based on User Type
                switch(l.user_type)
                {
                    case "Admin":
                        {
                            //Display Admin Dashboard
                            frmAdminDashboard admin = new frmAdminDashboard();
                            admin.Show();
                            this.Hide();
                        }
                        break;

                    case "User":
                        {
                            //Display User Dashboard
                            frmUserDashboard user = new frmUserDashboard();
                            user.Show();
                            this.Hide();
                        }
                        break;

                    default:
                        {
                            //Display an error message
                            MessageBox.Show("Invalid User Type.");
                        }
                        break;
                }
            }
            else
            {
                //login Failed
                MessageBox.Show("Login Failed. Try Again");
            }


In the next episode, we will DISPLAY THE USERNAME of LOGGED IN USER and also validate the added_by column in our User Module where we will insert the id of logged in user.

Thank you so much for reading this post.
If you are interested to join this course please click on the link below



And don't forget to SUBSCRIBE   

Comments

  1. Sir I am Following your Course in Youtube After Watching 10th Video ..An Error Occurs So Can You Please Help Me Out from the Error (LoginDAL dal = new LoginDAL();)(System.TypeInitializationExeption)

    ReplyDelete