Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am caching my xml data into a data set as given below:
C#
public static void LoadCache()
{
    static DataSet ds = new DataSet();
    ds.ReadXml(HttpContext.Current.Server.MapPath("myXML.xml"));
    HttpRuntime.Cache.Insert("myCache", ds,null, Cache.NoAbsoluteExpiration,
                             Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}

//Method to Call Cache
public DataSet GetXMLData()
{
    if (HttpRuntime.Cache["myCache"] == null)
    {
        LoadCache();
    }
    return (DataSet)HttpRuntime.Cache["myCache"];
}


Now I have a dataset dsData In my page load that is filled from this cache and I do the following:
C#
DataView dv = dsData.Tables[0].DefaultView;
dv.RowFilter = "key=0";
DataTable dtTemp = dv.ToTable();
dtTemp.Columns.Add("sort"); //Works fine only once


The above chunk works only for once, when I refresh my page it shows Column with same name already exist in dtTemp.

Also when I tried Debugging I found Out that The Cache Dataset ds also contains same column.
I am unable to figure out why this happens.
Posted
Updated 1-Sep-14 22:37pm
v3

I revised my first answer as it did not answer the question.
I still don't have an answer, after trying the code below.
It works fine as far as I can see.

C#
static DataSet ds = new DataSet();
public static void LoadCache()
{

    DataTable dt = ds.Tables.Add();
    dt.Columns.Add("key", typeof(int));
    dt.Columns.Add("name", typeof(string));

    dt.Rows.Add(0, "Donald");
    dt.Rows.Add(0, "Duck");
    dt.Rows.Add(1, "Mickey");
    dt.Rows.Add(1, "Mouse");

    HttpRuntime.Cache.Insert("myCache", ds, null, Cache.NoAbsoluteExpiration,
                              Cache.NoSlidingExpiration, CacheItemPriority.High, null);
}

public DataSet GetXMLData()
{
    if (HttpRuntime.Cache["myCache"] == null)
    {
        LoadCache();
    }
    return (DataSet)HttpRuntime.Cache["myCache"];
}

public void GetData()
{
    DataView dv = GetXMLData().Tables[0].DefaultView;
    dv.RowFilter = "key=0";
    DataTable dtTemp = dv.ToTable();
    dtTemp.Columns.Add("sort");
}

private void SomeFunction()
{
    // Call GetData several times.
    GetData();
    GetData();
    GetData();
}


As I don't know the structure of your dataset, I cannot do much more testing.
There are also other differences that might be a factor.
 
Share this answer
 
v3
Comments
ChintanShukla 2-Sep-14 2:40am    
I tried this but Problem still exists as LoadCache will be called once and HttpRuntime.Cache["myCache"] will be called every time as it wont be nul the next time
George Jonsson 2-Sep-14 4:31am    
Sorry, I misunderstood your question.
Let me see if I can revise my answer.
ChintanShukla 2-Sep-14 5:36am    
Thanks for the help. It works fine in the above case but not working when I Load XML.
Does Default View has some reference with the dataset made by loading xml? For time being i just used another dataset dts = GetXMLData(); and copied dsData = dts.Copy();
It worked fine
George Jonsson 2-Sep-14 5:54am    
This is probably the problem. However, if you look up the method DataView.ToTable() it states that it creates a new table, hence I didn't think this would be the problem.
set

HttpRuntime.Cache["myCache"] = null;

after adding a column.
 
Share this answer
 
Comments
ChintanShukla 2-Sep-14 4:18am    
If I do so then every time it will call LoadCache() and hence Read XML on every page load. Then whats the point using cache. I can Directly write loadXML on pageload. I want to use caching to avoid xml read every time.

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