Null reference exception in gridview binding to text data
-
I am binding the same gridview to excel or text data based on user selection, and passing the dataview in session to different page for further analysis. It works fine when the user selects excel data; but in case user selects text data, it shows Null Reference exception in another page. Code is as follows: protected void Button_view_click( object sender, Eventargs e) { if(....=="Excel") { BindExcel(); } else(.... == "text") { BindTExt(); } GridView1.EnableViewState = true; dv = GridView1.DataSource as DataView; Session["DataViewExcel"] = dv; } In anothere page, i am using following code : Private void PageLoad() { DataView dv1 = new DataView(); DataTable dt1 = new DataTable(); dv1 = Session["DataViewExcel"] as DataView; dt1 = dv1.ToTable(); // Exception occurs here, only in case of text data } Stack Trace is as follows : Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] PreThesisSoftware.DiscreteDistribution.Page_Load(Object sender, EventArgs e) in C:\Users\Ashutosh\Documents\Visual Studio 2008\Projects\UploadFile\PreThesisSoftware\PreThesisSoftware\DiscreteDistribution.aspx.cs:23 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
-
I am binding the same gridview to excel or text data based on user selection, and passing the dataview in session to different page for further analysis. It works fine when the user selects excel data; but in case user selects text data, it shows Null Reference exception in another page. Code is as follows: protected void Button_view_click( object sender, Eventargs e) { if(....=="Excel") { BindExcel(); } else(.... == "text") { BindTExt(); } GridView1.EnableViewState = true; dv = GridView1.DataSource as DataView; Session["DataViewExcel"] = dv; } In anothere page, i am using following code : Private void PageLoad() { DataView dv1 = new DataView(); DataTable dt1 = new DataTable(); dv1 = Session["DataViewExcel"] as DataView; dt1 = dv1.ToTable(); // Exception occurs here, only in case of text data } Stack Trace is as follows : Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] PreThesisSoftware.DiscreteDistribution.Page_Load(Object sender, EventArgs e) in C:\Users\Ashutosh\Documents\Visual Studio 2008\Projects\UploadFile\PreThesisSoftware\PreThesisSoftware\DiscreteDistribution.aspx.cs:23 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
And which line is line 23 ? Have you used the debugger to step through this code ? What have you done to diagnose the problem ? The error means what it says, you're using an uninitialised object somewhere. Probably dv1 = Session["DataViewExcel"] as DataView;. The reason to use 'as' is that if the cast is not valid, you get null back. Using 'as' and not checking the return for null, is bad programming.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
I am binding the same gridview to excel or text data based on user selection, and passing the dataview in session to different page for further analysis. It works fine when the user selects excel data; but in case user selects text data, it shows Null Reference exception in another page. Code is as follows: protected void Button_view_click( object sender, Eventargs e) { if(....=="Excel") { BindExcel(); } else(.... == "text") { BindTExt(); } GridView1.EnableViewState = true; dv = GridView1.DataSource as DataView; Session["DataViewExcel"] = dv; } In anothere page, i am using following code : Private void PageLoad() { DataView dv1 = new DataView(); DataTable dt1 = new DataTable(); dv1 = Session["DataViewExcel"] as DataView; dt1 = dv1.ToTable(); // Exception occurs here, only in case of text data } Stack Trace is as follows : Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] PreThesisSoftware.DiscreteDistribution.Page_Load(Object sender, EventArgs e) in C:\Users\Ashutosh\Documents\Visual Studio 2008\Projects\UploadFile\PreThesisSoftware\PreThesisSoftware\DiscreteDistribution.aspx.cs:23 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627