C# 行更新命令时Gridview字段为空
我目前正在使用两个绑定到不同sqldatasources的GridView,它们都使用文本框为每一行接受一个值。当我去编辑然后更新行中的其他字段时,文本框填充的值变成空值。它之前工作正常,但在参数化值以及添加RowCancelingEdit方法等之后,它已停止 是什么使“Name”值为空C# 行更新命令时Gridview字段为空,c#,asp.net,gridview,C#,Asp.net,Gridview,我目前正在使用两个绑定到不同sqldatasources的GridView,它们都使用文本框为每一行接受一个值。当我去编辑然后更新行中的其他字段时,文本框填充的值变成空值。它之前工作正常,但在参数化值以及添加RowCancelingEdit方法等之后,它已停止 是什么使“Name”值为空 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridView
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
String qry = "UPDATE [PlayerStatistics] SET [Name] = @Name, [Apps] = @Apps, [Minutes] = @Minutes, [Goals] = @Goals, [Assists] = @Assists, [Yellows] = @Yellows, [Reds] = @Reds WHERE [PlayerID] = @original_PlayerID";
using (SqlCommand cmd = new SqlCommand(qry, con))
{
cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar);
cmd.Parameters.AddWithValue("@Apps", SqlDbType.Int);
cmd.Parameters.AddWithValue("@Minutes", SqlDbType.Int);
cmd.Parameters.AddWithValue("@Goals", SqlDbType.Int);
cmd.Parameters.AddWithValue("@Assists", SqlDbType.Int);
cmd.Parameters.AddWithValue("@Yellows", SqlDbType.Int);
cmd.Parameters.AddWithValue("@Reds", SqlDbType.Int);
cmd.Parameters.AddWithValue("@original_PlayerID", SqlDbType.Int);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
标记:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PlayerID" DataSourceID="SqlDataSource1" EmptyDataText="Please enter player(s) into the table." OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting1" Width="100%" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Apps" HeaderText="Apps" SortExpression="Apps" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Minutes" HeaderText="Minutes" SortExpression="Minutes" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Goals" HeaderText="Goals" SortExpression="Goals" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Assists" HeaderText="Assists" SortExpression="Assists" ControlStyle-Width="100%" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Yellows" HeaderText="Yellows" SortExpression="Yellows" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Reds" HeaderText="Reds" SortExpression="Reds" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:TemplateField HeaderText ="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="BtnEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit" />
<span onclick="return confirm ('Are you Sure?')" >
<asp:LinkButton ID="BtnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"/>
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CausesValidation="false" CommandName="Update" ConflictDetection="OverwriteChanges" Text="Update" />
<asp:Button ID="BtnCancel" runat="server" CausesValidation="false" CommandName="Cancel" ConflictDetection="OverwriteChanges" Text="Cancel" />
</EditItemTemplate>
<ControlStyle Width="100%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
AddWithValue
的第二个参数是值,而不是数据类型。由于Name
字段在GridView中不可编辑,因此可以将其从更新查询和参数中删除。您的事件处理程序如下所示:
using (SqlCommand cmd = new SqlCommand("UpdatePlayer", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PlayerID", GridView1.DataKeys[e.RowIndex].Values["PlayerID"]);
cmd.Parameters.AddWithValue("@Apps", e.NewValues["Apps"]);
cmd.Parameters.AddWithValue("@Minutes", e.NewValues["Minutes"]);
...
}
您还应该将数据绑定到GridView,并在rowUpdate
事件处理程序的末尾重置EditRowIndex
:
GridView1.EditIndex = -1;
BindData();
我已更改代码以适应e.NewValues,它返回了以下错误:“参数化查询”(@Name nvarchar(4000),@Apps nvarchar(4000),@Minutes nvarchar(400)需要未提供的参数“@Name”。“BindData”是否指GridView1.DataBind()当我使用sqldatasourceCan时,您可以在问题中包含GridView的标记吗?我已经将其添加到Post中了哪个字段包含名称?哪个字段包含应用程序?好的,我想我最终修复了它,必须恢复到我最初发布的内容,并使“name”变量不是只读的。您知道为什么会发生这种情况吗?非常感谢您的帮助努力
GridView1.EditIndex = -1;
BindData();