title: Exception of type 'System.OutOfMemoryException' was thrown. Why?
body: <p>I have a dynamic query that returns around 590,000 records.  It runs successfully the first time, but if I run it again, I keep getting a <code>System.OutOfMemoryException</code>.  What are some reasons this could be happening?</p>
  
  <p>The error is happening here:</p>
  
  <pre><code>  public static DataSet GetDataSet(string databaseName,string
                                     storedProcedureName,params object[] parameters)
      {
          //Creates blank dataset
          DataSet ds = null;
  
          try
          {
              //Creates database
              Database db = DatabaseFactory.CreateDatabase(databaseName);
              //Creates command to execute
              DbCommand dbCommand = db.GetStoredProcCommand(storedProcedureName);
              dbCommand.CommandTimeout = COMMAND_TIMEOUT;
              //Returns the list of SQL parameters associated with that stored proecdure
              db.DiscoverParameters(dbCommand);
  
              int i = 1;
              //Loop through the list of parameters and set the values
              foreach (object parameter in parameters)
              {
                  dbCommand.Parameters[i++].Value = parameter;
              }
              //Retrieve dataset and set to ds
              ds = db.ExecuteDataSet(dbCommand);
          }
              //Check for exceptions
          catch (SqlException sqle)
          {
              throw sqle;
          }
          catch (Exception e)
          {
              throw e; // Error is thrown here.
          }
          //Returns dataset
          return ds;
      }
  </code></pre>
  
  <p>Here is the code the runs on the button click:</p>
  
  <pre><code>protected void btnSearchSBIDatabase_Click(object sender, EventArgs e)
  {
  
          LicenseSearch ls = new LicenseSearch();
  
          DataTable dtSearchResults = new DataTable();
  
          dtSearchResults = ls.Search();
  
          Session["dtSearchResults"] = dtSearchResults;
  
          Response.Redirect("~/FCCSearch/SearchResults.aspx");
          }
          else
              lblResults.Visible = true;
      }
  </code></pre>