This is a migrated thread and some comments may be shown as answers.

Rebinging Parent table generates error

5 Answers 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AkAlan
Top achievements
Rank 2
AkAlan asked on 04 Jan 2011, 12:24 AM
I have a Heierarchy RadGrid with one detail table and when I perform an insert on the detail table I perform an insert into the datasource of the parent table and everything works fine until I try to rebind the grid using " e.Item.OwnerTableView.ParentItem.OwnerTableView.DataBind()" in the RadGrid1_InsertCommand. I get an error "Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object." I tried to step through and it seems as if maybe it's erroring when trying to populate a control although I'm not sure. If I remove the rebind line it does the insert and I see the change after refreshing the grid which leads me to believe the data is good. Thanks for any help.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Jan 2011, 12:49 PM
Hello AkAlan,

I guess you are using AdvancedDataBinding to poplate  RadGrid . And you are trying to Rebind the grid using DataBind() method which is not possible. That is you cannot use both AdvanceDataBinding and SimpleDataBinding. If you want to refresh the parent grid, try the Rebind() method.
 e.Item.OwnerTableView.ParentItem.OwnerTableView.Rebind().

Hope this informatiuon helps,
Princy.
0
AkAlan
Top achievements
Rank 2
answered on 04 Jan 2011, 05:53 PM
I have edited this post due to new info.

Hi Princy, Yes,I am using a SqlDataSource and I had tried the rebind method but get the same error. Maybe I need to place the rebind command someplace other than in the RadGrid1_InsertCommand section? I deleted all the parent rows except one and deleted all child rows. The error happens after the InsertCommand has finished running. I stepped through the code and found that after the InsertCommand is run it goes through the ItemDatabound sub and then back to the InsertCommand where no code is ran on this go around but when the sub exits, I get the error "Object reference not set to an instance of an object." I'll post my two subs in case that is a clue. Thanks for any help.
Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
       If e.Item.OwnerTableView.Name = "Transfers" Then ' only want to be here if it's the Transfers table inserting
           Dim parentItem As GridDataItem = TryCast(e.Item.OwnerTableView.ParentItem, GridDataItem) ' need to get the id of the parent table so we know which cylinder to insert against
           Dim lastEditedBy As String = Right(User.Identity.Name, Len(User.Identity.Name) - 4)
           sdsRefrigerantTransfers.InsertParameters("CylinderID").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("CylinderID").ToString()
           sdsRefrigerantTransfers.InsertParameters("EnteredBy").DefaultValue = lastEditedBy
           Dim qtyAfterTransfer As RadTextBox = DirectCast(e.Item.FindControl("rtbQtyAfterTransfer"), RadTextBox)
           'Need to update the Cylinder Quantity with the new value from Cylinder Qty After Transfer
           sdsUpdateRefrigerantQty.UpdateParameters("CylinderID").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("CylinderID").ToString()
           sdsUpdateRefrigerantQty.UpdateParameters("RefrigerantQty").DefaultValue = qtyAfterTransfer.Text
           sdsUpdateRefrigerantQty.Update()
           e.Item.OwnerTableView.ParentItem.OwnerTableView.Rebind()
       End If
       If e.Item.OwnerTableView.Name = "Cylinders" Then ' only want to be here if it's the Cylinders table inserting
           Dim lastEditedBy As String = Right(User.Identity.Name, Len(User.Identity.Name) - 4)
           sdsRefrigerantTransfers.InsertParameters("EnteredBy").DefaultValue = lastEditedBy
       End If
   End Sub

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
       If e.Item.OwnerTableView.Name = "Cylinders" Then
           If e.Item.IsInEditMode Then
               Dim rdpHydroTest As RadDatePicker = DirectCast(e.Item.FindControl("rdpHydroTestDate"), RadDatePicker)
               Dim rdpOnSite As RadDatePicker = DirectCast(e.Item.FindControl("rdpOnSite"), RadDatePicker)
               Dim rdpDepartSite As RadDatePicker = DirectCast(e.Item.FindControl("rdpDepartSite"), RadDatePicker)
               rdpHydroTest.MaxDate = Now()
               rdpOnSite.MaxDate = Now()
               rdpDepartSite.MaxDate = Now()
               If Not TypeOf e.Item Is IGridInsertItem Then 'only want to be here during edits, not inserts
                   Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                   item("EditCommandColumn").Controls.Clear() 'hide the edit button
                   ' want to set max dates on radDatePickers
                   'need to get text or lable values here so we can populate combo boxes with existing values
                   Dim refrigerantCombo As RadComboBox = DirectCast(item.FindControl("rcbRefrigerantType"), RadComboBox)
                   Dim cylinderCombo As RadComboBox = DirectCast(item.FindControl("rcbCylinderType"), RadComboBox)
                   ' Cast the e.Item.DataItem object to its real type 
                   Dim dataItem As DataRowView = TryCast(e.Item.DataItem, DataRowView)
                   '...access the value in the Column where the RadComboBox resides.
                   Dim selectedRefrigerantType As String = dataItem("RefrigerantTypeID").ToString()
                   Dim selectedCylinderType As String = dataItem("CylinderType").ToString
                   'this particular column had some spaces in the data so I remove them here, shouldn't be a problem but will leave it for now
                   selectedRefrigerantType = Replace(selectedRefrigerantType, " ", "")
                   selectedCylinderType = Replace(selectedCylinderType, " ", "")
                   ' Look for the Item with the same value as the one in the current Column.
                   Dim refrigerantType As RadComboBoxItem = refrigerantCombo.FindItemByValue(selectedRefrigerantType)
                   ' If such an Item exists...
                   If refrigerantType IsNot Nothing Then
                       ' ... select it.
                       refrigerantType.Selected = True
                   End If
                   Dim cylinderType As RadComboBoxItem = cylinderCombo.FindItemByText(selectedCylinderType)
                   If cylinderType IsNot Nothing Then
                       ' ... select it.
                       cylinderType.Selected = True
                   End If
               End If
           End If
       End If
       If e.Item.OwnerTableView.Name = "Transfers" Then
           If e.Item.IsInEditMode Then
               Dim rdpTransferDate As RadDatePicker = DirectCast(e.Item.FindControl("rdpTransferDate"), RadDatePicker)
               rdpTransferDate.MaxDate() = Now()
               rdpTransferDate.DbSelectedDate = Now()
           End If
       End If
   End Sub

