E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Data Caching Sample by Charles Carroll

Here is code to read data into 3 listboxes with a 10 minute cache for each.

   filename=/experiments/caching/cached.aspx

<Test Script Below>


<%@ trace="true"%>
<%@ Import Namespace="System.Data.sqlclient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration.ConfigurationSettings" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    Dim strConn as string=AppSettings("LearnaspSamples")

    Trace.Write("cityinfo","startfill")
    Dim strSQL as string ="select distinct city from publishers"
    If cache("citydistinct") IS Nothing
        Trace.Write("citydistinct","created a cache")
        cache.insert("citydistinct",DataTableMaker(strConn,strSQL),Nothing,DateTime.Now.AddMinutes(10),TimeSpan.Zero)
    Else
        Trace.Write("citydistinct","grabbed from cache")
    End If

    cy.DataSource = cache("citydistinct")
    cy.DataBind()
    Trace.Write("cityinfo","endfill")

    Trace.Write("stateinfo","startfill")
    strSQL="select distinct state from publishers"
    If cache("statedistinct") IS Nothing
        Trace.Write("statedistinct","created a cache")
        cache.insert("statedistinct",DataTableMaker(strConn,strSQL),Nothing,DateTime.Now.AddMinutes(10),TimeSpan.Zero)
    Else
        Trace.Write("statedistinct","grabbed from cache")
    End If
    st.DataSource = cache("statedistinct")
    st.DataBind()    
    Trace.Write("stateinfo","endfill")
    
    Trace.Write("zipinfo","startfill")
    strSQL="select distinct zip from publishers"
    If cache("zipdistinct") IS Nothing
        Trace.Write("zipdistinct","created a cache")
        cache.insert("zipdistinct",DataTableMaker(strConn,strSQL),Nothing,DateTime.Now.AddMinutes(10),TimeSpan.Zero)
    Else
        Trace.Write("zipdistinct","grabbed from cache")
    End If
    zp.DataSource = cache("zipdistinct")
    zp.DataBind()
    Trace.Write("zipinfo","endfill")
End Sub

Function DataTableMaker(strConn as string,strSQL as string) as datatable
    Dim dt1 As New DataTable("temp")
        Dim conn As New SqlConnection(strConn)
        Dim adapter As New SqlDataAdapter(strSQL, conn)
        adapter.Fill(dt1)
    Return(dt1)
End Function
</script>
<html><head>
<title>Dropdowns</title>
</head>
<form runat="server">
<body bgcolor="#FFFFFF">
<asp:Table runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="cy" datatextfield="city" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="st" datatextfield="state" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Zip</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="zp" datatextfield="zip" runat="server"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body></html>

Here is code to read data into 3 listboxes without the cache.

   filename=/experiments/caching/noncached.aspx

<Test Script Below>


<%@ trace="true"%>
<%@ Import Namespace="System.Data.sqlclient" %>
<%@ Import Namespace="System.Configuration.ConfigurationSettings" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    Dim strConn as string=AppSettings("LearnaspSamples")

    Dim Conn as new SQLconnection(strConn)

    Trace.Write("cityinfo","startfill")
    Dim strSQL as string ="select distinct city from publishers"
    Dim Cmd as New SQLCommand(strSQL,Conn)
    Conn.Open()
    cy.DataSource = Cmd.ExecuteReader()
    cy.DataBind()
    Conn.close()
    Trace.Write("cityinfo","endfill")

    Trace.Write("stateinfo","startfill")
    strSQL="select distinct state from publishers"
    Dim Cmd2 as New SQLCommand(strSQL,Conn)
    Conn.Open()
    st.DataSource = Cmd2.ExecuteReader()
    st.DataBind()    
    Conn.close()
    Trace.Write("stateinfo","endfill")
    
    Trace.Write("zipinfo","startfill")
    strSQL="select distinct zip from publishers"
    Dim Cmd3 as New SQLCommand(strSQL,Conn)
    Conn.Open()
    zp.DataSource = Cmd3.ExecuteReader()
    zp.DataBind()
    conn.close()
    Trace.Write("zipinfo","endfill")

End Sub
</script>
<html><head>
<title>Dropdowns</title>
</head>
<form runat="server">
<body bgcolor="#FFFFFF">
<asp:Table runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="cy" datatextfield="city" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="st" datatextfield="state" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Zip</asp:TableCell>
<asp:TableCell><ASP:DropDownList id="zp" datatextfield="zip" runat="server"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body></html>

Here is code to show all the caches that ASP.net is managing.

   filename=/experiments/caching/cacheshow.aspx

<Test Script Below>


<%@ trace="true"%>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    dim objItem as object,strName as string
    For Each objItem In System.Web.HttpContext.Current.Cache
        strName = objItem.Key
        trace.write("Cache Name: " & strName, "cache type=" & System.Web.HttpContext.Current.Cache(strName).GetType().ToString())
    Next
End Sub
</script>
<html><head>
<title>Dropdowns</title>
</head>
<form runat="server">
<body bgcolor="#FFFFFF">
</form>
</body></html>
Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site