Hi,
I was trying to add two quantities at different rows of same description i.e same product_name and same item_pack.1st I want to check the table if it contains the same description or not by comparing with the first record and so on. When found, I'll add up the quantities and then to delete that particular row (to avoid duplicate search). But when I delete the row the particular row, next time it throws an out of index exception.
Below Is the given code. Any suggestions appreciated. -
-
Try
-
-
If con.State = ConnectionState.Closed Then
-
con.Open()
-
End If
-
'Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select p.product_name, p.item_pack,p.product_manufacturer,p.product_description,w.available_qty, w.product_mrp from product_master as p inner join warehouse_master as w on p.product_name=w.product_name and p.item_pack= w.item_pack ", con)
-
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select product_name , item_pack, available_qty from warehouse_master order by serial_no", con)
-
Dim ds As DataSet = New DataSet
-
Dim ds1 As DataSet = New DataSet
-
da.Fill(ds, "Warehouse1")
-
DGV1_Home.DataSource = ds.Tables(0)
-
DGV1_Home.Show()
-
Dim row, row1, rowcnt As Integer
-
'Dim nm, nm1, pack, pack1 As String
-
rowcnt = ds.Tables(0).Rows().Count - 1
-
-
For row = 0 To rowcnt
-
MessageBox.Show("inside 1st for loop" & rowcnt)
-
Dim qty As Integer
-
qty = ds.Tables(0).Rows(row).Item(2)
-
For row1 = 1 To rowcnt
-
MessageBox.Show("inside 2nd for loop" & rowcnt)
-
If ds.Tables(0).Rows(row).Item(0) = ds.Tables(0).Rows(row1).Item(0) And ds.Tables(0).Rows(row).Item(1) = ds.Tables(0).Rows(row1).Item(1) Then
-
MessageBox.Show("iif " & rowcnt)
-
qty += ds.Tables(0).Rows(row1).Item(2)
-
ds.Tables(0).Rows.RemoveAt(row1)
-
rowcnt = ds.Tables(0).Rows.Count - 1
-
-
End If
-
MessageBox.Show(rowcnt)
-
Next
-
MessageBox.Show(ds.Tables(0).Rows(row).Item(0) & qty)
-
rowcnt = ds.Tables(0).Rows.Count - 1
-
MessageBox.Show("out side 2nd loop" & rowcnt)
-
Next
-
MessageBox.Show("out side 1st for loop")
-
'DGV1_Home.DataSource = ds.Tables(0)
-
'DGV1_Home.Show()
-
Catch ex As Exception
-
MessageBox.Show(ex.ToString)
-
Finally
-
con.Close()
-
End Try
-
-
Here Is the data
Sl.No anacin stripe10 10
2 dependal stripe10 25
3 anacin stripe20 18
4 anacin stripe10 24
5 eno satchet 80
6 eno bottle 10
7 axacef stripe20 25
2 7269
Hi GR,
First if you are only trying to summarize, not actually remove the duplicate rows then you can do it easier in SQL.
This query - Select product_name , item_pack, SUM(available_qty) AS QTY from warehouse_master GROUP BY product_name, item_pack order by serial_no"
will give you one row for each unique product name item pack combination and sum them for you.
If you really need to purge them for the database you need to make some changes to your loop. Right now you are saying that rowcnt = total number of rows. When you delete a row then the actual number of rows is less than it was since you set the rowcnt.
For example I have two rows. You first loop say to loop for 0 to 1. You start to process row 0. Your second loop finds a match in row 1 so you remove it. Exit to you first loop which now tries to process row 1. It doesn't know that you have deleted it so you get an error index (row(1) is out of range there is no row here anymore.
I find that unless there is a compelling reason not to I will loop backward through the outer loop - for row = rowcnt to 0 step -1
that way I start with the index that will change when I delete. Then in the inner loop I want to get a new row count as it will have changed from a previous row removal and also go backwards. -
Rowcnt2 = ds.table(0).rows.count-1
-
For row1 = rowcnt2 to 0 step -1
-
David
Many a thanks David. Actually I have done it through SQL. But I wanted to show to my niece how the sql actually works through Vb.net codings. I have stuck at the for loop. I thought the condition checking is dynamic. But It was found to be static at the beginning of the loop. Thanks for the insight and also the suggestions. Also I've noticed , U have taken a great interest in my submissions. So nice of U David. Thanks Again.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: ES Kim |
last post by:
"A Book on C" explains a technique to use an arbitrary array index range.
int* p = malloc(sizeof(int) * 10) - 1;
This way, the allocated array can be accessed with index range 1 ~ 10,
not 0 ~ 9. You cannot use p, of course, and the memory should be
released with free(p + 1).
It worked with gcc and MSVC, but I'm not sure if it makes sense to take
the address before the initial element of an array. Does anyone have
any definite answer?
|
by: smith flyers |
last post by:
string thingword=" testing hmmm";
string firstLetter,restOfWord;
foreach (string word in thingword.Split())
{
Console.WriteLine(word); // if this only use for output and the rest in
the block is commented, no error !!!
|
by: Pengyu |
last post by:
Whenever I call listView.RemoveAt(listView.Count-1),
which remove the last item, I get an out of range
exception. How to solve this problem?
Thanks a lot,
Pengyu.
|
by: Bill Johnson |
last post by:
I have a further question regarding an archive post at:
http://groups-beta.google.com/group/microsoft.public.dotnet.languages.csharp/messages/fe7604b410b324e8,ab8f23349b8bf19d,d79c7c14342df130,34af0447073a6842
and the error message:
System.ArgumentOutOfRangeException from System.Windows.Forms
Specified argument was out of the range of valid values.
Parameter name: '70' is not a valid value for 'displayIndex'.
|
by: Brad |
last post by:
Now I am encountering my second very strange problem. I will describe the first one later. But my current problem lies with trying to bind to a combobox:
Try
If clubID <> "" And txtYear.Text <> "" Then
DsEvents1.Clear()
daEventInfo.SelectCommand.Parameters(0).Value = clubID
daEventInfo.SelectCommand.Parameters(1).Value = txtYear.Text
daEventInfo.Fill(DsEvents1)
ElseIf txtYear.Text = "" Then
MsgBox("Event Year needs to be selected",...
| |
by: Nick |
last post by:
Hi people,
I have a custom control combo box in a windows application that has a list
of items to be displayed. there are currently 17 items that are bound to the
table that acts as the data source for this combo. Now as this combo is an
editable box, when I try to add a new item, at times, and
ArgumentOutOfRangeException is raised. On debugging, this is raised whenevr I
try to access the control using:
Me.SelectedValue
or any other...
|
by: pcnerd |
last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap
& then displays the bitmap. When I run the program, I see the pixels being
plotted down the left side of the form. When it gets to the bottom, the
"Argument out of range exception" occurs. So, obviously the program starts.
The Help tells me that the argument isn't within the range of values, but it
doesn't explain how to fix the problem. So the Help doesn't help!...
|
by: dedipya |
last post by:
Environment W2003K with Oracle 10 client connected to oracle using oracle odp provider.orcle is on solaries machine.
while filling the dataset .net is throughing the error "argument was out of the range of valid values".
we are getting error when trying to fill the dataset with more than 16 columns.
if any date column is avaible in retrivels then fill dataset method is throwing the bellow error.
Parameter name: Year, Month, and Day...
|
by: Pucca |
last post by:
Hi: Below is the error I got from the 2 lines of code below. I don't
understand why and how to correct it. The actionMenu.DropDownItems has 0
item in its collection at the time of the code. Thanks.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and
less than the size of the collection.
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |