Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to implement a sqlDependency task, which can automatically refresh the page, whenever I insert new data into my desired database table.

The code below, is not refreshing the content automatically, when I insert new data into the database table. if I refresh it the page, manually, then the content gets updated.
C#
protected void Page_Load(object sender, EventArgs e)
  {
      Label1.Text = "Cache Refresh: " +
      DateTime.Now.ToLongTimeString();

      // Create a dependency connection to the database.
      SqlDependency.Start(GetConnectionString());

      using (SqlConnection connection =
          new SqlConnection(GetConnectionString()))
      {
          using (SqlCommand command =
              new SqlCommand(GetSQL(), connection))
          {
              SqlCacheDependency dependency =
                  new SqlCacheDependency(command);
              // Refresh the cache after the number of minutes
              // listed below if a change does not occur.
              // This value could be stored in a configuration file.
              int numberOfMinutes = 1;
              DateTime expires =
                  DateTime.Now.AddMinutes(numberOfMinutes);

              Response.Cache.SetExpires(expires);
              Response.Cache.SetCacheability(HttpCacheability.Public);
              Response.Cache.SetValidUntilExpires(true);

              Response.AddCacheDependency(dependency);

              connection.Open();

              GridView1.DataSource = command.ExecuteReader();
              GridView1.DataBind();
          }
      }
  }


Am I doing something wrong in my code or is there extra method I require for this task. Any hints would be most appreciated. Thank you.
Posted
Comments
ChintanShukla 30-Sep-14 6:31am    
Page wont get refreshed automatically even tho the cache is updated. You need to explicitally refresh the page I guess. Use Response.Redirect after updation

To implement SQL dependency you needs to add paramter in Config as well.
 
Share this answer
 
Hi,

Here is a very good article on SQL Dependency : Query Notification using SqlDependency and SqlCacheDependency[^]

If you required run time data push at client you can use SignalR :http://www.asp.net/signalr[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900