Thursday, January 9, 2014

NIIT organize Workshop for improving knowledge about various software technology

Dear Student,

NIIT, Asia's largest IT trainer, have launched an initiative to expand the technology horizons of India's large student population by introducing specialized education and training programs and it is on these lines that NIIT organize Workshop for improving knowledge about various software & Hardware technology:

1. Desktop Application Development(Java/.NET)

2. Web Application Development(Java/.NET/PHP)

3. Mobile Application Development(Java)

4. Hardware & Networking

workshop

Tuesday, October 22, 2013

Caching:Varying the Output Cache By Parameter in ASP.NET

OutPutCacheByPrameter.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OutPutCacheByPrameter.aspx.cs" Inherits="OutPutCacheByPrameter" %>



 



<!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>Caching; Varying the Output Cache By Parameter</title>



</head>



<body>



    <form id="form1" runat="server">



    <div>



    <asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="False"



                ShowHeader="False">



                <Columns>



                 <asp:HyperLinkField DataTextField="EmpName" DataNavigateUrlFields="EmpId"



                DataNavigateUrlFormatString="EmployeeDetails.aspx?EmpId={0}" />



                </Columns>



            </asp:GridView>



    </div>



    </form>



</body>



</html>






Code File:





using System;



using System.Collections;



using System.Configuration;



using System.Data;



using System.Linq;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



using System.Data.SqlClient;



 



public partial class OutPutCacheByPrameter : System.Web.UI.Page



{



    SqlConnection con;



    SqlCommand cmd = new SqlCommand();



    DataSet ds = new DataSet();



    SqlDataAdapter da;



    protected void Page_Load(object sender, EventArgs e)



    {



        BindData();



    }



    public void BindData()



    {



        string Appcon = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;



        con = new SqlConnection(Appcon);



        cmd.CommandText = "Select * from Employees";



        da = new SqlDataAdapter(cmd);



        cmd.Connection = con;



        con.Open();



        cmd.ExecuteNonQuery();



        da.Fill(ds);



        if (ds.Tables[0].Rows.Count > 0)



        {



            grdEmployee.DataSource = ds;



            grdEmployee.DataBind();



        }



    }



}






web.config:





<connectionStrings>



        <add name="ConnectionStr" connectionString="Data Source=.;Initial Catalog=asp;User Id=sa;Password=niitbpl123"/>



    </connectionStrings>






Code Behind File:





using System;



using System.Collections;



using System.Configuration;



using System.Data;



using System.Linq;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



using System.Data.SqlClient;



 



public partial class OutPutCacheByPrameter : System.Web.UI.Page



{



    SqlConnection con;



    SqlCommand cmd = new SqlCommand();



    DataSet ds = new DataSet();



    SqlDataAdapter da;



    protected void Page_Load(object sender, EventArgs e)



    {



        BindData();



    }



    public void BindData()



    {



        string Appcon = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;



        con = new SqlConnection(Appcon);



        cmd.CommandText = "Select * from Employees";



        da = new SqlDataAdapter(cmd);



        cmd.Connection = con;



        con.Open();



        cmd.ExecuteNonQuery();



        da.Fill(ds);



        if (ds.Tables[0].Rows.Count > 0)



        {



            grdEmployee.DataSource = ds;



            grdEmployee.DataBind();



        }



    }



}






EmployeeDetails.aspx





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeDetails.aspx.cs" Inherits="EmployeeDetails" %>



<%@ OutputCache Duration="5" VaryByParam="none" %>



<!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>CACHING: Output Cache By Parameter: Employee Details</title>



</head>



<body>



    <form id="form1" runat="server">



    <div>



    <asp:Label ID="lblTime" runat="server"></asp:Label><br />



            <br />



            <asp:DetailsView ID="dtlEmployee" runat="server">



            </asp:DetailsView>



    </div>



    </form>



</body>



</html>






Code Behind File:





using System;



using System.Collections;



using System.Configuration;



using System.Data;



using System.Linq;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



using System.Data.SqlClient;



 



public partial class EmployeeDetails : System.Web.UI.Page



{



    SqlConnection con;



    SqlCommand cmd = new SqlCommand();



    DataSet ds = new DataSet();



    SqlDataAdapter da;



    protected void Page_Load(object sender, EventArgs e)



    {



        lblTime.Text = System.DateTime.Now.ToString();



        BindData();



    }



    public void BindData()



