我有一個手動編程的gridview。我擁有所有事件的所有方法,一切似乎都在工作,但RowUpdating事件。我得到這個錯誤:「對象引用未設置爲對象的實例。」我無法弄清楚這裏發生了什麼。所有的控件都有輸入的值。這是我的代碼。我究竟做錯了什麼??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();
}
併線引發錯誤?您可以複製和粘貼異常的堆棧軌道,因爲這肯定會有所幫助。 –
你確定'sqlConnection1'不是null? –
sqlConnection1是頁面的全局連接字符串。我在這個頁面中使用了幾次。 – briskovich