DataBind and GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zhshqzyc
    New Member
    • Feb 2008
    • 9

    DataBind and GridView

    Hi,

    I am going to create two aspx pages.
    The first page will display the whole content of a table which is from a DataSet.
    Code:
     
    DataSet myDataSet = new DataSet(); 
     try
            {
                myConnection.Open();
                // create the data
                GenerateDataSet(myDataSet, myConnection);
            }
            finally
            {
                myConnection.Close();
                // bind each to table to a grid
                GridView1.DataSource = myDataSet.Tables["Weather"];
            }
            // databind the page
            GridView1.DataBind();
    The second page will only display few selected columns of the table.
    My questions are
    1) How can the contents in the second page be binded with the datasource?
    Suppose I don't want to create the second table.
    2) How can I pass the data source from the first page to the second page?


    Thanks.
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    If possible execute a fresh query on the second page...or pass a primary key information in Querysting by which you can retrieve required records........ .........
    On the otherhand you can keep your dataset to Session or Cache variable............but you have to consider many aspects of storing a dataset to session or cache....

    here is some point ::
    *************** *************** ****

    If you decide to save a dataset between round trips, you must decide where to keep it. This issue is the
    standard one of state maintenance in Web Forms pages — where do you store information you want to
    preserve between round trips?
    You have two options:
    On the server, save the dataset in Session state, Application state, or using a cache.
    On the client — that is, in the page — save the dataset using view state or by putting the data into your own hidden field. ( View state is also implemented using a hidden field. )

    Storing the dataset on the server uses server resources. If you store too much data ( a large dataset, or many users storing small datasets ), it can affect server performance and scalability. Using a cache can partly offset this problem, because the cache manager
    will discard the dataset if the server needs memory or if cached data expires. But because the dataset is not guaranteed to be in the cache, you must add logic to your page to check that the dataset is available in the cache; if not, you must recreate it and put a copy back in the cache.
    Storing data in the page means that you do not need server resources to save the data. However, the data becomes part of the HTML stream of the page. If the dataset is large, it can substantially affect the
    time it takes for the page to load into the user's browser and to post the page back to the server.
    TIP: Always try to keep the size of a dataset to a minimum by filling it with only the records you need.
    No matter where you decide to store the dataset, you must add logic to your page to save it and restore it at the appropriate time. The following example shows a typical way to store and restore a dataset in Session state. The dataset dsCustomers1 is an instance of the dataset class dsCustomers. Note that the dataset is stored in Session state as type Object. When restoring the dataset from Session state, you must cast it from Object back to a dataset class.

    [CODE=css]private void Page_Load ( object src, EventArgs e )
    {
    if ( Page.IsPostBack )
    {
    dsCustomers1 = ( dsCustomers ) Session [ "myDsCustom ers" ];
    }
    else
    {
    if ( Session [ "myDsCustom ers" ] == null )
    {
    oleDbDataAdapte r1.Fill ( dsCustomers1 );
    Session [ "myDsCustom ers" ] = dsCustomers1;
    }
    }
    }[/CODE]

    Comment

    • zhshqzyc
      New Member
      • Feb 2008
      • 9

      #3
      Thanks for your reply. I will study on the skills of dataset in Session state.
      It's brand new to me.

      Comment

      Working...