title: Error Updating Gridview row with Linq , Entity Models , C#
body: <p>Hi and thanks in advance,</p>
  
  <p>I am trying to do a inline edit in my gridview but I keep getting this error:</p>
  
  <blockquote>
    <p>Sys.WebForms.PageRequestManagerServerErrorException: Object reference
    not set to an instance of an object.</p>
  </blockquote>
  
  <p>I had this working before, but then I had to add make a few modifications such as images and doing some boundfields instead of EditItemTemplate</p>
  
  <p>Asp.net frontend</p>
  
  <pre><code>&lt;asp:GridView ID="GridViewRangeSetup" runat="server" AllowSorting="True" OnSorting="Gridview_Sort"
                          PagerStyle-Mode="NumericPages" AutoGenerateColumns="false" Width="100%" CssClass="gridView"
                          OnPageIndexChanging="GridViewRangeSetup_PageIndexChanging" AllowPaging="True"
                          PageSize="20" DataKeyNames="RangeId" OnRowCommand="GridViewRangeSetup_RowCommand"
                          OnRowEditing="GridViewRangeSetup_RowEditing" OnRowCancelingEdit="GridViewRangeSetup_CancelEditRow"
                          OnRowUpdating="GridViewRangeSetup_UpdateRow" OnRowDataBound="GridViewRangeSetup_RowDataBound"&gt;
                          &lt;RowStyle CssClass="rowStyle"&gt;&lt;/RowStyle&gt;
                          &lt;HeaderStyle CssClass="headerBar" ForeColor="#ffffff"&gt;&lt;/HeaderStyle&gt;
                          &lt;AlternatingRowStyle CssClass="altRow" /&gt;
                          &lt;Columns&gt;
                              &lt;asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%" HeaderStyle-ForeColor="#f2f2f2"
                                  HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/icon_edit.png" CausesValidation="false"
                                          CommandArgument='&lt;%#Eval("RangeId") %&gt;' CommandName="Edit" /&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="~/images/icon_update.png"
                                          CausesValidation="false" CommandArgument='&lt;%#Eval("RangeId") %&gt;' CommandName="Update" /&gt;
                                      &lt;asp:ImageButton ID="ImageCancel" runat="server" ImageUrl="~/images/icon_cancel.png"
                                          CausesValidation="false" CommandArgument='&lt;%#Eval("RangeId") %&gt;' CommandName="Cancel" /&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="5%" /&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;%--  &lt;asp:TemplateField HeaderText="Copy" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
                                  HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:ImageButton ID="imgCopy" runat="server" CausesValidation="false" CommandArgument='&lt;%#Eval("RangeId") %&gt; '
                                          CommandName="Copy" ImageUrl="~/images/icon_copy.png" /&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" /&gt;
                              &lt;/asp:TemplateField&gt;--%&gt;
                              &lt;asp:TemplateField HeaderText="Delete" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
                                  HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:ImageButton ID="imgDelete" runat="server" CausesValidation="false" OnClientClick="return DeleleAlert();"
                                          CommandArgument='&lt;%#Eval("RangeId") %&gt;' CommandName="Remove" ImageUrl="~/images/icon_delete.png" /&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" /&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /&gt;
                              &lt;%--&lt;asp:TemplateField HeaderText="Description"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="txtDescription" runat="server" CssClass="textbox"&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="lblDescription" runat="server" Text='&lt;%# Bind("Description") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;--%&gt;
                              &lt;asp:TemplateField HeaderText="Country" SortExpression="Country"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:DropDownList ID="ddlCountry" runat="server" CssClass="dropdown" AutoPostBack="True"
                                          AppendDataBoundItems="true" DataTextField="CountryName" DataValueField="CountryId"&gt;
                                      &lt;/asp:DropDownList&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblCountry" runat="server" Text='&lt;%# Bind("CountryName") %&gt;'&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="State/Province" SortExpression="Region"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:DropDownList ID="ddlRegion" runat="server" CssClass="dropdown" AutoPostBack="True"
                                          AppendDataBoundItems="true" DataTextField="RegionName" DataValueField="GeographicRegionId"&gt;
                                      &lt;/asp:DropDownList&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblRegion" runat="server" Text='&lt;%# Bind("RegionName") %&gt;'&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:TemplateField HeaderText="Base/Facility" SortExpression="Facility"&gt;
                                  &lt;EditItemTemplate&gt;
                                      &lt;asp:DropDownList ID="ddlFacility" runat="server" CssClass="dropdown" AutoPostBack="True"
                                          AppendDataBoundItems="true" DataTextField="BaseName" DataValueField="BaseId"&gt;
                                      &lt;/asp:DropDownList&gt;
                                  &lt;/EditItemTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblFacility" runat="server" Text='&lt;%# Bind("BaseName") %&gt;'&gt;&lt;/asp:Label&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:BoundField DataField="MapName" HeaderText="Map Name" SortExpression="MapName" /&gt;
                              &lt;asp:TemplateField HeaderText="Map"&gt;
                                  &lt;HeaderStyle HorizontalAlign="center" /&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:HyperLink ID="HyperLink_Map1" runat="server" NavigateUrl='&lt;%# DataBinder.Eval(Container.DataItem,"MapPath") %&gt;'
                                          Text=""&gt;
                                          &lt;asp:Image ID="Image1" runat="server" ImageUrl='&lt;%# DataBinder.Eval(Container.DataItem,"MapPath") %&gt;'
                                              Width="50px" Height="50px" /&gt;
                                      &lt;/asp:HyperLink&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                              &lt;asp:BoundField DataField="LowLat" HeaderText="Low Latitude" SortExpression="LowLat" /&gt;
                              &lt;asp:BoundField DataField="LowLong" HeaderText="Low Longitude" SortExpression="LowLong" /&gt;
                              &lt;asp:BoundField DataField="HighLat" HeaderText="High Latitude" SortExpression="HighLat" /&gt;
                              &lt;asp:BoundField DataField="HighLong" HeaderText="High Longitude" SortExpression="HighLong" /&gt;
                              &lt;asp:TemplateField HeaderText="Status"&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:Button ID="RangeSetup_Status" CssClass="page-btn blue" CausesValidation="false"
                                          CommandArgument='&lt;%#Eval("RangeId") %&gt; ' runat="server" Text="Status" OnClick="btnRangeStatus_Click"&gt;
                                      &lt;/asp:Button&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                          &lt;/Columns&gt;
                      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>C# backend</p>
  
  <pre><code>protected void GridViewRangeSetup_UpdateRow(object sender, GridViewUpdateEventArgs e)
  {
  
      WISSModel.WISSEntities context = new WISSModel.WISSEntities();
      //TextBox txtDescription = (TextBox)GridViewRangeSetup.Rows[e.RowIndex].Cells[3].Controls[0];
      GridViewRow row = GridViewRangeSetup.Rows[e.RowIndex];
      //TextBox txtDescription = row.FindControl("txtDescription") as TextBox;
      //DropDownList ddlCountry = (DropDownList)GridViewRangeSetup.Rows[e.RowIndex].Cells[4].Controls[0];
      DropDownList ddlCountry = row.FindControl("ddlCountry") as DropDownList;
      DropDownList ddlRegion = row.FindControl("ddlRegion") as DropDownList;
      DropDownList ddlFacility = row.FindControl("ddlFacility") as DropDownList;
      //TextBox mapName = row.FindControl("MapName") as TextBox;
      //TextBox lowLat = row.FindControl("LowLat") as TextBox;
      //TextBox lowLong = row.FindControl("LowLong") as TextBox;
      //TextBox highLat = row.FindControl("HighLat") as TextBox;
      //TextBox highLong = row.FindControl("HighLong") as TextBox;
  
  
      int rangeID = Convert.ToInt32(GridViewRangeSetup.DataKeys[e.RowIndex].Values[0].ToString());
  
      if (rangeID &gt; 0)
      {
          var rangeEdit = (from r in context.Ranges
                           where r.RangeId == rangeID
                           select r).SingleOrDefault();
  
          //rangeEdit.Description = txtDescription.Text.Trim();
          rangeEdit.Description = e.NewValues["Description"].ToString();
          rangeEdit.CountryId = Convert.ToInt32(ddlCountry.SelectedValue);
          rangeEdit.GeographicRegionId = Convert.ToInt32(ddlRegion.SelectedValue);
          rangeEdit.BaseId = Convert.ToInt32(ddlFacility.SelectedValue);
          rangeEdit.RangeMap.MapName = e.NewValues["MapName"].ToString();//mapName.Text.Trim();
          rangeEdit.RangeMap.LowLat = Convert.ToDouble(e.NewValues["LowLat"].ToString());//Convert.ToDouble(lowLat.Text.Trim());
          rangeEdit.RangeMap.LowLong = Convert.ToDouble(e.NewValues["LowLong"].ToString());//Convert.ToDouble(lowLong.Text.Trim());
          rangeEdit.RangeMap.HighLat = Convert.ToDouble(e.NewValues["HighLat"].ToString());//Convert.ToDouble(highLat.Text.Trim());
          rangeEdit.RangeMap.HighLong = Convert.ToDouble(e.NewValues["HighLong"].ToString());//Convert.ToDouble(highLong.Text.Trim());
  
          context.SaveChanges();
      }
      else // copy row
      {
          WISSModel.Range range2 = new WISSModel.Range
          {
              //Description = txtDescription.Text.Trim(),
              CountryId = Convert.ToInt32(ddlCountry.SelectedValue),
              GeographicRegionId = Convert.ToInt32(ddlRegion.SelectedValue),
              BaseId = Convert.ToInt32(ddlFacility.SelectedValue),
              isDeleted = false
  
  
          };
          context.AddObject("Ranges", range2);
          context.SaveChanges();
      }
  
      GridViewRangeSetup.EditIndex = -1;
      LoadData();
  
  }
  </code></pre>