对象引用未设置为ASP.net中的对象实例 [英] Object reference not set to an instance of an object in ASP.net

查看:65
本文介绍了对象引用未设置为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.

推荐答案

首先,我想建议始终习惯使用Parameterized Query.关于您的问题,可能未建立CONNECTION 或参数ID 为空.

谢谢
First of all I would like to suggest that Always make a habbit to use Parameterized Query. Regarding to your issue it may possible that CONNECTION is not established or the parameter ID is null.

Thanks


当您尝试访问空对象的成员时,通常会发生此错误.
尝试调试代码并找出空对象.

原因可能是此行返回null-gvDetails.Rows[e.RowIndex].FindControl("txtEmail").
尝试访问该对象的.Text属性时,可能会出错.
This error normally occurs when you are trying to access a member of an object that is null.
Try debugging your code and tracing out the object that is null.

One cause could be this line returns null - gvDetails.Rows[e.RowIndex].FindControl("txtEmail").
When you try and access the .Text property of this object you could be getting an error.


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中的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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