gpt4 book ai didi

c# - ASP .NET RowUpdating GridView 问题

转载 作者:行者123 更新时间:2023-11-30 14:14:42 25 4
gpt4 key购买 nike

我在使用 RowUpdating 方法时遇到问题。我的 GridView 已连接到我们的本地 SQL Server,我正在尝试更新数据。以下是来自 MSDN 的 RowUpdating 方法的代码。

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["TaskTable"];

//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

//Reset the edit index.
GridView1.EditIndex = -1;

//Bind data to the GridView control.
BindData();

}

我收到这个错误:

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

最佳答案

尝试此代码一次。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tname = (TextBox)row.FindControl("nam");
TextBox tques = (TextBox)row.FindControl("que");
MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
bind();
}

关于c# - ASP .NET RowUpdating GridView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com