RandyBlackburn
Programmer
ASP 1.1
I'm getting a null reference error when I delete a record that has just been added, and then rebind a datagrid.
I delete the record from a dataset (dsLeadCreditor).
A dataview (dvLeadSearch) uses that dataset as it's source, and
a datagrid (LeadGrid) uses the dataview as its source.
I can perform all the necessary machinations on the datagrid (including normal deletes), except cancelling an add/update in process.
Add/update -- A new record button, adds a blank record in pos 0 of the dataset. Then the datagrid is re-bound, then the user is put into update mode on the record -- no problem
If the user hits cancel during the update, it's supposed to delete the record from the dataset, and rebind the grid. It is that databind that causes the error below:
====================================================
below is the code in question:
and
I have tried using accept changes in the delete subroutine...but it causes concurrency errors with other actions.
I'm getting a null reference error when I delete a record that has just been added, and then rebind a datagrid.
I delete the record from a dataset (dsLeadCreditor).
A dataview (dvLeadSearch) uses that dataset as it's source, and
a datagrid (LeadGrid) uses the dataview as its source.
I can perform all the necessary machinations on the datagrid (including normal deletes), except cancelling an add/update in process.
Add/update -- A new record button, adds a blank record in pos 0 of the dataset. Then the datagrid is re-bound, then the user is put into update mode on the record -- no problem
If the user hits cancel during the update, it's supposed to delete the record from the dataset, and rebind the grid. It is that databind that causes the error below:
Code:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 482: dvLeadSearch.RowFilter = ""
Line 483: LeadGrid.DataSource = dvLeadSearch
Line 484: LeadGrid.DataBind()
Line 485:
Line 486:
Source File: C:\Inetpub\[URL unfurl="true"]wwwroot\BreakOut1\Lead.aspx.vb[/URL] Line: 484
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.DataView.IsOriginalVersion(Int32 index)
System.Data.DataRowView.GetColumnValue(DataColumn column)
System.Data.DataColumnPropertyDescriptor.GetValue(Object component)
below is the code in question:
Code:
Private Sub NewLeadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NewLeadBtn.Click
LeadGrid.DataSource = DsLeadCreditor1
If LeadGrid.CurrentPageIndex <> 0 Then
LeadGrid.CurrentPageIndex = 0
DataBind()
End If
Dim dr As DataRow = Me.DsLeadCreditor1.Lead.NewRow
dr("LeadFirstName") = ""
dr("LeadLastName") = ""
dr("LeadDate") = Today
Me.DsLeadCreditor1.Lead.Rows.InsertAt(dr, 0)
'DsLeadCreditor1.AcceptChanges()
dvLeadSearch.RowFilter = ""
Session("datasetlead1") = DsLeadCreditor1
Session("dvLeadSearch") = dvLeadSearch
SqlDataAdapterLeadCreditor.Update(DsLeadCreditor1)
LeadGrid.EditItemIndex = 0
LeadGrid.DataBind()
NewLeadBtn.Visible = False
End Sub
and
Code:
Private Sub LeadGrid_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles LeadGrid.DeleteCommand
Dim index As Integer
index = e.Item.ItemIndex
Dim key = Convert.ToInt32(LeadGrid.DataKeys(e.Item.ItemIndex).ToString)
Dim intKey As Int32 = Convert.ToInt32(key)
Dim LeadRow As dsLeadCreditor.LeadRow
LeadRow = DsLeadCreditor1.Lead.FindByLeadID(intKey)
'===============Below delete performs cascading delete of creditors
LeadRow.Delete()
'DsLeadCreditor1.AcceptChanges()
SqlAdapterLead.Update(DsLeadCreditor1) '(DataSet21)
Session("datasetlead1") = DsLeadCreditor1
Session("dvLeadSearch") = dvLeadSearch
dvLeadSearch.RowFilter = Nothing
dvLeadSearch.RowFilter = ""
LeadGrid.DataSource = dvLeadSearch
LeadGrid.DataBind()
End Sub
I have tried using accept changes in the delete subroutine...but it causes concurrency errors with other actions.