VB.NET Deleting row from dataTable is removing all rows - vb.net

I am having one heck of a time deleting one row from a datatable.
I'm using this code:
Dim foundRow As DataRow() = nodes.Select("identifier LIKE '*Scene Root*'")
If foundRow.Count > 0 Then foundRow(0).Delete()
nodes.AcceptChanges()
The problem is this is removing ALL rows from the datatable.
Dset.Tables("node").Rows(0).Delete()
That also deletes all rows from the table. I am a little confused as to why this is happening.
Help me regain my sanity!
I should add.. I have single stepped first example and it finds one row and it IS the row I want to delete but the actual .delete is deleting every row in the table.
Maybe its what's in the table?

It seems likely your code is running repeatedly. Try putting a Debug.WriteLine("Hello, world") in there and see how many times the message appears each time it's supposed to delete one row.

Related

Database Updating issues vb.net (2015)

I am having trouble updating a database in a particular way that's kinda difficult to explain but I'll try. Everything works good as it is the way I have set it up. But now I need to update the value in each cell for one column a row at a time in a For loop. The loop adds the values of six previous cells and I want that sum in a Sum column for each row(record). Currently the sum column contains the wrong sum for each row and the idea is to recalculate and overwrite the old sum column value. Now I'm just a newbie to vb.net and my code will probably look very amateurish to most here so bear with me.
I am using a binding source with the appropriate datasets and table adapters and the code I post is where I'm at at this point. What this code is doing is it gets the right value to overwrite the sum cell but it doesn't write it to the database after each row iteration of the For Loop. Instead it runs through all 3482 records and writes the last rows calculations to every rows sum column cell. Example the sum for the last row is 40 and every cell in the sum column will have 40 for it's value. Now I have tried moving the update section to different location in and out of the first for loop and it pretty much does the same thing or worse. Here's my code below with a follow up after.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim z As Int16, x As Int16, y As Int16, PosX As Int16, PosY As Int16, Result As Int16, SumResult As Int16
All649_BS.MoveFirst()
Deltas_BS.MoveFirst()
For z = 1 To All649_BS.Count
SumResult = All649_BS.Current(2)
For x = 3 To 7
y = x - 1
PosX = All649_BS.Current(x)
PosY = All649_BS.Current(y)
Result = PosX - PosY
SumResult = SumResult + Result
'Deltas_BS.Current(x) = Result
DeltaSum.Text = SumResult
Next
All649_DataSet.AcceptChanges()
Deltas_BS.Current(9) = SumResult
Try
Validate()
Deltas_BS.EndEdit()
DeltasTableAdapter.Update(All649_DataSet)
Messages.Text = ("Update successful")
Catch ex As Exception
Messages.Text = ("Update failed")
End Try
All649_BS.MoveNext()
Deltas_BS.MoveNext()
SumResult = 0
Next
End Sub
This is code I tried at first and when wouldn't work decided to write code to basically rewrite all cells for each row(record) all 3482 of them. But when I set it up like that I deleted all records in the table to write to and stated fresh. But then the code wouldn't even add new records to the table. I wasted a crap load of time trying to figure out why it wouldn't add a new record just to get started. So I went back to this code. All my other functions that add new records works fine for another table in the dataset and another whole database for another set of tables. Never had an issue til now. One thing I did change that may have caused this problem is I originally had a separate bindsource, dataset, tableadapter for each table in two different databases but it was cluttered and I kinda got a better idea how these things worked and tried to streamline it all. Instead of using a bunch of datasets I made one with ALL the tables I need and then a new bindingsource and tableadapter would be created when I bound a textbox to that one dataset. It was all going fine til I tackled a second table and all went to crap when it wouldn't add new records after I ditched the whole table to rewrite it all with the new sums added. I had to do the same thing to the first table and if I remember right at the moment it succeeded with a few minor difficulties after the code ran which I corrected using Access.
I hope I explained this well enough. I saw another solution for a similar problem and they used something like .rows(i)(6) type of accessing rows and cells via indexes but I don't think you can do that with binding sources and datasets and adapters. In that solution the guy was using a dgv that was bound.
A couple things I forgot ... my update command seems to work but for all cells one same value. UPDATE Deltas SET [Sum] = ? God it was so much easier using VB6. Another weird thing wash happening when I ran the old code trying to rewrite the whole table after deleting all record.. Ran code opened table in Access and it was all out of order. I would resort by date in Access and delete the auto number column and make a new one and save and it would all go back to disordered auto number column all out of whack again. And it would be different every time. I finally got a result where all I had to do was fix the first 10 records then I tried doing the second table using the same approach only difference was instead of a 6 cell sum it was a 7 and the indexes were adjusted accordingly for the extra column. If I think of anything else I'll be back here editing.
Yup I'm back ... one more thing is I use a break point on thhe next line on the 3 to 7 loop and continue thru til it exits then check the sumresult which is always good then step thru to end of validate/update stop there and look at the tableadapter contents for current row and that value is written in and correct for the column but the database update writes the last sumresult (40) to all the cells in the sum column. So I need to find out why the update cannot update that cell in each row as it steps thru the row looper.
Well I figured it out on my own. I took 4 or 5 days off from working on the problem and got back to it last night when I posted my dilemma. Just after my last edit I found the cause. I needed to use a WHERE condition to limit the update to just the record I wanted changed. I wasn't aware that the update does the whole table without that condition.
Thanks anyway for not being able to help me out. LOL What an idiot I am ! Such a simple solution and quite obvious but like I said in the post, I'm still very new to VB.Net

