Showing posts with label ASP.Net Funda. Show all posts
Showing posts with label ASP.Net Funda. Show all posts

Tuesday, September 28, 2010

Data Caching with Dataset

In the below example we are going to see that how we can apply Data caching on a Dataset object
Step 1. Create a web form DataCachingPage.aspx
Source View


Note : Replace ! with < and * with >
!%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataCachingPage.aspx.cs" Inherits="DataCachingPage" %*

!!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*

!html xmlns="http://www.w3.org/1999/xhtml"*
!head runat="server"*
!title*!/title*
!script language="javascript" src="JScript.js" *

!/script*
!/head*
!body*
!form id="form1" runat="server"*
!div style="height: 274px; width: 362px"*

!asp:GridView ID="grvDataCaching" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="257px" Width="337px"*
!/asp:GridView*
!br /*
!input id="Submit1" type="submit" value="Open Window" onclick ="startMethod()" /*
!/div*
!/form*
!/body*
!/html*
Code View
using System.Web.Caching;

public partial class DataCachingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//In the Below scenerion we are using Output Caching
//Step: 1 Fill a Dataset Object with a Locally bulid XM File.

FillDataSet fds = new FillDataSet() { file_Path=@"C:\Documents and Settings\BS26691\Desktop\Test.xml"};
grvDataCaching.DataSource = fds.returnDataSet();
grvDataCaching.DataBind();

//Add the created Dataset into the Cache Object

/*------------ Description of Supplied parameters.---------
1. "Dataset"=Key Name of Cache object.
2. fds.returnDataSet()=Value which is kept inside the Cache object
3. new CacheDependency(fds.file_Path)=Object of CacheDependency Class
* This Parameter is set only in those scenerion where we want to Update our cache object
* specific to a file or Resource.
* In the below scenerion if XMl File is Changed
* than Cache Object will be Updated Accordingly
* If there is no CacheDependency than set it as Null
4. DateTime.MaxValue=Absolute Expiration
* IF we want that our cache will be expired after certain ammount of time
* Than we use this type of Expiration
* e.g Weather ForCasting where we are sure for data to be cached for a whole day.
* if we are not setting this than we need to set it as DateTime.MaxValue
5. TimeSpan.FromMinutes(20)=Slidding Expiration Time
* in the above scenerio the Cache object will be expired after 20 second of its access.
* That means it will not be expired after 20 second
* If Cache is accessed in last 19th second the it is going to persist for next 21 seconds.
* if we are not setting this than we need to set it as TimeSpan.Zero
------------------------------------------------------------------------------
* Note: AT A SINGLE TIME ONLY ONE PARAMETR NEED TO BE SET AMONG 4 AND 5
* BOTH PARAMETER CAN'T BE SET SIMULTANEOUSLY
------------------------------------------------------------------------------
*/

Cache.Insert("Dataset", fds.returnDataSet(), new CacheDependency(fds.file_Path), DateTime.MaxValue, TimeSpan.FromMinutes(20));

}
}

Step 2. Create a Page POP_UP_window.aspx
Source View
Note : replace ! with <>

!%@ Page Language="C#" AutoEventWireup="true" CodeFile="POP_UP window.aspx.cs" Inherits="POP_UP_window" %*
!!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*
!html xmlns="http://www.w3.org/1999/xhtml"*
!head runat="server"*
!title*!/title*
!script language="javascript" src="JScript.js" *

!/script*
!style type="text/css"*
#Submit1 {
width: 121px;
}
!/style*
!/head*
!body*
!form id="form1" runat="server"*
!div*
Transported Data.
!br /*
!br /*
!br /*
!asp:GridView ID="grvDataCaching" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="224px" Width="367px"
AllowPaging="True"*
!RowStyle BackColor="#EFF3FB" /*
!/asp:GridView*
!br /*
!/div*
!p*
!/p*
!input id="Button2" type="Exit" value="button" OnClick="CloseMethod()" /*

!/form*

!/body*
!/html*
Code View :
public partial class POP_UP_window : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

//Retrive DataSet Value From Cache Object
DataSet ds = Cache["Dataset"] as DataSet ;

// Always Check for Null reference before accessing the Cache object.
if (ds == null)
{

//If Cache is Blank than fill the dataset with local Test xml file
FillDataSet fds = new FillDataSet() { file_Path = @"C:\Documents and Settings\BS26691\Desktop\Test.xml" };
// Add Dataset Object to Cache
Cache.Insert("Dataset", fds.returnDataSet());
}
//Fill your GridView with Cache Object
DataSet dset = Cache["Dataset"] as DataSet;
grvDataCaching.DataSource = dset;
grvDataCaching.DataBind();

}

}
Step 3. Create Class FillDataSet
public class FillDataSet
{
public string file_Path { get; set; }

public FillDataSet()
{
}
public DataSet returnDataSet()
{
DataSet ds = new DataSet();
ds.ReadXml(file_Path);
return ds;
}

}

Step 4. Create a File JScript.js

//This Function is called by a HTMl button
function startMethod() {
alert("After This Event POP-UP window will be Open.");
/*
"http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx" Repalce this Path with exact Page Path
*/

mywindow = window.open("http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx", "mywindow", "location=1,status=1,scrollbars=1");
mywindow.moveTo(10, 5);

}
// This method will Close the Open POP-UP window.
function CloseMethod() {
alert("Do You Want to Close This Window");
/*
"http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx" Repalce this Path with exact Page Path
*/
if (window.open("http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx", "mywindow", "location=1,status=1,scrollbars=1,width=500,height=500")) {
window.close();
}
}

Note : Set the DataCachingPage.aspx As Start Page

Data Caching

using System.Web.Caching;

Step 1. Cretae a Class DataCachingPage
Source View
Note : replace ! with < and * with >

!%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataCachingPage.aspx.cs" Inherits="DataCachingPage" %*

!!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*

!html xmlns="http://www.w3.org/1999/xhtml"*
!head runat="server"*
!title*!/title*
!script language="javascript" src="JScript.js" *

!/script*
!/head*
!body*
!form id="form1" runat="server"*
!div style="height: 274px; width: 362px"*

!asp:GridView ID="grvDataCaching" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="257px" Width="337px"*
!/asp:GridView*
!br /*
!input id="Submit1" type="submit" value="Open Window" onclick ="startMethod()" /*
!/div*
!/form*
!/body*
!/html

.CS File.
protected void Page_Load(object sender, EventArgs e)
{
//In the Below scenerion we are using Output Caching
//Step: 1 Fill a Dataset Object with a Locally bulid XM File.
FillDataSet fds = new FillDataSet() { file_Path=@"C:\Documents and Settings\BS26691\Desktop\Test.xml"};
grvDataCaching.DataSource = fds.returnDataSet();
grvDataCaching.DataBind();
//Add the created Dataset into the Cache Object

/*------------ Description of Supplied parameters.---------
1. "Dataset"=Key Name of Cache object.
2. fds.returnDataSet()=Value which is kept inside the Cache object
3. new CacheDependency(fds.file_Path)=Object of CacheDependency Class
* This Parameter is set only in those scenerion where we want to Update our cache object
* specific to a file or Resource.
* In the below scenerion if XMl File is Changed
* than Cache Object will be Updated Accordingly
* If there is no CacheDependency than set it as Null
4. DateTime.MaxValue=Absolute Expiration
* IF we want that our cache will be expired after certain ammount of time
* Than we use this type of Expiration
* e.g Weather ForCasting where we are sure for data to be cached for a whole day.
* if we are not setting this than we need to set it as DateTime.MaxValue
5. TimeSpan.FromMinutes(20)=Slidding Expiration Time
* in the above scenerio the Cache object will be expired after 20 second of its access.
* That means it will not be expired after 20 second
* If Cache is accessed in last 19th second the it is going to persist for next 21 seconds.
* if we are not setting this than we need to set it as TimeSpan.Zero
------------------------------------------------------------------------------
* Note: AT A SINGLE TIME ONLY ONE PARAMETR NEED TO BE SET AMONG 4 AND 5
* BOTH PARAMETER CAN'T BE SET SIMULTANEOUSLY
------------------------------------------------------------------------------
*/

Cache.Insert("Dataset", fds.returnDataSet(), new CacheDependency(fds.file_Path), DateTime.MaxValue, TimeSpan.FromMinutes(20));

}

Step 2. Create a Class POP_UP window.aspx
Source View
Note : replace ! with < and * with >

!%@ Page Language="C#" AutoEventWireup="true" CodeFile="POP_UP window.aspx.cs" Inherits="POP_UP_window" %*
!!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*
!html xmlns="http://www.w3.org/1999/xhtml"*
!head runat="server"*
!title*!/title*
!script language="javascript" src="JScript.js" *

!/script*
!style type="text/css"*
#Submit1 {
width: 121px;
}
!/style*
!/head*
!body*
!form id="form1" runat="server"*
!div*
Transported Data.
!br /*
!br /*
!br /*
!asp:GridView ID="grvDataCaching" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="224px" Width="367px"
AllowPaging="True"*
!RowStyle BackColor="#EFF3FB" /*
!/asp:GridView*
!br /*
!/div*
!p*
!/p*
!input id="Button2" type="Exit" value="button" OnClick="CloseMethod()" /*

!/form*

!/body*
!/html*
.CS File
protected void Page_Load(object sender, EventArgs e)
{

//Retrive DataSet Value From Cache Object
DataSet ds = Cache["Dataset"] as DataSet ;

// Always Check for Null reference before accessing the Cache object.
if (ds == null)
{
//If Cache is Blank than fill the dataset with local Test xml file
FillDataSet fds = new FillDataSet() { file_Path = @"C:\Documents and Settings\BS26691\Desktop\Test.xml" };
// Add Dataset Object to Cache
Cache.Insert("Dataset", fds.returnDataSet());
}
//Fill your GridView with Cache Object
DataSet dset = Cache["Dataset"] as DataSet;
grvDataCaching.DataSource = dset;
grvDataCaching.DataBind();

}

Step 3. Add A Third Class

public class FillDataSet
{
public string file_Path { get; set; }

public FillDataSet()
{
}
public DataSet returnDataSet()
{
DataSet ds = new DataSet();
ds.ReadXml(file_Path);
return ds;
}

}


Step 4.
Add A JScript.js File.
//This Function is called by a HTMl button
function startMethod() {
alert("After This Event POP-UP window will be Open.");
/*
"http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx" Repalce this Path with exact Page Path
*/
mywindow = window.open("http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx", "mywindow", "location=1,status=1,scrollbars=1");
mywindow.moveTo(10, 5);

}
// This method will Close the Open POP-UP window.
function CloseMethod() {
alert("Do You Want to Close This Window");
/*
"http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx" Repalce this Path with exact Page Path
*/
if (window.open("http://localhost:4909/DataCaching%2027_1.0/POP_UP%20window.aspx", "mywindow", "location=1,status=1,scrollbars=1,width=500,height=500")) {
window.close();
}
}