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

[Solved] RadGrid delete command code

1 Answer 877 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brett
Top achievements
Rank 1
Brett asked on 22 Jul 2009, 08:01 PM

I used from the training book the delete command.  The problem that I am having is when I try to delete a record in the RadGrid, I get the following error:

Object referance not set to an instance of an object

The weird thing is I got it from the Telerik RadControls Ajax book and besides that I have no clue.  See attached code:

Imports System.Data.SqlClient  
Imports Telerik.Web.UI  
 
Partial Class EmployeeFamily  
    Inherits System.Web.UI.Page  
 
 
    Protected Sub RadGrid1_DeleteCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.DeleteCommand  
 
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
        Dim MemberFamilyID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("MemberFamilyID").ToString()  
        Try 
            'Delete command execute  
            SqlDataSource1.DeleteCommand = "DELETE FROM MemberFamilyInfo Where MemberFamilyID = '" + MemberFamilyID + "'" 
        Catch ex As Exception  
            RadGrid1.Controls.Add(New LiteralControl("Unable to delete MemberFamily. Reason: " + ex.Message))  
            e.Canceled = True 
        End Try 
 
 
 
    End Sub 
End Class 
 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Jul 2009, 05:32 AM
Hi Brett,

Have you set the DataKeyNames property of grid MasterTableView to "MemberFamilyID"? You can also try the following code in order to get the key value in code behind. Give a try with this code and see whether it helps.

C#:
 
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        string MemberFamilyId1 = item.GetDataKeyValue("MemberFamilyID").ToString();      
        try 
        { 
            //Delete command execute           
        } 
      . . . 
    } 

-Shinu.
Tags
General Discussions
Asked by
Brett
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or