Cashing
1. Remember that ASP.NET throws away Web form variables after the form is displayed, so you'll want to save data sets
as Application, Session, or Cache variables so that you don't have to recreate them each time the form is displayed.
2. The Cache object is uniquely useful when you're working with data sets because it allows you to specify an expiration
for the data it contains. Data sets can be large, and you usually don't wnat to leave them in memory indefinitely.
As with Application state, all code within the application has access to data in the Cache object.
3. The following code creates the data set once, the first time anyone visits the page. The data set is then maintained
in memory for 20 minutes after it was last accessed - at which time, it is discarded. This produces very quick
response time for most requests but doesn't tie up the servers memory with unused data
4. Also, by combining both data tables in a single data set, you ensure that both items exist in the Cache object. If
you store the data tables separately, it's possible that one item might be discarded before the other.
NOTE: The "Cache" object is just like the "Application" state object; however, "Cache" allows you to set expiration
rules for the Data.
TWO EXAMPLES (1st is with Two Combobox's. 2nd is with Start Page)
FIRST EXAMPLE
// Data adapter, combines SQL command and connection string
SqlDataAdapter da_DB = new SqlDataAdapter("SELECT * FROM myTable"), "server=DBServer2;database=MyDatabase etc....");
// Data set to contain two data tables
public DataSet ds_Both = new DataSet("Both");
private void Page_Load(object sender, System.EventArgs e)
{
// Create Cache if this is the first time the page is displayed or if the cache object doesn't yet exist
if ( (!IsPostBack) || (Cache.Get("ds_Both") == null) )
{
// Create the Table
DataTable dtMyDataTable= new DataTable("MydtTableName");
da_DB.Fill(dtMyDataTable);
// Change the adapter's SELECT command
da_DB.SelectCommand.CommandText = "SELECT * FROM Users";
// Create the Users Table
DataTable Users = new DataTable("Users");
da_DB.Fill(Users);
// Add both tables to a single dataset
ds_Both.Tables.Add(dtMyDataTable);
ds_Both.Tables.Add(Users);
// Cache data set for 20 minutes
Cache.Add("ds_Both", ds_Both, null, DateTime.MaxValue, System.TimeSpan.FromMinutes(20)0, System.Web.Caching.CacheItemPriority.default, null);
// Bind the drop-down list to display data
drp_MyDataRepeater.DataBind();
}
else
// If this is post-back get the Cached data set.
ds_Both = (DataSet) Cache["ds_Both"];
}
private void drp_MyDataRepeater_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Set a filter on the view of the Users table
ds_Both.Tables["Users"].DefaultView.RowFilter = "ContactID=" + drp_MyDataRepeater.SelectedItem.Value.ToString();
// Bind to the datagrid
grd_Users.DataBind();
}
SECOND EXAMPLE
SWITCHBOARD WEB FORM
private void Page_Load(object sender, System.EventArgs e)
{
// The first time this page is displayed
if (!IsPostBack)
{
AddToCache("da_Calls", sqlDataAdapter1);
AddToCache("da_Contact_Types", sqlDataAdapter2);
AddToCache("da_Contacts", sqlDataAdapter3);
// Fill data sets and add them to Cache
sqlDataAdapter1.Fill(ds_Calls1);
AddToCache("ds_Calls", ds_Calls1);
sqlDataAdapter2.Fill(ds_Contact_Types1);
AddToCache("ds_Contact_Types", ds_Contact_Types1);
sqlDataAdapter3.Fill(ds_Contacts1);
AddToCache("ds_Contacts", ds_Contacts1);
}
}
void AddToCache(string Name, object Item)
{
// If item is already in cache, simply return
if (Cache[Name] != null) return;
// Otherwise, cache the item for 20 minutes with sliding expiration
Cache.Add(Name, Item, null, DateTime.MaxValue, System.TimeSpan.FromMinutes(20), System.Web.Caching.CacheItemPriority.Default, null);
}
THIS POPULATES OBJECTS WITH THE SWITCHBOARDS OBJECTS
CONTACTS WEB FORM
1. INCLUDE THE FOLLOWING IMPORT STATEMENT
using System.Data.SqlClient
2. CREATE THE FIELDS TO USE ON YOUR 2ND FORM
3. ADD THE FOLLOWING CLASS LEVEL VARIABLES
SqlDataAdapter da_Contacts;
ContactManagement.ds_Contacts ds_Contacts;
// These variables are public for data binding
public ContactManagement.ds_Contact_Types ds_Contact_Types;
public string[] arrState = {"AL", "AK", "AR", "AZ",
"CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL",
"IN", "IA", "KS", "KY", "LA", "MA", "ME", "MD", "MI",
"MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ",
"NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC",
"SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WY"};
4. ADD THE FOLLOWING TO THE "PAGE_LOAD" EVENT FOR THE 2ND FORM
private void Page_Load(object sender, System.EventArgs e)
{
// Get the cached variables
da_Contacts = (SqlDataAdapter) Cache["da_Contacts"];
ds_Contacts = (ds_Contacts) Cache["ds_Contacts"];
ds_Contact_Types = (ds_Contact_Types) Cache["ds_Contact_Types"];
if (!IsPostBack)
{
// Bind to data -- populates drp_Contact_Types and drp_State lists
drp_Contact_Types.DataBind();
drp_States.DataBind();
}
}