title: Delete Row Using Delete button in ASP GridView
body: <p>I have a GridView on an ASP. NET web form which is displaying some information from a table in a SQL database. I also have some buttons to delete, update and add new data. However, my delete button doesn't work. I keep getting the error "Object reference not set to an instance of an object."</p>
  
  <p>I will post the function below with the data in the table. Can anyone assist me, please?</p>
  
  <pre><code> &lt;asp:GridView ID="gvFarmer" runat="server"
          BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="5" style="margin-right: 58px"
          CellSpacing="1" GridLines="None" AutoGenerateColumns="false" Height="166px" Width="692px" ShowFooter="true" ShowHeaderWhenEmpty="true" 
          OnRowCommand="gvFarmer_RowCommand" OnRowEditing="gvFarmer_RowEditing" OnRowCancelingEdit="gvFarmer_RowCancelingEdit" OnRowUpdating="gvFarmer_RowUpdating" OnRowDeleting="gvFarmer_RowDeleting"&gt;
  
  
          &lt;FooterStyle BackColor="#C6C3C6" ForeColor="Black" /&gt;
          &lt;HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /&gt;
          &lt;PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /&gt;
          &lt;RowStyle BackColor="#DEDFDE" ForeColor="Black" /&gt;
          &lt;SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#594B9C" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#33276A" /&gt;
  
          &lt;Columns&gt; &lt;%--Colums are created here --%&gt;
  
                                          &lt;%-- COLUMN 1--%&gt;
  
               &lt;%-- Creation of template field to hold column names and information for a table--%&gt;
              &lt;asp:TemplateField HeaderText="Farmer ID"&gt; &lt;%-- here the filed is created--%&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label Text='&lt;%# Eval("Farmer_Id") %&gt;' runat="server"&gt;&lt;/asp:Label&gt; &lt;%-- By default the filed will be a label for viewing--%&gt;
                        &lt;%-- The eval() function will binds the title of colums name in the database to the title in browser. So, if changes are made they are reflected --%&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt; &lt;%-- when the field is clicked on to be eidted, it will be a textbox so the user can interact with--%&gt;
                      &lt;asp:TextBox ID="txtFarmerID" runat="server" Text='&lt;%# Eval("Farmer_Id") %&gt;'&gt;&lt;/asp:TextBox&gt;
                       &lt;%-- The eval() function will binds the title of colums name in the database to the title in browser. So, if changes are made they are reflected --%&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;&lt;%-- This will be the default area from which new records are added to the table--%&gt;
                      &lt;asp:TextBox ID="txtFarmerIDFooter" runat="server"&gt;&lt;/asp:TextBox&gt;
                       &lt;%-- A textbox is used for getting the pieces of information to be added to the table--%&gt;
                  &lt;/FooterTemplate&gt;
  
              &lt;/asp:TemplateField&gt;&lt;%-- End of first column--%&gt;
  
              &lt;%-- COLUMN 2--%&gt;
               &lt;asp:TemplateField HeaderText="First Name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label Text='&lt;%# Eval("FirstName") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtFarmerFirstName" runat="server" Text='&lt;%# Eval("FirstName") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox ID="txtFarmerFirstNameFooter" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/FooterTemplate&gt;
  
              &lt;/asp:TemplateField&gt;
  
              &lt;%-- COLUMN 3--%&gt;
               &lt;asp:TemplateField HeaderText="Last Name"&gt; 
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label Text='&lt;%# Eval("LastName") %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtFarmerLastName" runat="server" Text='&lt;%# Eval("LastName") %&gt;'&gt;&lt;/asp:TextBox&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox ID="txtFarmerLastNameFooter" runat="server"&gt;&lt;/asp:TextBox&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p></p>
  
  <p>Below is the C# Code for the Delete Function after you click the delete icon:</p>
  
  <pre><code> protected void gvFarmer_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
  
          try
          {
              using (SqlConnection con = new SqlConnection(conStr))
              {
                  con.Open();
  
                  string InsertQuery = "DELETE FROM Farmer WHERE Farmer_Id = @Farmer_Id";
                  //parametrized variables are used to prevent sql injection
  
                  SqlCommand insert = new SqlCommand(InsertQuery, con);
  
                  insert.Parameters.AddWithValue("@Farmer_Id", (gvFarmer.Rows[e.RowIndex].FindControl("txtFarmerID") as TextBox).Text.Trim());
                  //get the info from textbox, trim spaces and store it in appropirate fields in the database
  
                  insert.ExecuteNonQuery(); //function executes the insert query
  
                  PopulateGridView(); //function is called to show updated view.
  
                  lblSuccess.Text = "Record Deleted!";
                  lb1Error.Text = "";
              }//using block ends here
  
          }
          catch (Exception ex)
          {
  
              lblSuccess.Text = "";
              lb1Error.Text = ex.Message;
          }//end of try catch
  
      }
  </code></pre>