我有一个手工编程的gridview。我有所有事件的所有方法,一切似乎都在工作,但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();
}
答案 0 :(得分:0)
根据this page,您绑定的行中的值将在事件的Keys
,OldValues
和NewValues
属性中返回给您ARGS。因此,您根本不需要访问控制值。
我会尝试类似的事情:
cmd.Parameters.Add("@calibration_id", SqlDbType.VarChar).Value = e.NewValues["ID"];
cmd.Parameters.Add("@date", SqlDbType.VarChar).Value = e.NewValues["date"];
......看看是否能让你随处可见。
答案 1 :(得分: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.