Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Caching with Data Source Controls:

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 293
    Comment on it

    Caching with Data Source Controls:

    Why do we need caching..?

    In a Website majority of the pages are dynamic that provide dynamic content, customized

    for the user requesting the page. Dynamic pages also help provide dynamic content fetched

    from a changing data store. So fetching of data from the data store and displaying it on

    the web page needs some time which say 'x' seconds now if the same page is reloaded again,

    it will again take 'x' seconds to fetch the data from the data store and display it on the

    web page this reduces the performance of the application. To overcome this issue Caching

    is used. Through Caching the caches the output generated by the page and serve the cached

    content in future requests, and hence improve the performance of the application.

    Data Source Control:

    Data source controls provide data to data-bound controls such as the GridView, FormView, etc controls,we can use the SqlDataSource or AccessDataSource control to retrieve data from a database with little or no code. To retrieve data from a database using the SqlDataSource control, we need to set the following properties:

    1.ProviderName- name of the ADO.NET provider that represents the database we are working with.

    2.ConnectionString- path of the connection string that connects the database.

    3.SelectCommand- Type of command SQL query or stored procedure that returns data from the database.

    Example:

    <asp:SqlDataSource
              id="SqlDataSource1"
              runat="server"
              DataSourceMode="DataReader"
              ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
              SelectCommand="SELECT LastName FROM Employees">
          </asp:SqlDataSource>
    

    Caching with Data Source:

    To cache data by data source controls, we have to enable caching as it is not enabled by default.To enable caching we have to set the DataSourceMode property to DataSet and EnableCaching property to true.We can also specify the number of seconds after which the cache data gets refreshed for which we set the CacheDuration property.

    Example: In the following example the caching is enabled and the cached data gets refreshed after 20 seconds.

    <%@ Page language="C#" %>
    
    <!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>ASP.NET Example</title>
    </head>
    <body>
      <form id="form1" runat="server">
    
      <asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      DataSourceMode="DataSet"
      ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
      EnableCaching="True"
      CacheDuration="20"
      SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees">
      </asp:SqlDataSource>
    
         <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1">
            <columns>
            <asp:BoundField HeaderText="First Name" DataField="FirstName" />
            <asp:BoundField HeaderText="Last Name" DataField="LastName" />
            <asp:BoundField HeaderText="Title" DataField="Title" />
            </columns>
            </asp:GridView>
    
            </form>
        </body>
    </html>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: