简体   繁体   English

Asp.net缓存问题

[英]Asp.net Caching Issue

I have cached a dataset which has "StoreId" column. 我已经缓存了具有“ StoreId”列的数据集。 When I want to export the dataset to Excel I suppose to remove the "StoreId" column from the dataset and Exprot. 当我想将数据集导出到Excel时,我想从数据集和Exprot中删除“ StoreId”列。

Following is the code for Removing and Exporting to Excel. 以下是用于删除和导出到Excel的代码。

if (HttpContext.Current.Cache["stores"] != null)
        {
            using (DataSet dsStores = (DataSet)HttpContext.Current.Cache["stores"])
            {
                if (TrainingUtil.isDataSetValid(dsStores))
                {
                    DataTable dt = dsStores.Tables[0];
                    dt.Columns.Remove("storeId");
                    Quality.Qulaity_Utility.ExportDataSet(dt, ddlCity.SelectedItem.Text.ToString() + "_StoreCodes");
                }

            }
        }

  public static void ExportDataSet(DataTable dt,string filename)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/vnd.xls";
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename.Replace(" ", "_").ToString() + ".xls");

        DataGrid dgRecord = new DataGrid();
        //Color Setttings
        dgRecord.HeaderStyle.BackColor = System.Drawing.Color.Cyan;

        dgRecord.DataSource = dt;
        dgRecord.DataBind();

        //Cells color settings
        foreach (DataGridItem dgi in dgRecord.Items)
        {
            foreach (TableCell tcGridCells in dgi.Cells)
            {
                tcGridCells.Attributes.Add("class", "sborder");
            }
        }
        //Render the datagrid

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        dgRecord.RenderControl(htmlTextWriter);
        //lstMonthlyReport.RenderControl(htmlTextWriter);
        //Add the style sheet class here
        HttpContext.Current.Response.Write(@"<style> .sborder { color : Black;border : 1px Solid Black; } </style> ");
        //Export
        HttpContext.Current.Response.Write(stringWriter.ToString());
        //End
        HttpContext.Current.Response.End();
        //style to format numbers to string
        //string style = @"<style> body { mso-number-format:\@; } </style>";
    }
}

after exporting the data and When I once againg want Stores information from Cached dataset I was unable to find the "StoreId" Column, I'm unable figure out where I'm doing wrong. 导出数据后,当我再次想要从缓存的数据集中存储信息时,我找不到“ StoreId”列,我无法弄清楚我在哪里做错了。 Plz help me out. 请帮我。

Thanks in advance. 提前致谢。

You will make your life easier if you never modify objects you put in Cache. 如果您从不修改放置在Cache中的对象,将使您的生活更加轻松。 Removing columns from a DataSet is not thread-safe, so if multiple requests access the Cache concurrently, you're in trouble. 从DataSet中删除列不是线程安全的,因此,如果多个请求同时访问Cache,则会遇到麻烦。

In this case, I would create a clone of the DataSet and export the clone. 在这种情况下,我将创建数据集的副本并导出该副本。 To do so, use the DataSet.Copy method: 为此,请使用DataSet.Copy方法:

DataSet dsStores = ((DataSet)HttpContext.Current.Cache["stores"]).Copy()

Or find a way of doing the export that doesn't require you to modify the DataSet . 或者找到一种不需要修改DataSet的导出方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM