title: Save data records and use it again without querying again into database
body: <p>I have a two control, which are a Search button and Export button. The user will 'Search' the data and the data will be inserted into DataTable and bound to the GridView or Repeater, and then when user click Export, the DataTable will be passed into the class that will generate an Excel file using the data inside DataTable.</p>
  
  <p>In my current code, the Search button and Export button both will query to fetch data from database. But what I need is only Search button that will query, and store the data into a DataTable or something, and when Export button is clicked it will use the earlier data to generate the Excel file.</p>
  
  <p>Here is my current code:
  Report.aspx
      
          
          </p>
  
  <pre><code>    &lt;asp:GridView ID="GridView1" runat="server"&gt;
          &lt;Columns&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Report.aspx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
          {
              /* Page load */
          }
  
  protected void btn_search_Click(object sender, EventArgs e)
          {
              DataTable dt = GetData();
              GridView1.DataSource = dt;
              GridView1.DataBind();
          }
  
  protected DataTable GetData()
      {
          DataTable dataTable = new DataTable();
          /* Query operation */
              adapter.Fill(dataTable);
          return dataTable;
      }
  
  protected void btn_export_Click(object sender, EventArgs e)
      {
          var excelClass = new ExcelClass();
          excelClass.ExcelData = GetData();
      }
  </code></pre>
  
  <p>I attempt to create a DataTable as a property in 'Report.aspx.cs' and fill it with the data when Search is clicked. But when I Export , the DataTable is empty again.</p>
  
  <pre><code>DataTable dataTable = new DataTable();
  protected void Page_Load(object sender, EventArgs e)
          {
              /* Page load */
          }
  
  protected void btn_search_Click(object sender, EventArgs e)
          {
              GetData();
              GridView1.DataSource = dataTable ;
              GridView1.DataBind();
          }
  
  protected void GetData()
      {
          /* Query operation */
              adapter.Fill(dataTable);
      }
  
  protected void btn_export_Click(object sender, EventArgs e)
      {
          var excelClass = new ExcelClass();
          excelClass.ExcelData = dataTable;
      }
  </code></pre>
  
  <p>But, I need to fetch the data just once, when Search is clicked, and use the data again when I Export it, so it wont query to the database twice.</p>