Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

15. Adding Insert, Update and Delete Functionality in Category Module



In the previous episode, we have successfully created methods to Select, Insert, Update and Delete in our Data Access Layer (DAL) for our "BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#".

In this episode, we will add the actual functionality to ADD, UPDATE and DELETE in our Category Module.

The following events are created to add these functionalities.

 WATCH THE VIDEO FIRST


1. Form Load Event to Display all categories when form is opened

    private void frmCategories_Load(object sender, EventArgs e)
        {
            //Here write the code to display all the categries in DAta Grid View
            DataTable dt = dal.Select();
            dgvCategories.DataSource = dt;
        }

2. Method to Clear all the text boxes


    public void Clear()
        {
            txtCategoryID.Text = "";
            txtTitle.Text = "";
            txtDescription.Text = "";
            txtSearch.Text = "";
        }

3. Function to Add new Category

    private void btnADD_Click(object sender, EventArgs e)
        {
            //Get the values from Categroy Form
            c.title = txtTitle.Text;
            c.description = txtDescription.Text;
            c.added_date = DateTime.Now;

            //Getting ID in Added by field
            string loggedUser = frmLogin.loggedIn;
            userBLL usr = udal.GetIDFromUsername(loggedUser);
            //Passign the id of Logged in User in added by field
            c.added_by = usr.id;

            //Creating Boolean Method To insert data into database
            bool success = dal.Insert(c);

            //If the category is inserted successfully then the value of the success will be true else it will be false
            if(success==true)
            {
                //NewCAtegory Inserted Successfully
                MessageBox.Show("New Category Inserted Successfully.");
                Clear();
                //Refresh Data Grid View
                DataTable dt = dal.Select();
                dgvCategories.DataSource = dt;
            }
            else
            {
                //FAiled to Insert New Category
                MessageBox.Show("Failed to Insert New CAtegory.");
            }
        }

4. Displaying data into from from Data Grid View

    private void dgvCategories_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //Finding the Row Index of the Row Clicked on Data Grid View
            int RowIndex = e.RowIndex;
            txtCategoryID.Text = dgvCategories.Rows[RowIndex].Cells[0].Value.ToString();
            txtTitle.Text = dgvCategories.Rows[RowIndex].Cells[1].Value.ToString();
            txtDescription.Text = dgvCategories.Rows[RowIndex].Cells[2].Value.ToString();
        }

5. Function to Update Existing Category

    private void btnUpdate_Click(object sender, EventArgs e)
        {
            //Get the Values from the CAtegory form
            c.id = int.Parse(txtCategoryID.Text);
            c.title = txtTitle.Text;
            c.description = txtDescription.Text;
            c.added_date = DateTime.Now;
            //Getting ID in Added by field
            string loggedUser = frmLogin.loggedIn;
            userBLL usr = udal.GetIDFromUsername(loggedUser);
            //Passign the id of Logged in User in added by field
            c.added_by = usr.id;

            //Creating Boolean variable to update categories and check
            bool success = dal.Update(c);
            //If the cateory is updated successfully then the value of success will be true else it will be false
            if(success==true)
            {
                //CAtegory updated Successfully
                MessageBox.Show("Category Updated Successfully");
                Clear();
                //Refresh Data Gid View
                DataTable dt = dal.Select();
                dgvCategories.DataSource = dt;
            }
            else
            {
                //FAiled to Update Category
                MessageBox.Show("Failed to Update Category");
            }
        }

6. Function to Delete Existing Category

    private void btnDelete_Click(object sender, EventArgs e)
        {
            //Get te ID of the Category Which we want to Delete
            c.id = int.Parse(txtCategoryID.Text);

            //Creating Boolean Variable to Delete The CAtegory
            bool success = dal.Delete(c);

            //If the CAtegory id Deleted Successfully then the vaue of success will be true else it will be false
            if(success==true)
            {
                //Category Deleted Successfully
                MessageBox.Show("Category Deleted Successfully");
                Clear();
                //REfreshing DAta Grid View
                DataTable dt = dal.Select();
                dgvCategories.DataSource = dt;
            }
            else
            {
                //FAiled to Delete CAtegory
                MessageBox.Show("Failed to Delete CAtegory");
            }
        }



Thank you so much for reading and watching my tutorial. If you want to see more posts like this, then don't forget to SUBSCRIBE.

If you want to START from the beginning of this course, then CLICK HERE.

BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#

Comments

  1. System.invalidoperationexception in system.data.dll ... additional information-- the connection string property has not been initialized ... please help how to solve thik problem

    ReplyDelete