title: Deleting a records in gridview error message box in ASP.NET C#
body: <p>How can I generate an error message box when a user delete a record's associated with other records in the tables in gridview? If a user delete a category from TblCategory I want it to generate an error message box that will inform the user that there are still books under that category and will not delete unless he removes all records associated to that category. </p>
  
  <p>Thanks in advance.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;asp:FormView ID="FormView1" runat="server" DataKeyNames="categoryid" DefaultMode="Insert"
          DataSourceID="categoryDataSource"&gt;
  
          &lt;EditItemTemplate&gt;
  
              categoryid:
              &lt;asp:Label ID="categoryidLabel1" runat="server" 
              Text='&lt;%# Eval("categoryid") %&gt;' /&gt;
              &lt;br /&gt;
  
              Name:
              &lt;asp:TextBox ID="nameTextBox" runat="server" 
              Text='&lt;%# Bind("name") %&gt;' /&gt;
              &lt;br /&gt;
  
              &lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                  CommandName="Update" Text="Update" /&gt;
                  &amp;nbsp;
                  &lt;asp:LinkButton ID="UpdateCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
  
          &lt;/EditItemTemplate&gt;
  
          &lt;InsertItemTemplate&gt;
  
              Name:
              &lt;asp:TextBox ID="nameTextBox" runat="server" 
              Text='&lt;%# Bind("name") %&gt;' /&gt;
              &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="nameTextBox" ValidationGroup="createcategory"&gt;
              &lt;/asp:RequiredFieldValidator&gt;
              &lt;br /&gt;
  
              &lt;asp:Button ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="createcategory" 
                  CommandName="Insert" Text="Create" /&gt;
                  &lt;asp:Button ID="InsertCancelButton" runat="server" 
                  CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt;
          &lt;/InsertItemTemplate&gt;
  
          &lt;ItemTemplate&gt;
              &lt;asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                  CommandName="New" Text="New" /&gt;
          &lt;/ItemTemplate&gt;
          &lt;EmptyDataTemplate&gt;
              &lt;asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                  CommandName="New" Text="New" /&gt;
          &lt;/EmptyDataTemplate&gt;
      &lt;/asp:FormView&gt;
      &lt;asp:SqlDataSource ID="categoryDataSource" runat="server" 
          ConnectionString="&lt;%$ ConnectionStrings:LibrarySystemConnectionString %&gt;" 
          DeleteCommand="DELETE FROM [TblCategory] WHERE [categoryid] = @categoryid" 
          InsertCommand="INSERT INTO [TblCategory] ([name]) VALUES (@name)" 
          SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]" 
          UpdateCommand="UPDATE [TblCategory] SET [name] = @name WHERE [categoryid] = @categoryid"&gt;
          &lt;DeleteParameters&gt;
              &lt;asp:Parameter Name="categoryid" Type="Int32" /&gt;
          &lt;/DeleteParameters&gt;
          &lt;UpdateParameters&gt;
              &lt;asp:Parameter Name="name" Type="String" /&gt;
              &lt;asp:Parameter Name="categoryid" Type="Int32" /&gt;
          &lt;/UpdateParameters&gt;
          &lt;InsertParameters&gt;
              &lt;asp:Parameter Name="name" Type="String" /&gt;
          &lt;/InsertParameters&gt;
      &lt;/asp:SqlDataSource&gt;
  &lt;/p&gt;
  &lt;h4 class="style6"&gt;
      List of Categories&lt;/h4&gt;
  &lt;p&gt;
      &lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
          AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
          DataKeyNames="categoryid" DataSourceID="categoryDataSource" ForeColor="#333333" 
          GridLines="None" RowDeleted="grdCategory_RowDeleted"&gt;
          &lt;RowStyle BackColor="#EFF3FB" /&gt;
          &lt;Columns&gt;
              &lt;asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /&gt;
              &lt;asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" /&gt;
          &lt;/Columns&gt;
          &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
          &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
          &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
          &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
          &lt;EditRowStyle BackColor="#2461BF" /&gt;
          &lt;AlternatingRowStyle BackColor="White" /&gt;
      &lt;/asp:GridView&gt;
  </code></pre>