C# 尝试更新gridview中的行时,是否会出现以下错误?
我有一个C# 尝试更新gridview中的行时,是否会出现以下错误?,c#,sql,asp.net,C#,Sql,Asp.net,我有一个GridView,它显示来自我的SQL数据库的数据。用户可以选择更新或删除网格视图中的行。现在的情况似乎是,当用户单击要更新的行时,文本框会出现在您可以编辑的位置(直到此时为止,所有操作都按预期进行),但当单击“更新”时,它会崩溃,出现以下错误 我已经在下面的代码中指出了它崩溃的地方 System.NullReferenceException: 'Object reference not set to an instance of an object.' (... as System.
GridView
,它显示来自我的SQL数据库的数据。用户可以选择更新或删除网格视图中的行。现在的情况似乎是,当用户单击要更新的行时,文本框会出现在您可以编辑的位置(直到此时为止,所有操作都按预期进行),但当单击“更新”时,它会崩溃,出现以下错误
我已经在下面的代码中指出了它崩溃的地方
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();
}
受保护的void onrowUpdate(对象发送方,GridViewUpdateEventArgs e)
{
GridViewRow row=GridView1.Rows[e.RowIndex];
int customerId=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
字符串名称=(row.FindControl(“txtName”)作为文本框);
字符串email=(row.FindControl(“txtEmail”)作为文本框);
字符串license=(row.FindControl(“txtllicense”)作为文本框);
string query=“更新许可证集DisplayName=@DisplayName,EmailAddress=@EmailAddress,LicenseType=@LicenseType其中CustomerId=@CustomerId”;
string constr=ConfigurationManager.ConnectionString[“LicenseConnection”]。ConnectionString;
使用(SqlConnection con=newsqlconnection(cont))
{
使用(SqlCommand cmd=newsqlcommand(query))
{
cmd.Parameters.AddWithValue(“@CustomerId”,CustomerId);
cmd.Parameters.AddWithValue(“@DisplayName”,name);
cmd.Parameters.AddWithValue(“@EmailAddress”,email);
名称:
电子邮件地址:
国家:
我不知道为什么这会给我这个错误你的文本框名是“txtEmailAddress”不是“txtEmail”这个.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>