Reputation: 841
I am new to HttpContext.Current.Cache and I found some examples of implementing it and I am having some issues. Does anyone know what could be causing the problem? I may be using it totally wrong but I have done as much research as I can.
I am getting Value cannot be null.
for sda.fill(dt); so I assuming my DataTable is not creating a new instance.
Thanks
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
SqlCommand command = new SqlCommand(
"SELECT Sequence, Column_Name FROM dbo.SD_Fields", connection);
DataTable dt = HttpContext.Current.Cache["1234"] as DataTable;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = command;
sda.Fill(dt);
// SqlDependency.Stop(ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
return dt;
Upvotes: 0
Views: 822
Reputation: 100585
Most likley you are not adding value to the cache to start with. Somewhere in your code there should be call to put value in the cache (before trying to get one) like:
HttpContext.Current.Cache["1234"] = new DataTable(...);
Note that normally you check if you have cached value and if not present create new (or get value from wherever you need it). Possibly caching immediately after creation.
Upvotes: 1