Ask the Expert

Mysterious error when updating a DataTable-based datagrid

I am getting an error when trying to do an Update to a datagrid based on a DataTable: "Object reference not set to an instance of an object" Any help or suggestions are greatly appreciated!
Your problem is with Session State. When your application sends a web page to the browser the server destroys your application variables. So your dtglobal and dvglobal objects will no longer be instantiated on a subsequent postback.

One way to overcome this problem is to save these objects in session state and retrieve them in Page_Load.

You can add the following lines of code to the end of your "btCreateObjs_Click" method to save the objects in session state:

Me.Session.Add("dtGlobal", dtglobal) Me.Session.Add("dvGlobal", dtglobal)

And, you can add the following lines of code to the Page_Load method to retrieve the objects from session state:

dtglobal = DirectCast(Me.Session("dtGlobal"), DataTable) dvglobal = DirectCast(Me.Session("dvGlobal"), DataView

You need to remember that when the web page is on the client, you are no longer on the server. The only objects retained on the server are those in session state or cache.

This was first published in July 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: