I am facing a strange issue with Gridview control i.e. wherever I am calling .databind 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 datatable. Gridview is not dynamically
created.
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.
rizwanaarif
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
2 Posts
Gridview DataBind Function: Object reference not set to an instance of an object
Mar 05, 2019 12:45 PM|rizwanaarif|LINK
HI All,
I am facing a strange issue with Gridview control i.e. wherever I am calling .databind 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 datatable. Gridview is not dynamically created.
grdPNParent.DataSource = dt; grdPNParent.DataBind();
If I comment this line highlighted in Red. Error goes away.
Thanks in Advance
All-Star
53131 Points
23682 Posts
Re: Gridview DataBind Function: Object reference not set to an instance of an object
Mar 05, 2019 01:15 PM|mgebhard|LINK
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.
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
All-Star
52683 Points
15724 Posts
Re: Gridview DataBind Function: Object reference not set to an instance of an object
Mar 06, 2019 02:53 AM|oned_gk|LINK
How you get grdParent? Is it directly to the control?
Suwandi - Non Graduate Programmer
None
0 Points
2 Posts
Re: Gridview DataBind Function: Object reference not set to an instance of an object
Mar 06, 2019 09:20 AM|rizwanaarif|LINK
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
All-Star
40565 Points
6233 Posts
Microsoft
Re: Gridview DataBind Function: Object reference not set to an instance of an object
Mar 07, 2019 01:53 AM|Fei Han - MSFT|LINK
Hi rizwanaarif,
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.
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