Click here to Skip to main content
15,892,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I declare a DataTable dt on PageLoad, or before it, as Global will the data in it be destroyed with every post back event

Example:-
C#
public partial class FAS_Reports_Forms_frmRpt_PurchReg : System.Web.UI.Page
{
// DataTable dt  =  new DataTable();
    Common cmn = new Common();
    Cls_Messages objMsg = new Cls_Messages();
    Cls_DateClass objDate = new Cls_DateClass();
    Consts consts = new Consts();
    protected void Page_Load(object sender, EventArgs e)
    {
        //cmn.CheckSession(Session["UName"]);
        if (!IsPostBack)
        {
            try
            {
               // divTitle.Visible = false;
                //if (cmn.chkfinyrdt(consts.localdate) == true)
                //{
                //    txttodt.text = consts.localdate;
                //}
                //else
                //{
                //    txttodt.text = cmn.pubenddt;
                //}
                hdPubYear.Value = Convert.ToString(Session["pubYearCode"]);
                txtFromDt.Text = cmn.pubStartDt;
                hid_unm.Value = Convert.ToString(Session["UName"]);
                Session["MonYr"] = null;
                Session["Report_Name"] = null;
                //Session["Report_Title"] = null;
                Session["dtAttend"] = null;
                string sql = "SELECT party.Id," +
                         "lEFT( party.PartyName +REPLICATE('…', 40), 40)+ ISNULL(CITY.Description,'') TTT " +
                         "FROM Party party " +
                         "Join City on CITY.Id = party.City_Id ORDER BY party.PartyName";
                cmn.fillDDLNormal(ddlAccount, sql, "id", "TTT");
                fillTitle();
            }
            catch (Exception)
            {
                return;
            }
        }
    }
Posted
Updated 29-Feb-12 21:19pm
v4

Yes, the data table values are getting destroyed that time,
you can use the state management for preserve data table. use session to preserve data table or data set.




Exploring Session in ASP.NET
 
Share this answer
 
v3
Comments
Photon_ 4-Mar-12 23:52pm    
but don't you think it will consume a lot memory space
Аslam Iqbal 5-Mar-12 1:10am    
Good link. 5
Yes, On each and every postback the global variables defined on the page load or before that will get reset. As on every postback page load event occurs and during load, if the current request is a postback, control properties are loaded with information recovered from view state and control state so if the viewstate for any variable is not saved before postback then the value in it will be lost.

You need to study the ASP.Net Page Life Cycle thoroughly.

Please refer to this link for more details:-

aspnet-page-life-cycle-events/

http://msdn.microsoft.com/en-us/library/ms178472.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900