Solved

caching dropdownlist

Posted on 2006-11-13
11
203 Views
Last Modified: 2010-04-23
On my "options.aspx" page, I have a dropdownlist . I populate it by getting the values from the database...

user1 can have A1,A2,A3
User2 can have A1,A2
User3 can have A1,A2,A3

I bind the dropdownlist to an Arraylist...

How can I cache this? Should I cache it based on username? if so, then what if i have 30 users..will I have 30 cached pages in memory??

Any ideas?
0
Comment
Question by:Camillia
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 17929743
FarzaDW,

You can store arraylist to a session variable. In this case arraylist contains value A,B,C etc for each user.

ex:

        Dim ar As New ArrayList
        ar.Add("A")
        ar.Add("B")
        ar.Add("C")
        Session("Options") = ar

You will get this value thru out pages. Since session is user based, 30 user can hold their own values.

Thad
0
 
LVL 7

Author Comment

by:Camillia
ID: 17929771
But then next time when the user logs in, I have to ***hit the database***, get the values, store them in session again...

The dropdownlist doesnt change as often so I want to cache it....
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17930173
Then you can cache your dataset using Data Caching using insert method and even you can set expiring date or even to XML file as cache dependency.

Then each time you can test whether cache object exist or not, and take decision on hitting database.

Thad
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 7

Author Comment

by:Camillia
ID: 17930237
let me ask you this:

If I do that...then will be cached for EVERYONE? will everyone see the SAME cached values in the dropdownlist?...

For example, User1 should see values: A1, A2,A3
                    User2 should see values: A1, A2
                    User3 should see values: A1, A2, A3

If user1 logs in and the dropdownlist is cached, then user2 logs in..user2 would see "A1,A2,A3"..instead of "A1,A2"...correct?

0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17930432
You can store all records in dataset. So your dataset contains A1,A2,A3,A4 etc for all users. Now it is cached. When ever you logged to page, you know who logged. You may have a ID for user, then filter your dataset with your user details, suppose user1 logged in, you took dataset from cache, and filter your dataset to 3 records by filtering based on user1. And set that dataset to dropdownlist and bind. Now user2 logged in and filter based on his/her credential, and bind to dropdownlist.

Thad
0
 
LVL 7

Author Comment

by:Camillia
ID: 17930480

ah, it makes sense...I read here and another place not to use a dataset:
http://aspnet.4guysfromrolla.com/articles/050405-1.aspx

Anyway to do what you're saying using ArrayLists? then cache the arrayList?
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 17930641
Yes you can cache arraylist. Depends on what your A1,A2 and its length and actual value.

Thad
0
 
LVL 7

Author Comment

by:Camillia
ID: 17930850

 i'm probably beating this to death..but with a cached ArrayList, how would I display the correct dropdownlist for the correct user??...

So, if I use the 'dataset', I can store the userID in it and as you explained, display the proper value to the user...

do the same thing with ArrayList...store the userID and the values for each user in the ArrayList?
0
 
LVL 4

Accepted Solution

by:
Xeavn earned 500 total points
ID: 17931407
Well I don't know too much about trying to store an Arraylist in Cache, although I suppose it could be done. This is how I handled the situation.

<asp:DropDownList id="UnitsDDL"
     CssClass="tableinput"
     DataSource='<%# LoadCachedDataByKey("sp_UnitsList", "Units") %>'
     DataTextField="UNITS_ABBREV"
     DataValueField="UNITS_ID"
     SelectedValue='<%# DataBinder.Eval(Container.DataItem, "Unit.ID") %>'
     OnKeyDown="t4e();"
     Visible="False"
     RunAt="Server" />

Public Function LoadCachedDataByKey(ByVal strSP As String, ByVal strCK As String) As DataView
      Dim myContext As HttpContext
      myContext = HttpContext.Current

      Dim Source As DataView
      Dim strCacheKey As String
      strCacheKey = strCK & Convert.ToString(HttpContext.Current.Session("UserID"))

      If myContext.Cache(strCacheKey) Is Nothing Then
            Dim spQuery As New SQLCommand
            spQuery.CommandType = CommandType.StoredProcedure
            spQuery.CommandText = strSP
            Source = dw.Data.LoadData(spQuery)
            myContext.Cache(strCacheKey) = Source
      Else
            Source = DirectCast(myContext.Cache(strCacheKey), DataView)
      End If

      Return Source
End Function 'LoadCachedDataByKey

In this case the Source = dw.Data.LoadData(spQuery) is a function call to get my data from that database, and load it into a DataView, and once I have that dataview I just cache the dataview using a combination of a string and the Users ID #
0
 
LVL 7

Author Comment

by:Camillia
ID: 17931474

" I just cache the dataview using a combination of a string and the Users ID #"...where are you doing this?...

I think i need to do something similar... cache the ArrayList with the UserID...that would tell me what ArrayList goes with what user....
0
 
LVL 4

Expert Comment

by:Xeavn
ID: 17931615
strCacheKey = strCK & Convert.ToString(HttpContext.Current.Session("UserID"))

This is the line where that is done. The strCacheKey is set to be strCK which in this case is just "Units" and then the persons id number which I have stored as a Session Variable. So the CacheKey becomes Units21 for the user with id # 21 and Units34 for the user with id # 34.

Then later on if that cache doesn't exist yet, I tell it to cache the dataview under that CacheKey. (This is the line that does that.)

myContext.Cache(strCacheKey) = Source
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question