Why isn't the global DataTable declaration retaining its value?

I have an aspx page with a gridview. In the page load event, I load the datatable with all data, for example:

HistoricalPricing historicalPricing = new HistoricalPricing();
DataTable dtHistoricalPricing = new DataTable();

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dtHistoricalPricing = historicalPricing.GetAuctionData();
        }
    }

      

The above information loads data into a data feed. I also have a list containing the list of auctions. When I click on the auction, I use the RowFilter on the DataView to display the Gridview with the selected data, but the DataTable seems to be losing its value and I can't figure out why. Here's the code below:

protected void lstAuctions_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView dvPricing = new DataView(dtHistoricalPricing); // Loses Value
        dvPricing.RowFilter = "Auction = 1"; //Hard-Coded for Test
        gvPricing.DataSource = dvPricing.ToTable();
        gvPricing.DataBind();
    }

      

+1


source to share


4 answers


Each time you postback, you are dealing with a new instance of your page class. This also means a new data object.



If you really want to keep it between postbacks (and make sure you take into account the memory implications for this when you can have 1000 people hitting this web server at the same time), you can put the data in a Session, ViewState, or elsewhere that is saved in the state.

+2


source


I think I figured it out because when I click on the ListBox it does a postback and I only load the data on the first page load? If this is correct, I think I have answered my own question.



I was putting data in Session after loading it on first page load and that seemed to fix my problem. Not sure if this is the best way.

+1


source


For an answer to your own question. You instantiate a new object for every page load, so when the postback is displayed in the list, your code is dealing with a different object.

It would be better to declare the object globally and then create it in Postback code like:

DataTable dtHistoricalPricing = null;

      

...

if (!Page.IsPostBack)
{
   if (dtHistoricalPosting == null)
   {
       //shouldn't need to do a new dtHistoricalPricing as the method below is returning a new instance?
       dtHistoricalPricing = historicalPricing.GetAuctionData();
   }
}

      

+1


source


It is difficult to preserve values ​​between requests in ASP.NET. The most reliable way is to put it in the ViewState, but it will send the whole thing to the client and back, so you shouldn't put a lot of data there. Session is an alternative, but it can become a problem when a user opens multiple windows with your page in the same session. Then there is also the application state and cache, but these are shared between ALL requests (regardless of the user). Also, if you have a web farm, the values ​​there are local to each server. Moreover, IIS can spawn multiple ASP.NET processes on the same machine, each with its own application state. The same can be said for static variables.

+1


source







All Articles