Starting a 'For Each As DataGridViewRow' at a specific Row

Currently I have a function that searches through every row in my DataGridView and looks like so.
For Each row As DataGridViewRow In DataGridView1.Rows
Next
However due to an issue caused by something else I need to make it start the For Each Row at a specific row.
I've tried "For Each row As DataGridViewRow In DataGridView1.Rows.IndexOf(RowToStartAt)" However this appears to be the wrong method to use.
Is what I'm trying to do actually possible or not? Or have I just tried to use the wrong method?
You could use this kind of syntax
For Each row in dgv.Rows.Cast(Of DataGridViewRow)().Skip(5)
Console.WriteLine(row.Cells(0).Value.ToString())
Next
In this example the loops skips the first 5 rows and then starts its enumeration
Not sure if this is really beneficial or not. A traditional for ... loop is probably more clearer but as Linq goes this achieve the result required.

VBA Tying Up in a Do While Loop

I'm using Excel 2010. I have a Do While loop processing a large table over 100,000 rows long. If it finds a particular cell in the table, it inserts two rows after that cell and copies the contents of that row to the two new blank rows just created. The loop works fine until it gets to about the 20,000 row and then it locks up. Up to that point it is processing perfectly. It does not always lock up on the same row. I'm using a copy, then a paste special to duplicate the row. After the copy paste is done for the row, I clear the clip board with "Application.CutCopyMode = False". If I comment out the copy/paste, the loop successfully completes.
For the amount of data that I'm working with, I would guess that it will insert about 30,000 rows based on the original table. Is there anything odd about copy/paste special that I should know about?
You are working with a table, why do you need to insert rows?
Append them on the end of the table. If the order is an issue a table in a particular order can be put in that order with a sort (a sort key which is probably implicit in your table already).
Better yet - append the new rows to an in memory table object and paste the entire table object to the end of the original table when your loop is complete. This way you also avoid processing your inserted rows, get much simpler logic in the loop, and the process will probably run faster.
First, I commented out the pastes and it failed. The insert of the two rows also failed without the copy and the pastes. My solution was to first order the starting table. As I found a row that needed duplication, I read that row into an Array and then saved the array to the row past the bottom of the table. After processing the table, I then reordered the rows using the order column I first created and then deleted that column. Runs faster then the copy/paste but I lost the cell formatting in the new rows.

Deleting data in last cell of data in Column A

I am in need of assistance agian. When my macro works, the number 1 is populated in the final cell in column A because the coding is set up to place a number 1 or 2 depending on other data until the last row of data. Since the last row of data has necessary information in other columns, I wanted to delete the 1 in column A because it is not needed. How do I code to take away the value in the final cell of a column? I've tried referencing to it and if statements. I am sure I am missing something simple.
This is my first macro using VBA.
Thanks.
I just started working with VBA in the last day or two, but I believe the thing you are looking for is: Cells(x,y).End(xldown).ClearContents
Alternatively, I think Range("A1").End(xldown).ClearContents would be equally effective.
But I just started doing this myself, so take with spoonful of salt.

Delete duplicate rows and each row is length of 40 columns . what to do?

Well this is not the first time im asking these question here but I cannot make it over.
I have to delete the duplicate rows in a column. say from
range("A1:A30000") have to delete all the duplicate rows in these columns
I have used the previous solutions from my previous questions given by the programmers on the stackoverflow. They are working amazing but not these time.Last time I had each row of 15 columns but these time each row with 40 columns and none of the scripts working. The pc its just getting hang and sometimes taking 2 hrs thats not what i want. I have used the dictionary method as suggested by Issun,Jon,Reafidy and Doc brown even they are not working. I dont know why.
So I thought to use the advanced filters but im unable to delete the duplicate rows from vba. I dont find it on google, I see the manual one with the boxes but not the vba script even i find its not working fine.
Range("A1:A5").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
What I have to do after applying that line. delete the duplicate rows now. I remember its like copy pasting but i dont remember the code well and something like
xltypeinvisble:delete
I dont remember the code well . I remember that i read on google now im unable to find it.
can anyone tell me to delete the duplicate entries using the advanced filter method ? do you think ,that rows had 40 columns each and that made my scripts to take long time and sometimes hang up?what might be the cause to it?
any other method which is faster is appreciated (probably dictionary one). I belive people say dictionary method but i dont know its not working fine may be the code error or not sure about the reason.its just getting hang up. so unable to know why, well its working fine for small data that i tested. but not with the bigger one.
any help is greatly appreciated!
I'm not familiar with the single line command you are looking for to delete all invisible rows at once, but you can loop through the rows in the range and manually delete them.
If this still runs too slow for you, you may want to consider adding the VBA commands to turn off screen updating and re-calculations while the macro is running.
Sub DeleteDUpes()
Dim ThisRange As Range
Dim NewRange As Range
Set ThisRange = Range("A1:A30000")
ThisRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
' Loop through each row from bottom up, deleting rows hidden by filter
For thisrow = ThisRange.Rows.Count To 1 Step -1
Set NewRange = ThisRange.Resize(1).Offset(thisrow - 1, 0)
If NewRange.EntireRow.Hidden Then
r.EntireRow.Delete
End If
Next
End Sub