    {



        string Appcon = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;



        con = new SqlConnection(Appcon);



        cmd.CommandText = "Select * from Employees where EmpId=" + Request.Params[0].ToString();



        da = new SqlDataAdapter(cmd);



        cmd.Connection = con;



        con.Open();



        cmd.ExecuteNonQuery();



        da.Fill(ds);



        if (ds.Tables[0].Rows.Count > 0)



        {



            dtlEmployee.DataSource = ds;



            dtlEmployee.DataBind();



        }



    }



}






Output:



1a1b

What is Caching in ASP.NET.

What is Caching?
Caching is a technique of persisting the data in memory for immediate access to requesting program calls. Many in the developer community consider caching as one of the features available to improve performance of Web applications.
Why Caching?

Consider a page that has list of Employee name, contact numbers and mail-Ids of existing employees of a company on an intranet accessible by all employees. This is very useful information that is available throughout the company and could also be one of the most accessed pages. The functionality of adding, updating or deleting is usually less intensive compared to more transaction-based systems like Purchase ordering, Voucher creation etc. Now in a normal scenario the process of querying database for each request is not cost-effective in terms of server resources, hence is lot better to cache or persist the data to avoid this costly loss of resources.

The .NET Advantage
ASP.NET provides the flexibility in terms of caching at different levels.
1. Page Level Output Caching

This is at the page level and one of the easiest means for caching pages. This requires one to specify Duration of cache and Attribute of caching.
Syntax: <%@ OutputCache Duration="60" VaryByParam="none" %>
The above syntax specifies that the page be cached for duration of 60 seconds and the value "none" for VaryByParam* attribute makes sure that there is a single cached page available for this duration specified.

2. Fragment Caching
Even though this definition refers to caching portion/s of page, it is actually caching a user control that can be used in a base web form page. In theory, if you have used include files in the traditional ASP model then this caching model is like caching these include files separately. In ASP.NET more often this is done through User Controls. Initially even though one feels a bit misleading, this is a significant technique that can be used especially when implementing "n" instances of the controls in various *.aspx pages. We can use the same syntax that we declared for the page level caching as shown above, but the power of fragment caching comes from the attribute "VaryByControl". Using this attribute one can cache a user control based on the properties exposed.
Syntax: <%@ OutputCache Duration="60" VaryByControl="DepartmentId" %>
The above syntax when declared within an *.ascx file ensures that the control is cached for 60 seconds and the number of representations of cached control is dependant on the property "DepartmentId" declared in the control.

3. Application Level Caching
With Page level Output caching one cannot cache objects between pages within an application. Fragment caching is great in that sense but has limitations by using user controls as means to do. We can use the Cache object programmatically to take advantage of caching objects and share the same between pages. Further the availability of different overloaded methods gives a greater flexibility for our Cache policy like Timespan, Absolute expiration etc. But one of the biggest takes is the CacheDependancy. This means that one can create a cache and associate with it a dependency that is either another cache key or a file.

Program 1: VarByParam=”none”

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



<%@ OutputCache Duration="5" VaryByParam="none" %>



<!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>CACHING: Page Output Caching</title>



</head>



<body>



    <form id="form1" runat="server">



    <div>



       <asp:Label ID="lblTime" runat="server"></asp:Label>



    



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />



    </div>



    </form>



</body>



</html>




Code Behind file:





using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



 



public partial class _Default : System.Web.UI.Page 



{



    protected void Page_Load(object sender, EventArgs e)



    {



       



    }



    protected void Button1_Click(object sender, EventArgs e)



    {



        lblTime.Text = DateTime.Now.ToString();



    }



}




Output:



12









Program 2: VarByParam=”TextBox1”





<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>



<%@ OutputCache Duration="8" VaryByParam="TextBox1" %>



<!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>Untitled Page</title>



</head>



<body>



    <form id="form1" runat="server">



    <div>



    <asp:Label ID="lblTime" runat="server"></asp:Label>



    



    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />



    </div>



    </form>



</body>



</html>






Code Behind file:





using System.Web.Security;



using System.Web.UI;



using System.Web.UI.HtmlControls;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Xml.Linq;



 



public partial class Default2 : System.Web.UI.Page



{



    protected void Page_Load(object sender, EventArgs e)



    {



 



    }



    protected void Button1_Click(object sender, EventArgs e)



    {



        lblTime.Text = DateTime.Now.ToString();



    }



}




Output:



Enter the Name in textbox and click the button yon will see the time



3





4





5



Note: Wile changing the name in textbox time will change



6

Popular Posts

You might also like:-

Google+ Followers