Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databind error on datagrid using dataview

Status
Not open for further replies.

RandyBlackburn

Programmer
Oct 1, 2002
153
US
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:

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.
 
try setting LeadGrid.EditItemIndex = 0
to
LeadGrid.EditItemIndex = -1



 
dvannoy,

Thanks for the response.

Making that change eliminates the ability to edit after the insert, and the databind still aborts...as far as I can see, the database IS being changed....it's just the databind after the insert/cancel(delete) that aborts.

Everything works fine if the dataset is the datagrid's datasource, vice the dataview... except that then I can't do a search using the dataview.

Also, I've found that if I try to change the grid's data source TO a data view FROM a dataset, it works until the databind, where it is RESET to the dataset....

would a datatable maybe work better here? If so, how would I change things?





 
At this point, I'd appreciate ANY WORKAROUND to fix the databind error in the delete routine.

Thanks,
Randy
 
you have somewhat of a unique problem since you are always in edit mode within your grid.

to use a datatable you would do something like this.

Dim dgCommand As New SqlCommand(strSQL, cn)
Dim dgDA As New SqlDataAdapter(dgCommand)

cn.Open()

dgDA.Fill(dgDS, "YourTablename")

dg.DataSource = dgDS.Tables("YourTableName")

dg.DataBind()

let me see what else I can find out..



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top