Question

I want to use viewstate variable's value which is saved in one page on another page. But while doing so it shows NullReferenceException. I am new to ASP.net. Please Help me out.

in register.aspx

ViewState("name")=textbox1.text

in success.aspx

dim a as string

a=ViewState("name").toString

Was it helpful?

Solution

Use Session["name"] = textbox1.text...

If I am not mistaken, you don't have direct control or should have direct control over viewstate.

OTHER TIPS

ViewState is the technique to persist state across postbacks and is lost when you load another page. therefore you will need to use another way to send the data to the next page. Read up more here on MSDN

The common choices for sending data across pages include:

  1. QueryString
  2. By setting up the previous page property
  3. Session variables

ViewState is restricted to a page, so it cannot be used on another page. The reason is that ViewState is serialized in the page output in a hidden field that is transferred to the client and back to the server upon a postback.
If you want to transfer data to another pages you have several other alternatives:

  • As @AnastacioGianareas also pointed out, Session memory is one of them. It is located on the server, but sessions will expire after a certain time of user inactivity. Being located on the server, it reduces application scalability if you have many users and lots of memory allocated in the session.
  • Hand over the data as a query string parameter to the other page, e.g. redirecting to "Success.aspx?name=". This will work for smaller amounts of data that you can put into the query string (e.g. ids or names). It is important that a client can also request Success.aspx with that query string parameter, so it should be reserved for uncritical data and be validated carefully.
  • Use a cookie to transfer it to the client and back again.

This link gives a good overview of the alternatives.

From MSDN:

View state is the method that the ASP.NET page framework uses by default to preserve page and control values between round trips. When the HTML for the page is rendered, the current state of the page and values that need to be retained during postback are serialized into base64-encoded strings and output in the view state hidden field or fields.

Options to persist data between pages are varied and depend upon your requirements, this page on MSDN describes each of these options and the considerations you should make.

For your requirement either QueryStrings, SessionState seem like the best solution.

As a side note, always validate your 'State' variables (regardless of which method you chose) to ensure that they aren't null and are of the expected type. i.e. all these values will be stored as Strings, if you are going to use a variable as another type (an int, double etc) you should make sure this is valid. Also, should you go down the querystring route, take into account any security considerations as users may / will modify these values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top