Connecting Tech Pros Worldwide Forums | Help | Site Map

Update-Insert DetailsView, result is duplicating lines

Newbie
 
Join Date: Oct 2007
Posts: 22
#1: Sep 23 '08
Everytime I edit a record, and hit update I get duplicating lines in my Details View. Here's my If statement:

Expand|Select|Wrap|Line Numbers
  1. If (Len(e.OldValues("AutoNumber")) > 0) Then
  2.             sql = "UPDATE SHIP_LOG SET FileNumber='" & e.NewValues("FileNumber") & "',BOL='" & e.NewValues("BOL") & "',Container='" & e.NewValues("Container") & "',DESTN='" & e.NewValues("DESTN") & "', Comments='" & e.NewValues("Comments") & "' WHERE POD_COMINV='" & e.Keys("POD_COMINV") & "' AND LineIndex='" & e.Keys("LineIndex") & "'"
  3.         Else
  4.             sql = "INSERT INTO SHIP_LOG (POD_COMINV,FileNumber,BOL,Container,DESTN,Comments,LineIndex) VALUES ('" & e.Keys("POD_COMINV") & "','" & e.NewValues("FileNumber") & "','" & e.NewValues("BOL") & "','" & e.NewValues("Container") & "','" & e.NewValues("DESTN") & "','" & e.NewValues("Comments") & "','" & e.Keys("LineIndex") & "')"
  5.         End If
  6.  
Any ideas???

Thanks

Expert
 
Join Date: Sep 2008
Location: USA
Posts: 188
#2: Sep 23 '08

re: Update-Insert DetailsView, result is duplicating lines


Is this code for a System.Data.DataTable update? Please clarify.

Is the data bound to a DataGrid?
What event are you using to trigger this update/insert?

Sounds like you are manually processing updates done manually in a DataGridView.
Do you have an additional DataAdapter.Update() statement that might be automatically updating the modified rows on its own?
Newbie
 
Join Date: Oct 2007
Posts: 22
#3: Sep 23 '08

re: Update-Insert DetailsView, result is duplicating lines


Quote:

Originally Posted by mldisibio

Is this code for a System.Data.DataTable update? Please clarify.

Is the data bound to a DataGrid?
What event are you using to trigger this update/insert?

Sounds like you are manually processing updates done manually in a DataGridView.
Do you have an additional DataAdapter.Update() statement that might be automatically updating the modified rows on its own?


Yes, the data is bound to a Gridview when I enter INV #.
Event I'm using is DetailsView1_ItemUpdating. No additional Update statement!
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Sep 23 '08

re: Update-Insert DetailsView, result is duplicating lines


Pl;ease remember to use [code] tags when posting code.

MODERATOR

Just a "by the way," you should learn how to use String.Format for building strings. Either that or System.Text.StringBuilder. Because reading long concatenated strings like that is painful, and hard to debug.
Expert
 
Join Date: Sep 2008
Location: USA
Posts: 188
#5: Sep 23 '08

re: Update-Insert DetailsView, result is duplicating lines


OK, you are using the actual DetailsView control, which I am not familiar with. Apologies, I thought you meant "details view" in a generic way and might be using a Forms.DataGridView with which I am more familiar.

Hopefully someone with more experience can help you. Nonetheless, sounds like ItemUpdating may be getting called twice...coders familiar with the control know these quirks and will tell you in a heartbeat.

In the meantime, maybe you could add some Trace statements to the ItemUpdating event handler and see if indeed it is getting called twice. Perhaps (longshot) after executing your sql it fires the updating event again? Try removing the "UPDATE part of your logic, so that it only inserts new records. If you don't get a duplicate, then it might mean it was firing an INSERT followed by an UPDATE for some reason.

Good luck.
Reply