首页 > 解决方案 > 尝试更新 gridview 中的一行时,会出现以下错误?

问题描述

我有一个GridView显示来自我的 SQL 数据库的数据。用户可以选择更新或删除GridView. 现在似乎发生的事情是,当用户单击行以更新文本框时,您可以在其中进行编辑(直到此时一切都按预期工作),但是当单击更新时,它会因以下错误而崩溃。

我在下面的代码中指出了该行崩溃的位置。

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

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

这是我目前正在使用的 代码:背后的代码

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();
    }

和:

<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>

不知道为什么这给了我这个错误

标签: c#sqlasp.net

解决方案


您的 Textbox-Name 是"txtEmailAddress"而不是"txtEmail"


推荐阅读