473,414 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

update cache object when update sql database

Expand|Select|Wrap|Line Numbers
  1. if (Cache["MyDataSet"]==null)
  2.         {
  3.  
  4.             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mtiladminConnectionString"].ConnectionString);
  5.             SqlDataAdapter mydataadapter = new SqlDataAdapter("select   intproductid,vchproductname,vchproductacctype,vchprodacclogoimg from productmaster  order by  intproductid desc", con);
  6.  
  7.  
  8.  
  9.             mydataadapter.Fill(ds, "Authors");
  10.  
  11.             SqlCacheDependency dependency = new SqlCacheDependency("mtiladmin", "productmaster");
  12.       //System .Web .Caching .CacheDependency   dependency=  new System.Web.Caching.CacheDependency(
  13.       //Server.MapPath("MyDataSet.xml"));
  14.  
  15.             Cache.Insert("mydataset", ds, dependency);
  16.  
  17. //Cache.Insert("MyDataSet", ds, 
  18. //    dependency, 
  19. //    DateTime.Now.AddMinutes(2), TimeSpan.Zero, 
  20. //    CacheItemPriority.High, null);
  21.  
  22.  
  23.  
  24.  
  25.             viewgrid.DataSource = Cache["MyDataSet"] as DataSet ;
  26.         viewgrid.DataBind();
  27.         //Response.Write(" from table");
  28.  
  29.     }
  30.     else if (Cache["MyDataSet"] != null)
  31. {
  32.    // DataView view = new DataView();
  33.  
  34.     DataSet ds1 = new DataSet();
  35.     ds1 =Cache["MyDataSet"] as  DataSet ;
  36.  
  37.  
  38.             //ds =  new DataSet(view);
  39.             viewgrid.DataSource = ds1;
  40.                 viewgrid.DataBind();
  41.                 //Response.Write("from cache");
  42.  
  43. }
when i update row , cache object are not updating
Jan 18 '09 #1
3 5400
when i update row on first click it is not updataing , it shows previous data, on secon click it is updating


my ciode is
Expand|Select|Wrap|Line Numbers
  1.  public void bindgrid()
  2.      {
  3.  
  4.          DataSet myCustomers;
  5.          myCustomers = (DataSet)Cache["MyDataSet"];
  6.  
  7.          if (myCustomers == null)
  8.          {
  9.              SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mtiladminConnectionString"].ConnectionString);
  10.              SqlDataAdapter mydataadapter = new SqlDataAdapter("select   intproductid,vchproductname,vchproductacctype,vchprodacclogoimg from productmaster  order by  intproductid desc", con);
  11.  
  12.              myCustomers = new DataSet();
  13.              mydataadapter.Fill(myCustomers, "Customers");
  14.              SqlCacheDependency dependency = new SqlCacheDependency("mtiladmin", "productmaster");
  15.              Cache.Insert("MyDataSet", myCustomers, dependency);
  16.  
  17.  
  18.          }
  19.  
  20.  
  21.          viewgrid.DataSource = myCustomers;
  22.          viewgrid.DataBind();
  23.  
  24.  
  25.         ////////////////////////////////////
  26.  
  27.     }  }
  28.  
  29.  protected void viewgrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
  30.     {
  31.         //Cache["MyDataSet"]="" ;
  32.  
  33.         int ID = Int32.Parse(viewgrid.DataKeys[e.RowIndex].Values[0].ToString());
  34.         TextBox txtpath1 = (TextBox)viewgrid.Rows[e.RowIndex].FindControl("txtprodname");
  35.         TextBox txtpath2 = (TextBox)viewgrid.Rows[e.RowIndex].FindControl("txtproductacctype");
  36.         TextBox txtpath18 = (TextBox)viewgrid.Rows[e.RowIndex].FindControl("txtprodacclogoimg");
  37.         string query = "update productmaster set vchproductName='" + txtpath1.Text + "',vchproductacctype='" + txtpath2.Text + "',vchprodacclogoimg='" + txtpath18.Text + "' where intproductid=" + ID;
  38.         SqlCommand cmd1 = new SqlCommand(query, con);
  39.         cmd1.Connection.Open();
  40.         cmd1.ExecuteNonQuery();
  41.         viewgrid.EditIndex = -1;
  42.  
  43.         bindgrid();
  44.     }
Jan 18 '09 #2
Frinavale
9,735 Expert Mod 8TB
When you updated the row, did you update the XML file you are using as a dependency?

Have you seen these articles
According to the second article, you can have an item evicted from the cache when a file changes....once the item is evicted you can recreate it.
Jan 19 '09 #3
Frinavale
9,735 Expert Mod 8TB
In your code for updating:

You are updating the data source and then when you are finished you are binding (different/old) data to the GridView.

When you are binding the data to your GridView you are retrieving the data stored in Cache, not the data that you have just updated.

After updating, the data source will be evicted from Cache the next time someone attempts to access the page because you have changed the source. Since it's evicted, it will appear as Null/Nothing during the next request...when this happens your code is recreating the cached version of your data source.


Therefore, I would recommend not using the bindgrid() method when you are updating the row....you need to deal with the data source instead of the cached version of the data source in this section of your code.
Jan 19 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: abdoly | last post by:
i wrote a code to update datagrid with the datagrid updatecommand but i cant get the updated values after being update that is the code private void DataGrid1_UpdateCommand(object source,...
4
by: SMG | last post by:
Hi there, I want to update my cached dataset, if there is any change in database. I know this is possible in case of ASP.Net 2.0 , But how do I execute the same task with ASP.Net 1.0 As there...
20
by: Janaka | last post by:
I tend to use the SqlDataAdapter class in WinForms projects because once the inital data is loaded any modifications can be easily added/updated through a call to the Update() method. I've found...
6
by: Charts | last post by:
I used HttpContext.Current.Cache To cache data from database. The code is like that. public static DataView GetCategories() { if ( HttpContext.Current.Cache == null ) {...
1
by: William Sullivan | last post by:
I'm trying to nail down some issues with the cache in my application. Currently, I have an object that stands between my business logic and database logic called CacheLogic (cute, no?). ...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
5
by: Stan SR | last post by:
Hi, Some newbie questions.. :-) First, what is the namespace to use for the Cache class ? When I use this bit of code I get an error if (Cache==null) Cache.Insert("myUserList",userlist);...
2
by: hharry | last post by:
Hello All, I save a Hashtable of stock symbols + prices to the cache. The prices are updated every 30 minutes and saved to a database table. I would like to always have the latest prices in...
1
by: teenagelcruise | last post by:
hi, i have a problem with my code which is i cannot update and addnew data into the database but i can delete the data.plz give me an idea.this is my code that i wrote. <html> <head> <meta...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.