473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Increase value of datatable cell by one depending on exception

153 100+
Hi,

I have a datatable being populated in a for each loop and it's working great. I'm now trying to eradicated duplicates, I have set productid as uniques and it throws an exception when another product is added which is already present. In this exception I want to increase qty column of the already present row by 1 ....


set column[6] = column[6] + 1 where productID = exception

To basically increase the qty by 1.

Any ideas on the syntax please?

Regards

Brendan
Aug 1 '09 #1
3 2606
tlhintoq
3,525 Expert 2GB
Personally, I wouldn't handle it that way. Instead of trying to fix after causing an error I prefer to not cause the error. In your loop check to see if the new item exists. If it does exist, don't add it (which you say is the cause of the problem)
Aug 1 '09 #2
brendanmcdonagh
153 100+
Hi, yes I think I mised the obvious.

II have looked at a way to check if it is contained in the datatable but there doesn't seem to be a method "contains".

How would you do it? It must be done in this loop?

Expand|Select|Wrap|Line Numbers
  1. row = dtSearch.NewRow();
  2.                     row["ProductID"] = p.Id;
  3.                     String url = p.ImagePath;
  4.                     row["ImagePath"] = ResolveUrl(url);
  5.  
  6.                     row["ProductName"] = p.Name;
  7.                     row["OfferPromotion"] = p.OfferPromotion;
  8.                     row["OfferValidity"] = p.OfferValidity;
  9.                     row["Price"] = (decimal)p.Price;
  10.                     row["Quantity"] = p.Quantity;
  11.                     row["Rating"] = p.Rating;
  12.                     dtSearch.Rows.Add(row);
Aug 2 '09 #3
MrMancunian
569 Expert 512MB
I'd check my data first, before assigning it to a new row. Put p.id in an Integer and check dtSearch in either a loop or using LINQ (in case of VS2008) to see if the key already exists. If not, create a NewRow(); and assign the values. If it does exist, it's your choice how you handle it :-)

Steven
Aug 3 '09 #4

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

Similar topics

8
by: amber | last post by:
Hello, How can I trigger an event, when a value (cell) in my datagrid changes? TIA, amber
0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
2
by: Robert | last post by:
I'm sure this is a fairly basic question, but I've been looking all over the web for days for suggestions on how to do this. I've got a datagrid that's bound to a dataset on my form. It includes...
2
by: John A Grandy | last post by:
anyone know why this is ? Imports System.Data Imports System.Diagnostics Public Class WebForm12
1
by: Johan Wendelstam | last post by:
Hi I have an DataList item where i use DataBinder.Eval(Container.DataItem, "sent") to fetch data and display it and it works fine. But depending on wheter a value in the datatable that is the...
9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
0
by: Matt | last post by:
I derived my own custom class from the datagrid class. I overrode the ProcessCmdKey Function, like this, to catch the up and down arrow keys: ====== Protected Overrides Function...
3
by: Ryan Liu | last post by:
Can someone give a sample to prevent a row from being deleted in a datatable? I tried e.Row.RejectChanges(); in dt_RowDeleting() but seems does not work. I need verify if there other data...
0
by: Ryan Liu | last post by:
I have a program works fine in .NET 1.1 and just recompiled in .NET 2.0 without any code change. Compiles OK, but there is an exeception when execute it. Then I remove PK, it works all fine...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.