c# - 尝试更新 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>
不知道为什么这给了我这个错误
解决方案
您的 Textbox-Name 是"txtEmailAddress"而不是"txtEmail"。
推荐阅读
- python-2.7 - 将 Python 2 迁移到 Python 3
- vue.js - 如何优化 Vue 应用程序的生产版本
- java - 使用 Cygwin64 在 Windows 上构建 hsdis-amd64.dll 时对“dlopen”、“dlclose”、“dlsym”和“dlerror”的未定义引用
- c# - 如果我不选择第一个单选按钮,则为 null
- javascript - 如何通过 express 发送 API 响应
- c++ - Boost的Serialization如何判断多态对象的运行时类型
- flutter - flutter_bloc 的 BlocBuilder 能否避免重建 Widget 的未更改部分?
- python-3.7 - 使用漂亮的汤在网站上抓取数据时,我面临 HTTP 404 和 503 错误
- batch-file - 如何使上下文菜单上的批处理脚本在 UNC 路径上工作?
- python - Python中的阴影模式测试