In the previous episode, we have successfully created methods to INSET, UPDATE and DELETE in Data Access Layer (DAL) for our Product Module in BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#.
In this episode, we will use these methods to add actual functionality to Add New Product, Update & Delete Existing Products.
WATCH THE VIDEO FIRST
Some to the events and codes to Add these features are as follows:
1. Add Button Click Event to Insert New Product
Here, we will double click on Add Button to create the button click event, and the code inside the Add Button Click Event is as follows
private void btnAdd_Click(object sender, EventArgs e)
{
//Get All the Values from Product Form
p.name = txtName.Text;
p.category = cmbCategory.Text;
p.description = txtDescription.Text;
p.rate = decimal.Parse(txtRate.Text);
p.qty = 0;
p.added_date = DateTime.Now;
//Getting username of logged in user
String loggedUsr = frmLogin.loggedIn;
userBLL usr = udal.GetIDFromUsername(loggedUsr);
p.added_by = usr.id;
//Create Boolean variable to check if the product is added successfully or not
bool success = pdal.Insert(p);
//if the product is added successfully then the value of success will be true else it will be false
if(success==true)
{
//Product Inserted Successfully
MessageBox.Show("Product Added Successfully");
//Calling the Clear Method
Clear();
//Refreshing DAta Grid View
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
else
{
//Failed to Add New product
MessageBox.Show("Failed to Add New Product");
}
}
2. Form Load Event to Display all the added Products in Data Grid View
After the Product is added, we should be able to see the products when we open the form as well as after the product is added, updated and deleted.So the code to display the products in Data Grid View is as follows
private void frmProducts_Load(object sender, EventArgs e)
{
//Creating DAta Table to hold the categories from Database
DataTable categoriesDT = cdal.Select();
//Specify DataSource for Category ComboBox
cmbCategory.DataSource = categoriesDT;
//Specify Display Member and Value Member for Combobox
cmbCategory.DisplayMember = "title";
cmbCategory.ValueMember = "title";
//Load all the Products in Data Grid View
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
3. Clear Method to clear all the text boxes after Product is Inserted, Updated and Deleted
After the Product is Inserted, Updated or Deleted, we need to clear all the text boxes so that we can add other products.Thus, the code to create this method is as follows
public void Clear()
{
txtID.Text = "";
txtName.Text = "";
txtDescription.Text = "";
txtRate.Text = "";
txtSearch.Text = "";
}
4. Row Header Mouse Click Event to Display data in Text boxes
In order to Update or Delete the Products, first we need to select the product from Data Grid View.So, Row Header Mouse Click Event will help to select the product and display the data in text boxes and the code for this event is as follows
private void dgvProducts_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Create integer variable to know which product was clicked
int rowIndex = e.RowIndex;
//Display the Value on Respective TextBoxes
txtID.Text = dgvProducts.Rows[rowIndex].Cells[0].Value.ToString();
txtName.Text = dgvProducts.Rows[rowIndex].Cells[1].Value.ToString();
cmbCategory.Text = dgvProducts.Rows[rowIndex].Cells[2].Value.ToString();
txtDescription.Text = dgvProducts.Rows[rowIndex].Cells[3].Value.ToString();
txtRate.Text = dgvProducts.Rows[rowIndex].Cells[4].Value.ToString();
}
5. Update Button Click Event To Update the Products
After the product is selected from the Data Grid View, we can modify the values in text boxes and click on Update Button to update the Product.The code for Update Button Click event is as follows
private void btnUpdate_Click(object sender, EventArgs e)
{
//Get the Values from UI or Product Form
p.id = int.Parse(txtID.Text);
p.name = txtName.Text;
p.category = cmbCategory.Text;
p.description = txtDescription.Text;
p.rate = decimal.Parse(txtRate.Text);
p.added_date = DateTime.Now;
//Getting Username of logged in user for added by
String loggedUsr = frmLogin.loggedIn;
userBLL usr = udal.GetIDFromUsername(loggedUsr);
p.added_by = usr.id;
//Create a boolean variable to check if the product is updated or not
bool success = pdal.Update(p);
//If the prouct is updated successfully then the value of success will be true else it will be false
if(success==true)
{
//Product updated Successfully
MessageBox.Show("Product Successfully Updated");
Clear();
//REfresh the Data Grid View
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
else
{
//Failed to Update Product
MessageBox.Show("Failed to Update Product");
}
}
6. Delete Button Click Event To Delete the Selected Product
When the product we want to delete is selected, the product detail will be shown in Textboxes.Then we can click on Delete Button to Delete the Product. The code for Button Delete Event is as follows
private void btnDelete_Click(object sender, EventArgs e)
{
//GEt the ID of the product to be deleted
p.id = int.Parse(txtID.Text);
//Create Bool VAriable to Check if the product is deleted or not
bool success = pdal.Delete(p);
//If prouct is deleted successfully then the value of success will true else it will be false
if(success==true)
{
//Product Successfuly Deleted
MessageBox.Show("Product successfully deleted.");
Clear();
//Refresh DAta Grid View
DataTable dt = pdal.Select();
dgvProducts.DataSource = dt;
}
else
{
//Failed to Delete Product
MessageBox.Show("Failed to Delete Product.");
}
}
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.
Comments