Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

7. Adding CRUD (Create, Read, Update and Delete) Functionality in User M...



In the previous episode, we have successfully created Data Access Layer (DAL) for our User Module which contains methods to create new user, display all the users, update existing user and delete the user.

In this episode, we will add an actual functionality to add, update and delete users in our User Module, using the Business Logic Layer (BLL) and Data Access Layer (DAL) we created in previous episode.

If your are new and interested to build Billing and Inventory Management System for any type of store then, CLICK HERE to join.

Let's add the CRUD functionality in our User Module

PLEASE WATCH THE VIDEO FIRST

1. Creating Feature to Add New User

In order to add this feature, we need to add a button click event. So, follow the following steps
  • Go to the Users Form (i.e. frmUsers.cs)
  • Then double click on Add Button we created earlier in episode 4.
  • Then your add button will be created. Now paste the following code inside the event

//Gettting Data FRom UI

            u.first_name = txtFirstName.Text;

            u.last_name = txtLastName.Text;

            u.email = txtEmail.Text;

            u.username = txtUsername.Text;

            u.password = txtPassword.Text;

            u.contact = txtContact.Text;

            u.address = txtAddress.Text;

            u.gender = cmbGender.Text;
            u.user_type = cmbUserType.Text;
            u.added_date = DateTime.Now;
            u.added_by = 1;

            //Inserting Data into DAtabase
            bool success = dal.Insert(u);
            //If the data is successfully inserted then the value of success will be true else it will be false
            if(success==true)
            {
                //Data Successfully Inserted
                MessageBox.Show("User successfully created.");
                clear();
            }
            else
            {
                //Failed to insert data
                MessageBox.Show("Failed to add new user");
            }
            //Refreshing Data Grid View
            DataTable dt = dal.Select();
            dgvUsers.DataSource = dt;

2. Creating Feature to Display all the Users in Data Grid View

After successfully adding the feature to add new user, we need to display the user in data grid view. For this follow the following steps
  • First Go to the user form
  • Then select the frmUsers.cs and go to properties
  • Go to Event Tab
  • Then Double click on Form Load Event
  • Now, you have created the form load event for frmUsers.cs. Now, paste the following code inside that event
            DataTable dt = dal.Select();
            dgvUsers.DataSource = dt;


3. Creating Feature to Display Selected User on Form 

In order to display selected user, we need to create Row Header Mouse Click Event. In order to create that event, please follow the following steps
  • Go to the users Form and select Data Grid View.
  • Then go to the properties and select event tab.
  • Then look for Row Header Mouse Click Event and Double Click.
  • You have successfully create row header mouse click. Now, add the following code on that event
           //Get the index of particular row
            int rowIndex = e.RowIndex;
            txtUserID.Text = dgvUsers.Rows[rowIndex].Cells[0].Value.ToString();
            txtFirstName.Text = dgvUsers.Rows[rowIndex].Cells[1].Value.ToString();
            txtLastName.Text = dgvUsers.Rows[rowIndex].Cells[2].Value.ToString();
            txtEmail.Text = dgvUsers.Rows[rowIndex].Cells[3].Value.ToString();
            txtUsername.Text = dgvUsers.Rows[rowIndex].Cells[4].Value.ToString();
            txtPassword.Text = dgvUsers.Rows[rowIndex].Cells[5].Value.ToString();
            txtContact.Text = dgvUsers.Rows[rowIndex].Cells[6].Value.ToString();
            txtAddress.Text = dgvUsers.Rows[rowIndex].Cells[7].Value.ToString();
            cmbGender.Text = dgvUsers.Rows[rowIndex].Cells[8].Value.ToString();
            cmbUserType.Text = dgvUsers.Rows[rowIndex].Cells[9].Value.ToString();


4. Creating Feature To Update Selected User

In previous episode we have successfully displayed the info of the selected user on respective places in our User form. Now we have to create Button Click Event to update the selected user. Please follow the following steps to update the user
  • Go to the User Form and Double Click on Update Button.
  • Then btnUpdateButtonClick Event will be created.
  • Now paste the following code into the event to create and User Update feature on our Usermodule
            //Get the values from User UI
            u.id = Convert.ToInt32(txtUserID.Text);
            u.first_name = txtFirstName.Text;
            u.last_name = txtLastName.Text;
            u.email = txtEmail.Text;
            u.username = txtUsername.Text;
            u.password = txtPassword.Text;
            u.contact = txtContact.Text;
            u.address = txtAddress.Text;
            u.gender = cmbGender.Text;
            u.user_type = cmbUserType.Text;
            u.added_date = DateTime.Now;
            u.added_by = 1;

            //Updating Data into database
            bool success = dal.Update(u);
            //if data is updated successfully then the value of success will be true else it will be false
            if(success==true)
            {
                //Data Updated Successfully
                MessageBox.Show("User successfully updated");
                clear();
            }
            else
            {
                //failed to update user
                MessageBox.Show("Failed to update user");
            }
            //Refreshing Data Grid View
            DataTable dt = dal.Select();
            dgvUsers.DataSource = dt;


5. Creating Feature to Delete Selected User

Here, we will add a feature to Delete User from our User Module. In order to delete user, first we need to select the user from data grid view. Then, follow the following steps
  • Double Click on Delete Button.
  • Then, a btnDeleteButtonClick event will be created.
  • Now, paste the following code into the event to delete the user
            //Getting User ID from Form
            u.id = Convert.ToInt32(txtUserID.Text);

            bool success = dal.Delete(u);
            //if data is deleted then the value of success will be true else it will be false
            if(success==true)
            {
                //User Deleted Successfully
                MessageBox.Show("User deleted successfully");
                clear();
            }
            else
            {
                //Failed to Delete User
                MessageBox.Show("Failed to delete user");

            }
            //refreshing Datagrid view
            DataTable dt = dal.Select();
            dgvUsers.DataSource = dt;

6. Method to clear the form

I have created the method to clear the form after the user is successfully added and updated. Please find the code below

private void clear()
        {
            txtUserID.Text = "";
            txtFirstName.Text = "";
            txtLastName.Text = "";
            txtEmail.Text = "";
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtContact.Text = "";
            txtAddress.Text = "";
            cmbGender.Text = "";
            cmbUserType.Text = "";
        }


Thank you so much for reading my post till the end.

If you are new and is interested to join this course please click on the link below

Comments

  1. Hi,
    I tried you're code and in the execution appears this message ( the parameterized query which was not supplied )
    What can I do to correct this

    ReplyDelete
  2. Sir I got error in.... connectionString have not been initialised....plzz do help

    ReplyDelete