简体   繁体   中英

Asp.net Caching Issue

I have cached a dataset which has "StoreId" column. When I want to export the dataset to Excel I suppose to remove the "StoreId" column from the dataset and Exprot.

Following is the code for Removing and Exporting to 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. Plz help me out.

Thanks in advance.

You will make your life easier if you never modify objects you put in Cache. Removing columns from a DataSet is not thread-safe, so if multiple requests access the Cache concurrently, you're in trouble.

In this case, I would create a clone of the DataSet and export the clone. To do so, use the DataSet.Copy method:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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