RadGrid for ASP.NET AJAX

RadControls Send comments on this topic.
Error handling automatic operations
Controls > RadGrid > Insert/Update/Delete records > Error handling automatic operations

Glossary Item Box

RadGrid can fire three events after an automatic action occurred:

  • ItemUpdated
  • ItemInserted 
  • ItemDeleted
C# Copy Code
protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
       {
           
if (e.Exception != null)
           {
               e.KeepInEditMode = true;
               e.ExceptionHandled = true;
               DisplayMessage(
"Product " + e.Item["ProductID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
           }
           
else
           {
               
DisplayMessage("Product " + e.Item["ProductID"].Text + " updated");
           }
       }

       
protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
       {
           
if (e.Exception != null)
           {
               e.ExceptionHandled = true;
               e.KeepInInsertMode = true;
               DisplayMessage(
"Product cannot be inserted. Reason: " + e.Exception.Message);
           }
           
else
           {
               
DisplayMessage("Product inserted");
           }
       }

       
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
       {
           
if (e.Exception != null)
           {
               e.ExceptionHandled = true;
               DisplayMessage(
"Product " + e.Item["ProductID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
           }
           
else
           {
               
DisplayMessage("Product " + e.Item["ProductID"].Text + " deleted");
           }
       }

      
private void DisplayMessage(string text)
      {
           RadGrid1.Controls.Add(
new LiteralControl(text));
      }
VB.NET Copy Code
Protected Sub RadGrid1_ItemUpdated(source As Object, e As Telerik.Web.UI.GridUpdatedEventArgs) Handles RadGrid1.ItemUpdated
   If Not (e.Exception Is Nothing) Then
      e.KeepInEditMode = True
      e.ExceptionHandled = True
      DisplayMessage("Product " & e.Item("ProductID").Text & " cannot be updated. Reason: " & e.Exception.Message)
   Else
      DisplayMessage("Product " & e.Item("ProductID").Text & " updated")
   End If
End Sub 'RadGrid1_ItemUpdated


Protected Sub RadGrid1_ItemInserted(source As Object, e As GridInsertedEventArgs) Handles RadGrid1.ItemInserted
   If Not (e.Exception Is Nothing) Then
      e.ExceptionHandled = True
      e.KeepInInsertMode = True
      DisplayMessage("Product cannot be inserted. Reason: " & e.Exception.Message)
   Else
      DisplayMessage("Product inserted")
   End If
End Sub 'RadGrid1_ItemInserted


Protected Sub RadGrid1_ItemDeleted(source As Object, e As GridDeletedEventArgs) Handles RadGrid1.ItemDeleted
   If Not (e.Exception Is Nothing) Then
      e.ExceptionHandled = True
      DisplayMessage("Product " + e.Item("ProductID").Text + " cannot be deleted. Reason: " + e.Exception.Message)
   Else
      DisplayMessage("Product " + e.Item("ProductID").Text + " deleted")
   End If
End Sub 'RadGrid1_ItemDeleted


Private Sub DisplayMessage(ByVal text As String)
   RadGrid1.Controls.Add(New LiteralControl([text]))
End Sub 'DisplayMessage

The default behavior of Telerik RadGrid is to  let the DataSource control rise an exception  when error occurs when inserting/updating/deleting. To prevent this exception you should handle the corresponding event and in case (e.Exception != null) or (Not e.Exception Is Nothing) you should set e.ExceptionHandled to true and display error message. 

C# Copy Code
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
{
     
if (e.Exception != null)
     {
        e.ExceptionHandled = true;
        DisplayMessage(
"Product " + e.Item["ProductID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
     }
     ....
}
VB.NET Copy Code
 Protected Sub RadGrid1_ItemDeleted(ByVal source As Object, ByVal e As GridDeletedEventArgs) Handles RadGrid1.ItemDeleted
   If Not (e.Exception Is Nothing) Then
      e.ExceptionHandled = True
      DisplayMessage("Product " + e.Item("ProductID").Text + " cannot be deleted. Reason: " + e.Exception.Message)
   End If
   ................

End Sub