ASP .NET RowUpdating GridView控件故障 [英] ASP .NET RowUpdating GridView Troubles

查看:217
本文介绍了ASP .NET RowUpdating GridView控件故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用 RowUpdating 方法的麻烦。我的 GridView控件连接到我们的本地SQL Server,和我试图更新数据。这里是code从MSDN中的 RowUpdating 方法。

 保护无效TaskGridView_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
        //从session对象检索表。
        DataTable的DT =(数据表)会议[TaskTable];        //更新值。
        GridViewRow行= GridView1.Rows [e.RowIndex]
        dt.Rows [row.DataItemIndex] [ID] =((文本框)(row.Cells [1] .Controls [0]))文本。
        dt.Rows [row.DataItemIndex] [说明] =((文本框)(row.Cells [2] .Controls [0]))文本。
        dt.Rows [row.DataItemIndex] [IsComplete] =((复选框)(row.Cells [3] .Controls [0]))选中。        //重置编辑索引。
        GridView1.EditIndex = -1;        //数据绑定到GridView控件。
        BindData();}

我得到这个错误:


  

System.NullReferenceException:对象不设置到对象的实例。



解决方案

试试这个code一次。

 保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
    GridViewRow行=(GridViewRow)GridView1.Rows [e.RowIndex]
    INT ID = Int32.Parse(GridView1.DataKeys [e.RowIndex] .Value.ToString());
    文本框TNAME =(文本框)row.FindControl(南);
    文本框tques =(文本框)row.FindControl(阙);
    MySqlCommand的CMD =新的MySqlCommand(更新设置考试= NAME1 @名称,疑问句= @疑问句其中id = @id,CON);
    cmd.Parameters.Add(@ ID,MySqlDbType.Int16).value的= ID;
    cmd.Parameters.Add(@名,MySqlDbType.VarChar,30).value的= tname.Text.Trim();
    cmd.Parameters.Add(@疑问句,MySqlDbType.VarChar,40).value的= tques.Text.Trim();
    con.Open();
    cmd.ExecuteNonQuery();
    GridView1.EditIndex = -1;
    绑定();
}

I'm having trouble with the RowUpdating Method. My GridView is connected to our local SQL Server, and I'm trying to update the data. Here is the code for the RowUpdating Method from MSDN.

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

}

I get this error:

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

解决方案

try this code once.

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

这篇关于ASP .NET RowUpdating GridView控件故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