简体   繁体   中英

Data table in C#.net

I have a problem related to DataTable in .net. i am trying to fill datatable on page_load .that part is ok no issue datatable correctly filled up with data. but I noticed that when I clicked button on page... it again try to fill datatable which just a time-consuming task... I want datatable should fill once only ,when , when site open in browser .. not at every click ... because I have a large amount of data in table so it takes time to load in datatable.. plz help me out ... here is my code:

protectd void Page_Load(object sender, EventArgs e)
{
        cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

      //  if (this.IsPostBack == true)
        {
            dr1 = TableUtoP(); 
            dtWordsList.Load(dr1);
       }
}

OleDbDataReader TableUtoPunj(String ArrWord)
    {
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }
        cnn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "SELECT U, P from UtoP";
        cmd.Connection = cnn;
        OleDbDataReader dr = cmd.ExecuteReader();
        return dr;
    }

You need to check if the page is posting back or not, like this:

protected void Page_Load(object sender, EventArgs e)
{
    cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

    if(!IsPostBack)
    {
        // This will only happen when the page is first loaded, as the first time is not considered a post back, but all others are
        dr1 = TableUtoP(); 
        dtWordsList.Load(dr1);
    }
}

Only fill the dataTable if it's not a PostBack...

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //TO DO: Make the call to fill the data table here
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM