Link to home
Start Free TrialLog in
Avatar of guessdip
guessdip

asked on

How to store Data in Cache object in C# ?

Hii i developed a web part in c#. and i used splist to fill data in treeview control. now i need to store this all data in Cache object so i can use this for post back page.

So how do i store data in Cache object ??
public void FilParentTreeNode()
         {
             try
             {
                 SPList projectlist = web.Lists["Projects List"];
                 SPList phase = web.Lists["Project Phases"];
                 SPList milestone = web.Lists["Project Milestones"];
                 SPList issue = web.Lists["Project Issues"];
 
 
 
                 SPQuery query = new SPQuery();
 
                 if (StrFilterProject == "")
                 {
 
                     query.ViewFields = "<FieldRef Name=\"Title\"/><FieldRef Name='Log_x002e__x0020_FinalDispState' /><FieldRef Name='Start_x0020_Date' /><FieldRef Name='End_x0020_Date' /><FieldRef Name='Status' />";
 
                     query.Query = "<Query><OrderBy><FieldRef Name='OS' /><FieldRef Name='Title' /><FieldRef Name='Log_x002e__x0020_FinalDispState' /><FieldRef Name='Start_x0020_Date' /><FieldRef Name='End_x0020_Date' /><FieldRef Name='Status' /></OrderBy></Query>";
                 }
                 else
                 {
                     query.ViewFields = "<FieldRef Name=\"Title\"/><FieldRef Name='Log_x002e__x0020_FinalDispState' /><FieldRef Name='Start_x0020_Date' /><FieldRef Name='End_x0020_Date' /><FieldRef Name='Status' />";
 
                     query.Query = "<OrderBy><FieldRef Name='OS' /><FieldRef Name='Title' /><FieldRef Name='Log_x002e__x0020_FinalDispState' /><FieldRef Name='Start_x0020_Date' /><FieldRef Name='End_x0020_Date' /><FieldRef Name='Status' /></OrderBy><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + StrFilterProject + "</Value></Eq></Where>";
                 }
 
                 SPListItemCollection item = projectlist.GetItems(query);
                 
                 if (StrFilterProject == "")
                 {
                     ddlist.DataSource = item;
                     ddlist.DataBind();
                 }
 
                 foreach (SPListItem itemdata in item)
                 {
 
                     TreeNode project = new TreeNode();
                     
                     project.ToolTip = "Project List";
                    
 
                     try
                     {
                         if (itemdata["Start_x0020_Date"] == null)
                         {
                             itemdata["Start_x0020_Date"] = "1900-01-01 00:00:00.000";
                            
                         }
 
                         if (itemdata["End_x0020_Date"] == null)
                         {
                             itemdata["End_x0020_Date"] = "1900-01-01 00:00:00.000";
                         }
                         if (itemdata["Status"].ToString() == "Open")
                         {
                             project.Text = "<b> <Font Color=Green>" + itemdata["Title"].ToString() + "</font></b>" + "<font color=Red>" + "--" + "</font>"  + DateTime.Parse(itemdata["Start_x0020_Date"].ToString()).ToShortDateString() + "<font color=Red>" + "--" + "</font>" + DateTime.Parse(itemdata["End_x0020_Date"].ToString()).ToShortDateString() + "<font color=Red>" + "--" + "</font>" + itemdata["Log_x002e__x0020_FinalDispState"].ToString();
                         }
                         else
                         {
                             project.Text = "<b> <Font Color=Black>" + itemdata["Title"].ToString() + "</font></b>" + "<font color=Red>" + "--" + "</font>"  + DateTime.Parse(itemdata["Start_x0020_Date"].ToString()).ToShortDateString() + "<font color=Red>" + "--" + "</font>" + DateTime.Parse(itemdata["End_x0020_Date"].ToString()).ToShortDateString() + "<font color=Red>" + "--" + "</font>" + itemdata["Log_x002e__x0020_FinalDispState"].ToString();
                         }
//                         project.Text = itemdata["Title"].ToString() ;
                         project.Value = itemdata["Title"].ToString();
                         
                         tv.Nodes.Add(project);
                         project.SelectAction = TreeNodeSelectAction.SelectExpand;
                         project.PopulateOnDemand = true;
                         //       parent.SelectAction = TreeNodeSelectAction.Expand;
 
                         
                       
                        //     project.Collapse();
                       
                   //      project.PopulateOnDemand = false;
                        
                     }
                     catch { }
 
                     
                 }
           //      if (ViewState["Value"] != null)
           //      {
           ////          ((TreeNode)ViewState["Node"]).Parent.Expand();
                     
           //      }
                
             }
             catch { }
             
                  
               }

