locked
Gridview DataBind Function: Object reference not set to an instance of an object RRS feed

  • Question

  • User-506078843 posted

    HI All,

    I am facing a strange issue with Gridview control i.e. wherever I am calling <g class="gr_ gr_81 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="81" data-gr-id="81">.databind</g> method. It throws "Object reference not set to an instance of an object" exception in some cases. I am trying to bind it using a <g class="gr_ gr_178 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="178" data-gr-id="178">datatable</g>. Gridview is not dynamically created.

     grdPNParent.DataSource = dt;
     grdPNParent.DataBind();

    If I comment this line highlighted in Red. <g class="gr_ gr_272 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="272" data-gr-id="272">Error</g> goes away.

    Thanks in Advance

    Tuesday, March 5, 2019 12:45 PM

All replies

  • User475983607 posted

    The dt variable is null.  I recommend using the Visual Studio debugger to set a breakpoint and single stepping through your code to find the bug.

     grdPNParent.DataSource = dt;
     grdPNParent.DataBind();

    https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017

    Tuesday, March 5, 2019 1:15 PM
  • User-1716253493 posted

    How you get grdParent? Is it directly to the control?

    Wednesday, March 6, 2019 2:53 AM
  • User-506078843 posted

    Hi,

    Thanks for your time looking into it. I figured out the cause. a row_databound in nested (child) grid was causing the issue for in some scenarios.

    Thanks

    Wednesday, March 6, 2019 9:20 AM
  • User61956409 posted

    Hi rizwanaarif,

    It throws "Object reference not set to an instance of an object" exception

    Normally, if you are trying to access member fields/properties or function on an object reference that points to null, it would cause the issue.

    a row_databound in nested (child) grid was causing the issue for in some scenarios

    As mgebhard mentioned, if you try to set breakpoints within your code logic in code behind methods/events and debug the code step by step, it will be easy find the code snippet that causes the issue.

    With Regards, 

    Fei Han

    Thursday, March 7, 2019 1:53 AM