AffectedRows Property

ListViewDeletedEventArgs.AffectedRows Property

Gets the number of rows that were affected by the delete operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

public int AffectedRows { get; }

Property Value

Type: System.Int32
The number of rows that were affected by the delete operation.

Use the AffectedRows property to determine the number of items that were affected by the delete operation, as reported by the data source control. This property is typically used to determine whether the correct number of items were deleted in the following ways:

  • To verify that only a single item was deleted. Sometimes an incorrectly written delete statement can delete multiple items.

  • To verify that an item was deleted if an error occurs during the delete operation but no exception was raised.

  • To verify that an item was deleted when a data source control that supports optimistic concurrency (such as the SqlDataSource and ObjectDataSource controls) has its ConflictDetection property set to the ConflictOptions.CompareAllValues enumeration value. Under this setting, an item that has been modified by another user might not be deleted.

The following example shows how to use the AffectedRows property to verify that an item was deleted during the delete operation.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

    void Page_Load()
        Message.Text = String.Empty;

    void ContactsListView_ItemDeleted(Object sender, ListViewDeletedEventArgs e)
        // Determine whether an exception occurred during the delete operation. 
        if (e.Exception == null)
            // Ensure that a record was deleted. 
            if (e.AffectedRows > 0)
                Message.Text = e.AffectedRows + " item(s) deleted successfully.";
                Message.Text = "No item was deleted.";
            // Insert the code to handle the exception here. 

            // Indicate that the exception has been handled.
            e.ExceptionHandled = true;
            Message.Text = "An error occurred during the delete operation.";


<html xmlns="" >
  <head runat="server">
    <title>ListViewDeletedEventArgs Example</title>
    <form id="form1" runat="server">

      <h3>ListViewDeletedEventArgs Example</h3>

      <asp:Label ID="Message"

      <asp:ListView ID="ContactsListView" 
          <table cellpadding="2" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              <asp:LinkButton ID="DeleteButton" runat="server" 
                OnClientClick="return confirm('Are you sure?');" />

      <!-- This example uses Microsoft SQL Server and connects    -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET  -->
      <!-- expression to retrieve the connection string value     -->
      <!-- from the Web.config file.                              -->            
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        DeleteCommand="DELETE FROM Person.Contact WHERE [ContactID] = @ContactID">
            <asp:Parameter Name="ContactID" Type="Int32" />


Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

© 2015 Microsoft