validate if item is already exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AllanJohnson
    New Member
    • Apr 2012
    • 7

    validate if item is already exists

    Hello everybody, please assist me if how to validate if the item you want to add in the cart is already exists in listView items? for example I already add CH001 which is the productId of Chocolate strawberry, then I accidentally add again that product Id and I what I want is that it should not be acccepted on my listview again because CH001 is already on my cart, I search many times about it, its been 1 week from now but I am not able to find how is it to be done, I am barely new to c#, so sorry if I am noob, and so much thanks in advance. God Bless to all the helpful people, who are there to help such a noob like me :)
  • RhysW
    New Member
    • Mar 2012
    • 70

    #2
    in psuedo code:

    every time you add an item

    Code:
    string AddedproductID = (the new id)
    
    foreach (item I in cart)
    {
    int loop++;
    if (AddedProductID == I.ID)
    {
    MessageBox.show("Already exists");
    int matches++;
    }
    if(matches ==0 && loop ==cart.count)
    {
    //code to add item to list
    }
    }
    I havent tested this but it Should work, basically it checks to see if the id matches up to one that previously exists, if it does it just writes a messagebox and increments matches, if matches equals 0 and the foreach is at its end (when loop equals the amount of items in the cart) it will add this item to the list

    Comment

    • AllanJohnson
      New Member
      • Apr 2012
      • 7

      #3
      thanks for the quick reply. I've tried it, but it seems not working or it's me that didn't know how to revise it well, maybe I lack something. I forgot to mention that I am using textbox that generated values from my database, following is my code for adding items in listview

      Code:
      listView1.View = View.Details;
      
                  ListViewItem item2 = new ListViewItem(txtProductName.Text);
                  item2.Checked = true;
                  item2.SubItems.Add(txtProductPrice.Text);
                  item2.SubItems.Add(txtProductID.Text);
      
                  this.listView1.Items.AddRange(new ListViewItem[] { item2 });
      thanks again. God Bless you.

      Comment

      • RhysW
        New Member
        • Mar 2012
        • 70

        #4
        before you add it to the list run a check to see if txtProductID.Te xt matches with the ID column in the database

        Code:
        public bool CheckExisting()
        {
        foreach(ID_ROW row IN DATABASE_TABLE)
        {
        if (row == tbxProductID.Text)
        {
        return true;
        }
        }
        return false;
        }
        you would have to change the code in the foreach to check the right column in the right table of your database, this returns true if it matches an existing one, and false if it doesnt match any. in the add event you call this method , relying on its answer is if you add it or not, so if this returns false it is safe for you to add it, otherwise you can send a message saying it already exists if it returns true

        Comment

        • AllanJohnson
          New Member
          • Apr 2012
          • 7

          #5
          it gives error on this line
          Code:
          foreach(ID_ROW row IN DATABASE_TABLE)
          I changed the id_row to ProductID and DATABASE_TABLE to Products but it gives me error, by the way I am using visual studio 2010. And this code works well
          Code:
          listView1.View = View.Details;
           
                      ListViewItem item2 = new ListViewItem(txtProductName.Text);
                      item2.Checked = true;
                      item2.SubItems.Add(txtProductPrice.Text);
                      item2.SubItems.Add(txtProductID.Text);
           
                      this.listView1.Items.AddRange(new ListViewItem[] { item2 });
          the only problem is the validation. Is there any simple way? Sorry for being noob. Thanks again for understanding me. God Bless.

          Comment

          • RhysW
            New Member
            • Mar 2012
            • 70

            #6
            no that because the example i gave wasnt in the perfect format, you would need to look up a little bit of code to show you how to get a certain column from each row and put that in there instead, thats just a frame for how it could be done, not the exact template of what to do

            Comment

            Working...