Ellen Sellers
Ellen Sellers

Reputation: 191

When trying to update a row in gridview it give the below error?

I have a GridView that displays a data from my SQL database. Yhe user has the options to update or delete the row inside the GridView. What seems to happen now is that when the user clicks on the row to update the text boxes appear where you can edit (up until this point all works as expected) but when clicking on update it crashes with the below error.

I have pointed out in the code below where the line is where it crashes.

System.NullReferenceException: 'Object reference not set to an instance of an object.'

(... as System.Web.UI.WebControls.TextBox) returned null.

This is the code I am currently using: Code Behind

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        int customerId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        string name = (row.FindControl("txtName") as TextBox).Text;
        string email = (row.FindControl("txtEmail") as TextBox).Text;
        string license = (row.FindControl("txtLicense") as TextBox).Text;
        string query = "UPDATE License SET DisplayName=@DisplayName, EmailAddress=@EmailAddress, LicenseType=@LicenseType WHERE CustomerId=@CustomerId";
        string constr = ConfigurationManager.ConnectionStrings["LicenseConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Parameters.AddWithValue("@CustomerId", customerId);
                cmd.Parameters.AddWithValue("@DisplayName", name);
                cmd.Parameters.AddWithValue("@EmailAddress", email); <-- Error here
                cmd.Parameters.AddWithValue("@LicenseType", license);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        GridView1.EditIndex = -1;
        this.BindGrid();
    }

and:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
        DataKeyNames="CustomerId" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" PageSize="3"
        OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added."
        Width="450">
      <Columns>
        <asp:TemplateField HeaderText="Display Name" ItemStyle-Width="150">
          <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Text=''
              <%# Eval("DisplayName") %>'></asp:Label>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox ID="txtName" runat="server" Text=''
              <%# Eval("DisplayName") %>' Width="140"></asp:TextBox>
          </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email Address" ItemStyle-Width="150">
          <ItemTemplate>
            <asp:Label ID="lblEmailAddress" runat="server" Text=''
              <%# Eval("EmailAddress") %>'></asp:Label>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox ID="txtEmailAddress" runat="server" Text=''
              <%# Eval("EmailAddress") %>' Width="140"></asp:TextBox>
          </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="License Type" ItemStyle-Width="150">
          <ItemTemplate>
            <asp:Label ID="lblLicense" runat="server" Text=''
              <%# Eval("LicenseType") %>'></asp:Label>
          </ItemTemplate>
          <EditItemTemplate>
            <asp:TextBox ID="txtLicenseType" runat="server" Text=''
              <%# Eval("LicenseType") %>' Width="140"></asp:TextBox>
          </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true"
            ItemStyle-Width="150" />
      </Columns>
    </asp:GridView>
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
      <tr>
        <td style="width: 150px">
          Name:<br />
          <asp:TextBox ID="txtName" runat="server" Width="250" />
        </td>
        <td style="width: 150px">
          Email Address:<br />
          <asp:TextBox ID="txtEmail" runat="server" Width="250" />
        </td>
        <td style="width: 150px">
          Country:<br />
          <asp:TextBox ID="txtLicense" runat="server" Width="140" />
        </td>
        <td style="width: 150px">
          <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
        </td>
      </tr>
    </table>
  </ContentTemplate>
</asp:UpdatePanel>

Am not sure why this is giving me this error

Upvotes: 0

Views: 75

Answers (1)

kara
kara

Reputation: 3455

Your Textbox-Name is "txtEmailAddress" not "txtEmail".

Upvotes: 1

Related Questions