Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7091165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:09:00+00:00 2026-05-28T08:09:00+00:00

I realize this may be a simple problem, but I am new to ASP.net

  • 0

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(); 
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:09:01+00:00Added an answer on May 28, 2026 at 8:09 am

    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:

    • Undestanding View State (2004)
    • ViewState Property (.NET 4.0)
    • ASP.NET State Management Recommendations
    • ASP.NET Session State Overview
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this may be a very simple question but I need to know
I realize something like this has been asked, but this may be a little
This seems like a very simple problem, but I cannot get a scrollbox to
Hello I have a simple wcf service like this, with a test method which
NOTE : In hindsight, I realize this may seem like a stupid question, but
I realize this question may sound dumb, but just bear with me. I built
I realize this would violate convention, but I'm curious to know if you can
I realize this perhaps a naive question but still I cant figure out how
I realize this is more of a hardware question, but this is also very
I realize this is a rather odd request, but I was wondering if anyone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.