hog Posted June 3, 2003 Posted June 3, 2003 Any ideas why this code always reports the error "no row 6"? If the dataset table has 12 rows which I confirm by looking in the Access table then run this code I keep getting the same error? For intX = 0 To m_dsJob.Tables("tblJobs").Rows.Count - 1 m_drJob = m_dsJob.Tables("tblJobs").Rows(intX) m_drJob.Delete() m_odaJob.DeleteCommand = m_ocbJob.GetDeleteCommand m_odaJob.Update(m_dsJob.Tables("tblJobs").GetChanges) m_dsJob.Tables("tblJobs").AcceptChanges() Next Quote My website
zy_abc Posted June 3, 2003 Posted June 3, 2003 Hog, I think you are getting the error since you are deleting from the DataTable and Accepting the Changes. The rowcount will not be the same because of that. Quote Thanks & Regards, zy_abc
hog Posted June 3, 2003 Author Posted June 3, 2003 OK that makes sense, but what of this problem then.... A contract table and a job table. The contract table has a single entry stating 12 jobs The jobs table has 12 entries, one for each job. The use selects to delete the contract and thus the jobs. I'm trying to trap concurrency errors where some other user may be modifying the job data.....hence my code above. Have I lost the plot on this one? Quote My website
*Experts* jfackler Posted June 3, 2003 *Experts* Posted June 3, 2003 Because you update each loop, the number of rows is progressively decreasing. When you get to row = 6, you have deleted the first 6 rows and updated your ds thus there is now no row 6. Take the update out of your for loop. Incidently, update and fill methods automatically call an acceptchanges to the dataset. Unless you are directly implementing a command you don't need to call an acceptchanges. edit: zy beat me to it. Jon Quote
*Experts* jfackler Posted June 3, 2003 *Experts* Posted June 3, 2003 OK that makes sense, but what of this problem then.... A contract table and a job table. The contract table has a single entry stating 12 jobs The jobs table has 12 entries, one for each job. The use selects to delete the contract and thus the jobs. I'm trying to trap concurrency errors where some other user may be modifying the job data.....hence my code above. Have I lost the plot on this one? Can you not include a delete on both of the tables in your for loop? Quote
hog Posted June 3, 2003 Author Posted June 3, 2003 I've been up too long! If I change to this, For intX = 0 To m_dsJob.Tables("tblJobs").Rows.Count - 1 m_drJob = m_dsJob.Tables("tblJobs").Rows(intX) m_drJob.Delete() m_odaJob.DeleteCommand = m_ocbJob.GetDeleteCommand m_dsJob.Tables("tblJobs").AcceptChanges() Next I still get no row 6! Quote My website
zy_abc Posted June 3, 2003 Posted June 3, 2003 Why don't you try this Dim intY as Integer=m_dsJob.Tables("tblJobs").Rows.Count - 1 Dim intX as Integer=0 Dim int Z as Integer=0 While intZ<=intY m_drJob = m_dsJob.Tables("tblJobs").Rows(intZ) m_drJob.Delete() m_odaJob.DeleteCommand = m_ocbJob.GetDeleteCommand m_odaJob.Update(m_dsJob.Tables("tblJobs").GetChanges) m_dsJob.Tables("tblJobs").AcceptChanges() intY=intY-1 End While Hope this helps. This is just a basic idea as to how to overcome this error. Quote Thanks & Regards, zy_abc
*Experts* jfackler Posted June 3, 2003 *Experts* Posted June 3, 2003 For intX = 0 To m_dsJob.Tables("tblJobs").Rows.Count - 1 m_drJob = m_dsJob.Tables("tblJobs").Rows(intX) m_drJob.Delete() m_odaJob.DeleteCommand = m_ocbJob.GetDeleteCommand ' m_dsJob.Tables("tblJobs").AcceptChanges() Next Quote
*Experts* jfackler Posted June 3, 2003 *Experts* Posted June 3, 2003 A few thoughts, When you do a delete on a dataset, it is different than when you call a delete against a db. The db deletes the data. A ds marks the row for deletion and sets the column values to null. The actual deletion occurs when an acceptchanges is called. When you call an acceptchanges, the rowstate is reset to current and original. Consequently, if you call an acceptchanges before you call an update on your dataadapter, it wont read a modification in the data and no insert, delete, or update commands will be implemented against the db. Jon Quote
hog Posted June 3, 2003 Author Posted June 3, 2003 Result.........could you hear the penny drop :-) For intX = 0 To m_dsJob.Tables("tblJobs").Rows.Count - 1 m_drJob = m_dsJob.Tables("tblJobs").Rows(intX) m_drJob.Delete() m_odaJob.DeleteCommand = m_ocbJob.GetDeleteCommand Next m_odaJob.Update(m_dsJob.Tables("tblJobs").GetChanges) Thanks to you both..... Quote My website
*Experts* Nerseus Posted June 5, 2003 *Experts* Posted June 5, 2003 You can move out the last line of your For statement, where you set the DeleteCommand, to after the Next. There's no need to set it each time. Also, I've often deleted inside the loop. Just loop backwards, from Count-1 to 0 with a Step (if that's still the right keywork in .NET). The issue is that VB calculates the from and to values only once. So once it goes past the line "For intX..." it figures out 0 and your Count-1 and stores them in internal variables. It does NOT check them on each pass. Counting backwards works because you don't care if the count is changing. You can also use a While loop (as suggested), which WILL check the count each pass. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
hog Posted June 5, 2003 Author Posted June 5, 2003 Good advice.....cheers Nerseus :-) Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.