Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Null reference exception in gridview binding to text data

Null reference exception in gridview binding to text data

Scheduled Pinned Locked Moved ASP.NET
csharpvisual-studiowpfwcfdesign
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashutosh_karna
    wrote on last edited by
    #1

    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

    C H 2 Replies Last reply
    0
    • A ashutosh_karna

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A ashutosh_karna

        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

        H Offline
        H Offline
        httplover
        wrote on last edited by
        #3

        if(Session["DataViewExcel"] !=null){
        //dothing
        }

        living none stoping.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups