Developing a simple application using LINQ [Part 3]

Updated on 15-Jan-2012

 

This is an article continued from Developing a simple grocery inventory- Part 2. 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.

Continuing from the previous article, we will now see the controls on the Tab “Add/Delete” of the tab control.

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()

 2. PopulateProductsListbox()

 3. PopulateProductStatistics()(This method is covered in the next part   

    of the series) 

 

PopulateCategoryDropdown() – 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.

As we can see in the above function, by using a LINQ statement we have recovered the values of the Product_Category column from the Products table. The problem here is that the values recovered here will have repeating values while we only want unique values. So here, we encapsulate the LINQ statement into brackets and add “.Distinct()” after it. This is a LINQ function which effectively filters out unique values from the retrieved result set. So now we only have unique values for the category dropdown box. 

Now, we have to bind the retrieved values to the dropdown boxes. For this purpose we use “foreach” statement to traverse through the values. We use the “var” keyword and assign it to “category” which will represent individual values in the “categories” resultset. Apart from that, we assign the values to a dropdown box normally and pass “category” as the value.

 

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.

 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.

 

Please share your comments and feedback below. You can also join us on Facebook , and follow us on Twitter (@devworx)

Connect On :