ListBox is not showing the product with same name with different price

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
29
Programming Experience
Beginner
Hello to everyone,
Please Read Carefully and Answer afterwards.
Here I have an issue with C# WinForms ListBox, ListBox not showing the Same name Medicine with different details.
The Products are added to the database successfully. In which I have two medicines with the same name but are with different details,
Lets say Medicine 1 Name: Disprin = Rs.20 & Medicine 2 Name: Disprin = Rs. 50 and there are also other medicines with different names and data
The ListBox is fetching all the medicines successfully but,
Upon adding Medicines to the ListBox. In the list it is not showing the Medicine 2 with medicine 2 details but it is only showing the medicine 1 as Medicine 2 and all other Medicines with different names are fetched correctly.

Below is a code to add Medicines in ListBox:

Code adding Medicines in the ListBox:
private void P_SellMedicinesUC_Load(object sender, EventArgs e)
        {
            ListBoxMedicines.Items.Clear();
            string Query = "select Med_Name from Pharmacist_Medicine where E_Date >= getdate() AND E_Date > getdate()+30 AND Stock_Quantity > '0'";
            SqlDataAdapter ListMed = new SqlDataAdapter(Query, Conn.Connect);
            DataTable MedList = new DataTable();
            ListMed.Fill(MedList);
            for (int i = 0; i < MedList.Rows.Count; i++)
            {
                ListBoxMedicines.Items.Add(MedList.Rows[i][0].ToString());
            }

        }

This Query is Selecting the Names of the Medicines are neither Expired nor will Expire in 30 days Means it will add only Valid Medicines and also which are having Stock Quantity > 0.

Please instead answer theoretically try to answer practically with the code. Thanks

2021-10-19.png


Above Image is showing Two Medicines with different ID and Batch no and different price but have Same name.

2021-10-19 (1)_LI.jpg
2021-10-19 (2)_LI.jpg

Above two Images showing the Issue I defined above.
 
Last edited:
Upon adding Medicines to the ListBox. In the list it is not showing the Medicine 2 with medicine 2 details but it is only showing the medicine 1 as Medicine 2 and all other Medicines with different names are fetched correctly.
You only showed us your code for adding items to your listbox. You did not show us your code for how you are populating the controls on the right that shows the details of that selected item in the listbox.

Perhaps you are looking up the details by drug name instead of by ID? We can't tell since you didn't show us. Personally, I am kind of leaning on this theory because you only seem to store the name in the listbox, but not the ID as some kind of metadata. In fact, your query only seems to get the names, and not the names and IDs.
 
Also, your title is saying that the listbox is not showing two products with the same name, but your screenshots are showing that the listbox contains two products named "Digas". The issue is not with the listbox, but rather with how you are showing the details about the items.
 
You really ought to be retrieving all the data in that one query, binding the DataTable to a BindingSource and then binding that to the ListBox and all the other controls as well. You then don't need any extra code to populate the other controls. It will happen automatically when you make a selection in the ListBox.

My main concern when answering questions on sites like this one is that people learn something and you generally learn most by doing. That's the very reason that we tend to point people in a direction and then expect them to make the effort to do their own research based on our advice. I've given you information that you weren't aware of before, so you can now do your own research and learn how to bind data. If you make an attempt and it doesn't work then we can always help further but, if you're not prepared to make that effort, it doesn't encourage us to do more work for you than you're prepared to do for yourself.
 
Last edited:
You only showed us your code for adding items to your listbox. You did not show us your code for how you are populating the controls on the right that shows the details of that selected item in the listbox.

Perhaps you are looking up the details by drug name instead of by ID? We can't tell since you didn't show us. Personally, I am kind of leaning on this theory because you only seem to store the name in the listbox, but not the ID as some kind of metadata. In fact, your query only seems to get the names, and not the names and IDs.
C#:
private void ListBoxMedicines_SelectedIndexChanged(object sender, EventArgs e)
        {
            Txt_NoOfUnits.Clear();
            string Name = ListBoxMedicines.GetItemText(ListBoxMedicines.SelectedItem);
            Txt_MedicineName.Text = Name;
            string Query = "select Med_ID, E_Date, PricePerUnit from Pharmacist_Medicine where Med_Name = '" + Name + "'";

            SqlDataAdapter FetchMed = new SqlDataAdapter(Query, Conn.Connect);
            DataTable MedFetch = new DataTable();
            FetchMed.Fill(MedFetch);
            Txt_MedicineID.Text = MedFetch.Rows[0][0].ToString();
            DTP_ExpireDate.Text = MedFetch.Rows[0][1].ToString();
            Txt_PPU.Text = MedFetch.Rows[0][2].ToString();
            Txt_PPU.Text = String.Format("{0:N2}", double.Parse(Txt_PPU.Text));
        }
 
Also, your title is saying that the listbox is not showing two products with the same name, but your screenshots are showing that the listbox contains two products named "Digas". The issue is not with the listbox, but rather with how you are showing the details about the items.
Yes I checked by changing the Query of ListBox to Select Med_ID instead of Med_Name where it shows the Different ID it means ListBox have saved Items correctly.

The Issue is with the Controls that Even I select Digas with ID 1 or Digas with ID 12 both are showing same details in the controls as of Digas with ID 1, In the last reply I shared the code in which data is showing in controls upon selecting Medicine
 
Yes, and notice that on line 6 of your code in post #6 that your only criteria for getting data from the database is just the medicine name. Your database engine can't read your mind and tell that you want the drug with the same name but the other ID. You need to tell it.

Best solution is use a Binding Source as recommended above. Next best solution is to store metadata in with the list time so that you know which product is which.
 
Back
Top Bottom