473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

update cache object when update sql database

8 New Member
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 5413
mrutyunjaya
8 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
7664
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, System.Web.UI.WebControls.DataGridCommandEventArgs e) TextBox temp temp=(TextBox)e.Item.Cells.Controls String str=temp.Text; str always returnt the old value of the cell (before being updated) would u plz tell me about whats wrong i m doin
4
2186
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 is need, if any change in database/tables happens then my dataset should be updated. Any help on this,
20
1796
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 in aspnet that the same thing is possible but because of the stateless nature of the web in order to do this I have to go through the following steps: 1. First visit to page loads DataSet and binds DataList with data 2. User edits data and...
6
1981
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 ) { HttpContext.Current.Cache = GetCategoriesFromDB(); } return (DataView)HttpContext.Current.Cache;}
1
2701
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?). Global.asax.cs creates it in Application_Start, initializes it and places it in the cache. During initialization, CacheLogic retrieves data from the DB logic layer and caches it with removal callbacks set. Whenever an object in the business logic layer...
30
3377
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 commits it. How does the other user get the updated view without polling for changes? Is there some sort of callback mechanism that can be set up on the dataset or connection? TIA
5
2109
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); I don't know which namespace to use.
2
1504
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 the cache...is there a way to dynamically update a cache object ? Pointers appreciated
1
2456
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 http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Order Record</title> <meta name="Microsoft Border" content="tlb, default"> </head>
0
8059
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8330
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.