0
Princy
Top achievements
Rank 2
answered on 05 Jan 2011, 11:14 AM
Hello AkAlan,

You can refer the following code library which shows how to perform automatic operations (update/insert/delete) in hierarchical grid with SqlDataSource control.
Automatic operations in hierarchical grid with SqlDataSource control

Or try the approach specified in the following forum post.
Parent table update after updating detail table in hierarchy radgrid

Hope this helps,
Princy.
0
AkAlan
Top achievements
Rank 2
answered on 05 Jan 2011, 05:46 PM
Hi Princy, I had read these posts before but I don't use and am not familiar with the needdatasource event so I didn't pay a lot of attention to them. My understanding on the needdatasource is that if I am using a sqldatasource, I shouldn't use it. I'll continue to research and learn more on the subject and post my solution when I find one. Thanks for all your help on this.
0
AkAlan
Top achievements
Rank 2
answered on 11 Jan 2011, 06:11 PM
I submitted a ticket on this issue and got a resolution. http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=382212

Basically I put the rebind code int the ItemUpdated event of the grid like so.


Protected Sub RadGrid1_ItemUpdated(source As Object, e As GridUpdatedEventArgs) 
    Dim insertItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem) 
    insertItem.OwnerTableView.ClearEditItems() 
    RadGrid1.Rebind() 
End Sub

Tags
Grid
Asked by
AkAlan
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
AkAlan
Top achievements
Rank 2
Share this question
or