Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Guys! i am working on entity framework database in my c# application but some thing here is so difficult like i click on image then product name and product price add in listbox but when i again add same item so item will add no issue with it price is not update in same product name
Here is the code:
C#
Button b = (Button)sender;
                    tblProduct tp = (tblProduct)b.Tag;
                    string product = tp.productName;
                    lbProductsToBuy.Text = tp.ToString();
                    if (lbProductsToBuy.Items.Contains(tp))
                    {
                        MessageBox.Show("Items is already in listbox", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                        lbProductsToBuy.Items.Add(tp.productPrice);
                    }
                    else
                    {

                    }
                    {
                        products.Add(tp);
                        total += (decimal)tp.productPrice;


What I have tried:

Button b = (Button)sender;
tblProduct tp = (tblProduct)b.Tag;
string product = tp.productName;
lbProductsToBuy.Text = tp.ToString();
if (lbProductsToBuy.Items.Contains(tp))
{
MessageBox.Show("Items is already in listbox", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
lbProductsToBuy.Items.Add(tp.productPrice);
}
else
{

}
{
products.Add(tp);
total += (decimal)tp.productPrice;
Posted
Updated 24-Aug-16 2:56am

1 solution

You are inserting a other one than you check if it already exists.

C#
lbProductsToBuy.Items.Add(tp.productPrice); // Here you are inserting the price 



C#
if (lbProductsToBuy.Items.Contains(tp)) // There you are checking for item and noth its price


C#
if (lbProductsToBuy.Items.Contains(tp.productPrice)) //Solution check if contains price
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900