本文介绍了对象引用未设置为ASP.net中的对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象引用未设置为对象的实例.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例.

源错误:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 68:             TextBox txtEmail = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtEmail");
Line 69:             con.Open();
Line 70:             SqlCommand cmd = new SqlCommand("update Employee_Details set PrinterID='" + txtPrinterID.Text + "',Name='" + txtName.Text + "',Department='" + txtDepartment.Text + "',Description='" + txtDescription.Text + "',Email='" + txtEmail.Text + "' where Id=" + Id, con);
Line 71:             cmd.ExecuteNonQuery();
Line 72:             con.Close();<pre>



第70行为红色.



line 70 is in red.

推荐答案



protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           int Id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
           TextBox txtPrinterID = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtPrinterID");
           string username = gvDetails.DataKeys[e.RowIndex].Values["Username"].ToString();
           TextBox txtName = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtName");
           TextBox txtDepartment = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDepartment");
           TextBox txtDescription = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDescription");
           TextBox txtEmail = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtEmail");
           con.Open();
           SqlCommand cmd = new SqlCommand("update Employee_Details set PrinterID='" + txtPrinterID.Text + "',Name='" + txtName.Text + "',Department='" + txtDepartment.Text + "',Description='" + txtDescription.Text + "',Email='" + txtEmail.Text + "' where Id=" + Id, con);
           cmd.ExecuteNonQuery();
           con.Close();
           lblresult.ForeColor = Color.Green;
           lblresult.Text = username + " Details Updated successfully";
           gvDetails.EditIndex = -1;
           BindEmployeeDetails();
       }
       protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           gvDetails.EditIndex = -1;
           BindEmployeeDetails();
       }<pre>




我的代码是这样的.是不是错了?




my code is this. is it wrong?


这篇关于对象引用未设置为ASP.net中的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 07:03