SHeinema
SHeinema

Reputation: 634

Persisting a Variable across Requests

I realize this may be a simple problem, but I am new to ASP.net (C#) and am having an issue with a method in which I pass a 'Ride' object that was obtained from an sql database through a LINQ-to-entities statement. I attempt to set another global variable to the value of the Ride.identity attribute (which is a long), but in the next method, when I attempt to use this value, the new value has not persisted. Any thoughts? If there is some post-back that I am missing that reinitializes this variable, is there a way to save it? Thanks.

private void displayRide(Ride ride, int carNum)
{
    if (ride != null) 
    {
        ride.AssignedCar = carNum;
        ride.Status = "EnRoute";
        id_ridePendingConfirm = ride.identity; //<----THE PROBLEM IS HERE!
        myEntities.SaveChanges();

        RideToAssignDV.DataSource = new List<Ride> {ride};
        RideToAssignDV.DataBind();                    
    } 
    else 
    {
        //TODO: Redirect to error.aspx
        RideToAssignDV.DataSource = null;
        RideToAssignDV.DataBind(); 
    }
}

Upvotes: 2

Views: 6066

Answers (3)

Tim M.
Tim M.

Reputation: 54387

See this question and my answer for multiple methods of storing data between executions of a page. In brief, you need to store the value somewhere between postbacks. ASP.NET (and .NET in general) provide a variety of tools for doing so, but nothing is done automatically between requests. The page is created, executed, and destroyed with every request.

Upvotes: 2

Mario J Vargas
Mario J Vargas

Reputation: 1195

Store the value in ViewState. For example:

ViewState["RideId"] = ride.identity;

When you go and use it in the line in your code, you would need to do this:

id_ridePendingConfirm = (long)ViewState["RideId"];

But be careful. Since ViewState[key] returns an object, you will need to make sure it isn't a null reference, or else you'll receive an InvalidCastException.

I normally tell my my peers with less experience to create a protected property that will store this value and persist it in ViewState, as follows:

protected const string RideIdViewStateKey = "CurrentRideId";

protected long CurrentRideId
{
    get
    {
        object o = ViewState[RideIdViewStateKey];
        return (null == o)? -1 : (long)o;
    }

    set
    {
        ViewState[RideIdViewStateKey] = value;
    }
} 

Then in your code, do this:

// Assignment before postback so that you can preserve the state:
CurrentRideId = ride.identity;

// After postback in the method you have above:
id_ridePendingConfirm = CurrentRideId;

Now, since I don't know what your code should do when no Ride identity is available, I decided for -1, but it depends on what your code actually needs.

I do not recommend Session state for your scenario because this apparently needs to persist between page postbacks, not for the entire duration of the user's session. Also be careful with how much information you store in ViewState because it can easily be abused.

For more information on ViewState and Session State see:

Upvotes: 6

SLaks
SLaks

Reputation: 887797

Variables and fields do not persist across postbacks.

You need to use session state.

Upvotes: 3

Related Questions