In the previous article we have learned about datacontext, the “var” keyword, retrieving data through LINQ, binding retrieved data to controls and conditional retrieval of data. In this part we will be looking at the different groups of controls on the “Add/Delete” tab, looping through retrieved results and the “distinct” method in LINQ.
Controls on Tab “Add/Delete”
This tab contains many controls but most of them have similar functions. The controls here are grouped according to their functionality. The controls are grouped according to those that perform addition of a new product, modification of details of an existing product, deletion of a product, show statistics of the inventory and a listbox control that shows all the products available in the inventory.
Add Product Group Controls
The Controls are grouped together in a GroupBox control named “Add Product”
1. cboCategory_Add – Combobox to hold categories.
2. txtName_Add, txtPrice_Add,txtReorderQuantity_Add,txtQuantity_Add – textbox controls to hold values for the new product.
3. btnAddProduct – A button to execute the LINQ statement to add the new product.
Delete Product Group Controls
The Controls are grouped together in a GroupBox control named “Delete Product”
1. txtID_Delete, txtCategory_Delete, txtQuantity_Delete, txtPrice_Delete – textbox controls to show values for the product to be deleted.
2. btnDeleteProduct – A button to execute the LINQ statement to delete the selected product.
Modify Product Group Controls
The Controls are grouped together in a GroupBox control named “Modify Product”
1. cboCategory_Modufy – Combobox to hold categories.
2. txtID_Modify, txtName_Modify, txtPrice_Modify,txtReorderQuantity_Modify,txtQuantity_Modify – textbox controls to that hold values that are to be modified.
3. btnModifyProduct – A button to execute the LINQ statement to add the new product.
listBox_Products – list box which shows all available products in the inventory and through which you select products which you want to delete or modify.
lblTypesOfProducts, lblTotalProducts and lblReorderProducts – Labels which show the number of unique products, total quantity of all the products and the number of products that require reordering respectively.
Here, we will see more functionality of LINQ like inserting new records, deleting records, modifying records and also some more conditional retrieving of records.
Let’s encapsulate code into functions which we will be using regularly.
Functions:
1. PopulateCategoryDropdownbox()
This function will be used to populate the two Category dropdown boxes (In the Add and Modify Product Groups) that we have in this tab. These dropdown boxes will hold the category of the products available in the inventory. The category value in the Products repeats through the records many times since a lot of products belong to the same category. So here we need to filter out the unique values and add them to the Category dropdown boxes.
private void PopulateCategoryDropdownbox() { var categories = (from c in InventoryDataContext.Products select c.Product_Category).Distinct(); foreach (var category in categories) { cboCategory_Add.Items.Add(category); cboCategory_Modify.Items.Add(category); } } foreach (var category in categories) { cboCategory_Add.Items.Add(category); cboCategory_Modify.Items.Add(category); }
2. PopulateProductsListbox()
We will use this function to populate the products list box which will show the names of all the products available in the inventory. This is a very simple function.
private void PopulateProductsListbox() { var products = from n in InventoryDataContext.Products select n.Product_Name; listBox_Products.Items.Clear(); foreach (var product in products) { listBox_Products.Items.Add(product); } }
Here, we recover only the values from the “Products_Name†column from the Products table using LINQ. We clear the listbox of any previous items(required since we will be deleting and adding new products) and then add the result to the listbox.
3. PopulateProductStatistics()
In this function we will recover different statistics(products for reorder, types of products and total products) about the inventory. Here we will see two more functions in LINQ which are used to count the number of records retrieved and add all the values of a result set.
private void PopulateProductStatistics() { //Products for reorder int ReorderProducts = (from a in InventoryDataContext.Products where a.Product_Quantity <= a.Product_ReorderQuantity select a).Count(); lblReorderProducts.Text = ReorderProducts.ToString(); //Types of products int TypesOfProducts = (from a in InventoryDataContext.Products select a).Count(); lblTypesOfProducts.Text = TypesOfProducts.ToString(); //Total products int TotalProducts = (from a in InventoryDataContext.Products select a.Product_Quantity).Sum(); lblTotalProducts.Text = TotalProducts.ToString(); }
Notice that here instead of the “var” keyword we have used the datatype “int”. This is because here we are sure of the datatype of the result which is a single integer value. In the “Products for reorder” part of the code above we will retrieve the result the LINQ statement into the integer variable ReorderProducts which will hold a single value.
The conditional LINQ statement retrieves those products whose quantity is below or equal to their reorder quantity. We encapsulate the LINQ statement into brackets and add “.Count()” after it. This simply counts the number of records or values retrieved. We simply assign the integer value ReorderProducts to the label lblReorderProducts after converting it to a string value. Simple isn’t it?
//Types of products int TypesOfProducts = (from a in InventoryDataContext.Products select a).Count(); lblTypesOfProducts.Text = TypesOfProducts.ToString(); //Total products int TotalProducts = (from a in InventoryDataContext.Products select a.Product_Quantity).Sum(); lblTotalProducts.Text = TotalProducts.ToString();
In the “Total products” part of the code, we calculate the total quantity of all the products combined. So here we encapsulate the LINQ statement into brackets and add “.Sum()” after it. This adds up all the values of the result set, which in this case it is the quantity of each and every product. We assign the retrieved integer value “TotalProducts” to the label “lblTotalProducts”.
We also code the “SelectedIndexChanged” event of the listbox “listBox_Products” which shows the names of the products, so that when we click on a name, the values corresponding to the selected product show up in the textboxes related to deleting and modifying the product.
private void listBox_Products_SelectedIndexChanged(object sender, EventArgs e) { var product = (from a in InventoryDataContext.Products where a.Product_Name==listBox_Products.SelectedItem.ToString() select a).First(); txtID_Delete.Text = product.Product_ID.ToString(); txtCategory_Delete.Text = product.Product_Category; txtPrice_Delete.Text = product.Product_Price.ToString(); txtQuantity_Delete.Text = product.Product_Quantity.ToString(); txtID_Modify.Text = product.Product_ID.ToString(); txtName_modify.Text = product.Product_Name; cboCategory_Modify.Text = product.Product_Category.ToString(); txtPrice_Modify.Text = product.Product_Price.ToString(); txtQuantity_Modify.Text = product.Product_Quantity.ToString(); txtReorderQuantity_Modify.Text = product.Product_ReorderQuantity.ToString(); }
Now, that these functions are explained let us proceed further. We first call these functions in the form “Load” event. Our form is named “frmMain”.
private void frmMain_Load(object sender, EventArgs e) { PopulateProductsListbox(); PopulateCategoryDropdownbox(); PopulateProductStatistics(); }
I am Samith Jhon a content writer and a Professional Blogger. Certified with 1Z0-805 Exam test questions which is very
popular these days and have a great scope in the field of IT Certification. I always like to take 1Z0-820 Exam test questions to pass the Exam. These kinds of Exam questions could secure your future as well as your job.