Hi, i have a Devexpress grid in SharePoint, where i am pulling data from the Database. But the data updates daily to the database and i have written code to "cache dataset" to refresh grid for every 2 hours after database is updated, but looks like grid is not refreshing. But when i recycle app pool then it is working. Here is my code, but same code is working in my development. Please let me know to make any changes to code to refresh the grid for every 2 hours
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Runtime.Caching;
using System.Web.Caching;
namespace Grid
{
public class GridDS
{
private bool _Loaded = false;
private DataSet _ListDS = null;
private string _ConnStr = string.Empty;
public DataSet ListDS
{
get
{
return _ListDS;
}
}
public bool IsLoaded
{
get
{
return _Loaded;
}
}
public GridDS(string ConnStr)
{
_ConnStr = ConnStr;
Loadtable();
}
private void Loadtable()
{
ObjectCache cache = MemoryCache.Default;
string cacheKey = "RefreshData";
DataSet dsreturn = new DataSet();
if (cache.Contains(cacheKey))
{
dsreturn = (DataSet)cache.Get(cacheKey);
if (dsreturn != null && dsreturn.Tables[0].Rows.Count > 0)
{
dsreturn.Tables[0].PrimaryKey = new DataColumn[1] { dsreturn.Tables[0].Columns[0] };
_ListDS = dsreturn;
}
}
else
{
dsreturn = LoadListDS();
CacheItemPolicy itemPolicy = new CacheItemPolicy();
itemPolicy.AbsoluteExpiration = DateTime.Now.AddHours(2.0);
cache.Add(cacheKey, dsreturn, itemPolicy);
}
}
private DataSet LoadListDS()
{
try
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
SqlCommand cmd = new SqlCommand("Users", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(ds);
con.Close();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
ds.Tables[0].PrimaryKey = new DataColumn[1] { ds.Tables[0].Columns[0] };
_ListDS = ds;
}
return ds;
}
catch (Exception ex)
{
throw new Exception("Error loading grid from db: " + ex.Message);
}
}
}