-1

我有一个手动编程的网格视图。我拥有所有事件的所有方法,一切似乎都在工作,但 RowUpdating 事件。我收到此错误:“对象引用未设置为对象的实例。” 我无法弄清楚这里发生了什么。所有控件都输入了值。这是我的代码。我究竟做错了什么??

 protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            SqlCommand cmd = new SqlCommand();

            cmd.CommandText =    "update t_sizer_calibration " +
                                 "set " +
                                 "date = @date " +
                                 " time = @time, " +
                                 "name = @name, " +
                                 "lane = @lane, " +
                                 "carriers = @carrier, " +
                                 "rev = @REV, " +
                                 "new_tare = @n_tare, " +
                                 "current_tare = @ctare, " +
                                 "min = @min, " +
                                 "max = @max," +
                                 "sd = @sd, " +
                                 "wai = @wai, " +
                                 "bad_carriers = @badcar, " +
                                 "comments = @comments " + 
                                 "where calibration_id = @calibration_id"; 

            // define parameters
            cmd.Parameters.Add("@calibration_id", SqlDbType.VarChar).Value = glblID.Text;
            cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = gtxtDate.Text; 
            cmd.Parameters.Add("@time", SqlDbType.VarChar).Value = gtxtTime.Text;
            cmd.Parameters.Add("@name", SqlDbType.Int).Value = glblName.Text;
            cmd.Parameters.Add("@lane", SqlDbType.Int).Value = Convert.ToInt32(gtxtLane.Text);
            cmd.Parameters.Add("@carrier", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtCarriers.Text);
            cmd.Parameters.Add("@REV", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtREV.Text);
            cmd.Parameters.Add("@n_tare", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtN_Tare.Text);
            cmd.Parameters.Add("@ctare", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtC_Tare.Text);
            cmd.Parameters.Add("@min", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtMin.Text);
            cmd.Parameters.Add("@max", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtMax.Text);
            cmd.Parameters.Add("@sd", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtSD.Text);
            cmd.Parameters.Add("@wai", SqlDbType.Decimal).Value = Convert.ToDecimal(gtxtWAI.Text);
            cmd.Parameters.Add("@badcar", SqlDbType.Int).Value = Convert.ToInt32(gtxtBadCarriers.Text);
            cmd.Parameters.Add("@comments", SqlDbType.VarChar).Value = gtxtComments.Text; 

            cmd.CommandType = CommandType.Text;
            cmd.Connection = this.sqlConnection1;
            this.sqlConnection1.Open();
            //execute insert statement
            cmd.ExecuteNonQuery();
            this.sqlConnection1.Close();          
            //re-populate grid 
            fill_grid();
            GridView1.EditIndex = -1;
            GridView1.DataBind();

        }

这也是我的 rowupdated 方法:

 protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        fill_grid();
        //Reset the edit index.
        GridView1.EditIndex = -1;
        //Bind data to the GridView control.
        GridView1.DataBind();
    }
4

2 回答 2

0

我想到了。您必须使用 FindControl 来获取更新语句的新值。

如下所示:

 GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox comm = (TextBox)row.FindControl("gtxtComments");
            TextBox date = (TextBox)row.FindControl("gtxtDate"); ....... and so on. 
于 2012-10-16T23:13:41.930 回答
0

根据此页面,您绑定的行中的值将在事件参数的 、 和 属性中Keys返回OldValues给您。NewValues所以你根本不需要访问控制值。

我会尝试类似的东西:

cmd.Parameters.Add("@calibration_id", SqlDbType.VarChar).Value = e.NewValues["ID"];
cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = e.NewValues["date"];

...看看这是否能让你有所收获。

于 2012-10-16T17:35:52.890 回答