DataAdapter.update doubles the records in my database on every call

Alex_bg

Newcomer
Joined
Dec 26, 2004
Messages
17
Hello,

I have a page in ASP.NET with a datagrid, dataview and dataset. I am retaining the dataset values in a viewstate during postbacks.

When i click the delete or update button of my datagrid, the dataadapter updates the table with every record written twice.
Here is the code in DataGrid1_DeleteCommand:

Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand

Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
Dim dr As DataRow
dr = Me.F_positions1.Tables(0).Rows.Find(key)
Me.F_positions1.Tables(0).Rows.Remove(dr)
Me.OleDbDataAdapter1.Update(Me.F_positions1)
Me.DataGrid1.DataBind()

End Sub


The code i use to keep the dataset data is the following:

In the page_load handler:

If IsPostBack Then
Me.F_positions1.Clear()
Dim sr As New System.IO.StringReader(CStr(ViewState("ds_fiders")))
Me.F_positions1.ReadXml(sr)
Me.DataGrid1.DataBind()
end if


In the prerender handler:

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

Dim sw As New System.IO.StringWriter
Me.F_positions1.WriteXml(sw)
ViewState("ds_fiders") = sw.ToString()

End Sub


I will give one example: If I have rows 'a','b' and 'c', and I click delete button on 'c' row, the dataadapter saves two identical records as 'a' , two as 'b' and one as 'c'. The record I delete is present only once in the database table, the others, twice. This doubles the records im my table on every click of the delete button.


Please help!
Thanks,
Alex
 
Back
Top