我有这个错误,但任何一个错误都无法找到该错误 [英] I have error in this but am unable to find that error any one fineout that error
问题描述
错误是.......
对象引用未设置为对象的实例.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例.
源错误:
第29行:}
第30行:
第31行:dr = dt.NewRow();
第32行:dr [0] = TextBox1.Text;
第33行:dr [1] = TextBox2.Text;
我的代码是第一页.........
error is.......
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 29: }
Line 30:
Line 31: dr = dt.NewRow();
Line 32: dr[0] = TextBox1.Text;
Line 33: dr[1] = TextBox2.Text;
my code is 1st page.........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));
for (int i = 0; i < 1; i++)
{
dt = (DataTable)Session["CurrentData"];
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text;
dt.Rows.Add(dr);
Session["CurrentData"] = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Default3.aspx");
}
private void clearfields()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
}
第二页是...........
2nd page is...........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource =Session["CurrentData"];
GridView1.DataBind();
}
}
推荐答案
1.Session["CurrentData"]
;使用前应检查是否为空.这个家伙很可能是null的,导致dt
变为null,当您在dt
前面放置.newrow
的那一刻,异常就要来了.
1.Session["CurrentData"]
;should be checked for null before being used. Most probably this guy is null causingdt
to become null and the moment you put a.newrow
in front ofdt
the exception is coming.
嘿
替换以下代码
Hey
replace following code
for (int i = 0; i < 1; i++)
{
dt = (DataTable)Session["CurrentData"];
}
由
if(Session ["CurrentData"]!= null)
dt =(DataTable)Session ["CurrentData"];
并在页面加载
by
if(Session["CurrentData"]!=null)
dt = (DataTable)Session["CurrentData"];
and in page load
if(Session["CurrentData"]!=null)
{
GridView1.DataSource =Session["CurrentData"];
GridView1.DataBind();
}
else
GridView1.DataSource=null;
best luck
你好,
导致此错误的原因是因为dt数据表对象变为null,这就是为什么它显示错误对象引用未设置为对象实例的原因.
因此,要解决此错误,请检查会话对象的值(如果该对象即将到来),那么您将获得解决方案.
问候
安迪.
hi there,
this error due to because you dt data table object become null thats why it show error Object reference not set to an instance of an object.
so to solve this error please check the value of your session object if it coming then you got your solution.
Regards
Andy.
这篇关于我有这个错误,但任何一个错误都无法找到该错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!