473,799 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate if item is already exists

7 New Member
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 :)
Apr 30 '12 #1
5 7614
RhysW
70 New Member
in psuedo code:

every time you add an item

Expand|Select|Wrap|Line Numbers
  1. string AddedproductID = (the new id)
  2.  
  3. foreach (item I in cart)
  4. {
  5. int loop++;
  6. if (AddedProductID == I.ID)
  7. {
  8. MessageBox.show("Already exists");
  9. int matches++;
  10. }
  11. if(matches ==0 && loop ==cart.count)
  12. {
  13. //code to add item to list
  14. }
  15. }
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
Apr 30 '12 #2
AllanJohnson
7 New Member
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

Expand|Select|Wrap|Line Numbers
  1. listView1.View = View.Details;
  2.  
  3.             ListViewItem item2 = new ListViewItem(txtProductName.Text);
  4.             item2.Checked = true;
  5.             item2.SubItems.Add(txtProductPrice.Text);
  6.             item2.SubItems.Add(txtProductID.Text);
  7.  
  8.             this.listView1.Items.AddRange(new ListViewItem[] { item2 });
thanks again. God Bless you.
Apr 30 '12 #3
RhysW
70 New Member
before you add it to the list run a check to see if txtProductID.Te xt matches with the ID column in the database

Expand|Select|Wrap|Line Numbers
  1. public bool CheckExisting()
  2. {
  3. foreach(ID_ROW row IN DATABASE_TABLE)
  4. {
  5. if (row == tbxProductID.Text)
  6. {
  7. return true;
  8. }
  9. }
  10. return false;
  11. }
  12.  
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
Apr 30 '12 #4
AllanJohnson
7 New Member
it gives error on this line
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. listView1.View = View.Details;
  2.  
  3.             ListViewItem item2 = new ListViewItem(txtProductName.Text);
  4.             item2.Checked = true;
  5.             item2.SubItems.Add(txtProductPrice.Text);
  6.             item2.SubItems.Add(txtProductID.Text);
  7.  
  8.             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.
Apr 30 '12 #5
RhysW
70 New Member
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
Apr 30 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
13733
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the error (seems to be happening when I try creating a directory) If dirmgr.Exists("s:\blah\" & txt_name.Text) Then lblerror.Text = lblerror.Text & "Unable to build physical path. " &
9
7263
by: Carl Fenley | last post by:
I am successfully adding stored procedures to an Access database. However, I need to be able to check if the stored procedure of the same name already exists. Is there a way to do this other than waiting for the OleDbException caused when adding one that already exists? Here is the code snippet: Private Sub CreateStoredProcedures()
8
11670
by: bob | last post by:
I am writing some code to create new tables in a SQL database. However, I don't want to try to create a table if it already exists. How can I test beforehand to see if a particular named table already exists in the database, without actually trying to read records into a dataset? Thanks.
3
11049
by: byeung | last post by:
Hi, I am trying to check if a particular record already exists in an Access database through Excel vba code. Through code obtained at another forum, I got the following: *********************************************************************** Sub TheButton()
5
3194
by: Dave | last post by:
I've suprisingly not been able to find examples on this. I'm creating a user account setup page, and the validators work fine on all the other fields. But now I'm creating a customvalidator (for the first time) and it is not doing anything. The page needs to see if a username already exists. Am I doing this right? <asp:textbox TabIndex="1" ID="tLogin" MaxLength="40" runat="server" /> <asp:customvalidator id="Customvalidator1"...
4
13116
gundarap
by: gundarap | last post by:
Hello all, I'm working on minidom. My goal is to see whether an element already exists in the xml file before adding. I was using getElementsByTagName() to check weather the element already exists. The code looks something like this: Value = argv a = dom.getElementsByTagName(Name).childNodes.nodeValue if (a!=Value): add elements. else: return
1
9968
by: dwasler | last post by:
Try every thing I know to remove this alias I know there been other posting I read each one none seem to work. Thank You DLWasler dwasler@yahoo.com OS Window db2 V 8.2.X
0
1345
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I added Microsoft CDO exchange library for 2000 & activex data object 2.5 reference to the project) Dim sURL As String sURL = "http://sbsserver/Exchange/trial/contacts"
2
4681
by: Gaz | last post by:
Im trying to use c# and linq to simplfy searching through a listview but cannot get it to work can anyone help? here is what i got so far which doesnt work. var qry = from L in listviewname.items.all
7
5421
by: sasimca007 | last post by:
Hello friends, What my doubt is, a page is already designed in that page a table exists, and i am doing a script like a radio button exixts and when click that radio button some function is called and in that function i want to create a extra row in that table which already exists on page. That is my problem. How to insert a row dynamically in a table which already exists in the page?
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.