Open in new window

Avatar of Gyanendra Singh
Gyanendra Singh
Flag of India image

Put before your foreach loop

Cache["YourData"] = item;
Avatar of guessdip
guessdip

ASKER

Hiii,
There is no keyward cache . when i tried that time i got CacheData Storage.. but it did not work.
Ohh you wanted to use cache inside your webpart .. for using cache in webpart you need to set WebPartCache element in the web.config file ( asp.net cache object)

<WebPartCache Storage="CacheObject">

also include System.Web.Caching in your webpart
System.Web.Caching  as namespace
Thank u ,

 i will try and will let you know if i get any problem...
Hii I tried but i got error.  Error1 'System.Web.Caching.Cache' is a 'type' but is used like a 'variable'I      I wrote cache["item"]=item;  item is a data surce..
Hii All,
 I am sendig You My createchild Control Method.. Everytime fill function call so i am not getting perfomance.. How to store data in cache???
 protected override void CreateChildControls()
         {
        //     base.CreateChildControls();
            
             l2 = new Label();
             //l2.Text = "Dipesh";
             PanelTreeview = new Panel();
             PanelTreeview.ID = "p1";
             PanelTreeview.EnableViewState = true;
           
             btnExpand.ID = "ExpandButton";
             btnExpand.Text = "Expand All";
             btnCollasp.ID = "CollaspButton";
             btnCollasp.Text = "Collaspse All";
             btnPrint.ID = "btnPrint";
             btnPrint.Text = "Print";
             btnPrint.BackColor = System.Drawing.Color.LightBlue;
             btnPrint.Font.Bold = true;
             btnExpand.BackColor = System.Drawing.Color.LightBlue;
             btnCollasp.BackColor = System.Drawing.Color.LightBlue;
             //ExpandButoon.ForeColor = System.Drawing.Color.DarkBlue;
             btnExpand.Font.Bold = true;
             btnCollasp.Font.Bold = true;
             btnSelect.ID = "Select";
             btnSelect.Text = "Select";
             btnSelect.BackColor = System.Drawing.Color.LightBlue;
             btnSelect.Font.Bold = true;
 
             btnAll.ID = "All";
             btnAll.Text = "All";
             btnAll.BackColor = System.Drawing.Color.LightBlue;
             btnAll.Font.Bold = true;
             ddlist.ID = "ddlist";
             ddlist.DataTextField = "Title";
 
            
             StrFilterProject = "";
 
             tv = new TreeView();
             tv.ID = "tv";
             
             
             tv.EnableViewState = true;
             tv.EnableClientScript = true;
             tv.PopulateNodesFromClient = true;
 
 
 
 
 
             btnSelect.Click += new EventHandler(select_Click);
             btnAll.Click += new EventHandler(AllButton_Click);
             btnExpand.Click += new EventHandler(ExpandButoon_Click);
             btnCollasp.Click += new EventHandler(CollaspButton_Click);
             btnPrint.Click+=new EventHandler(btnPrint_Click);
             //if (!Page.IsPostBack)
             //{
                 FilParentTreeNode();
                 tv.CollapseAll();
                 //       ViewState["Tree"] = tv;
             //}
             //else
             //{
                
          //   }
                 Controls.Add(ddlist);
                 Controls.Add(btnSelect);
                 Controls.Add(btnAll);
                 Controls.Add(btnExpand);
                 Controls.Add(btnCollasp);
                 Controls.Add(btnPrint);
                 Controls.Add(l2);
                 Controls.Add(PanelTreeview);
         
                 Controls.Add(tv);
                 tv.SelectedNodeChanged += new EventHandler(tv_SelectedNodeChanged);
 
                 tv.TreeNodePopulate += new TreeNodeEventHandler(tv_TreeNodePopulate);
          
               
               
         }

Open in new window

What, exactly, are you trying to store?  Which object or objects?
I am trying to store Treeview with expanded and collasp.
ASKER CERTIFIED SOLUTION
Avatar of FrancineTaylor
FrancineTaylor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok I will try this.Hope will get solution..
I got the hint how to do..