views:

769

answers:

3

I want to load the data into session so that when the next button is clicked in crystal report viewer then in should load the data from the datatable instead retrieving the data again from the database. Here goes my code...

   ReportDocument rpt = new ReportDocument();
    DataTable resultSet = new DataTable();
    string reportpath = null;

   protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {

           if (Request.QueryString.Get("id") == "5")
            {
                string publication = Request.QueryString.Get("pub");
                DateTime date = DateTime.Parse(Request.QueryString.Get("date"));
                int pages = int.Parse(Request.QueryString.Get("pages"));
                int sort = int.Parse(Request.QueryString.Get("sort"));
                if (sort == 0)
                {
                    reportpath = Server.MapPath("IssuesReport.rpt");
                    rpt.Load(reportpath);
                    DataTable resultSet1 = RetrievalProcedures.IssuesReport(date,          publication, pages);
                Session["Record"] = resultSet1;
             }

          DataTable report = (DataTable)Session["Record"];
          rpt.SetDataSource(report);
          CrystalReportViewer1.ReportSource = rpt;

I am trying this code but when i clicked the next button it gives me the error that invalid report source..i guess the session is null thats why its giving me this error.

Any sugesstions how can I solve this...

+1  A: 

I think you'd want to use the Cache object with a unique key for each user instead of Session here.

Pseudo code:

var data = Cache["Record_999"] as DataTable;
if (data == null) {
    // get from db
    // insert into cache
}
SetDataSource(data);
John Sheehan
Cache is shared across requests, so requests from other people would result in retrieving a cached data set created with different parameters. You can use the cache if you apply Select() method filtering after retrieving the data from it. Session is what you'd want to use based on the code above.
cfeduke
Sorry, I meant to say Cache with a unique key for each user
John Sheehan
John, could you explain why the session is not available in this context? (I assume it isn't and that's why you're emulating it with the user-keyed cache)
kristian
It's available, but it's also more volatile than Cache. If he closes his browser, etc, it's gone.
John Sheehan
A: 

The problem lies not in with using Session, it lies with the logic used to determine when to retrieve data. Session is the correct approach to use here as Cache is shared across requests - that is, User A would see the report User B just configured if User B was the first user to execute code that used Cache instead of Session.

if (!Page.IsPostBack)
{
    if (Request.QueryString.Get("id") == "5")
    {
        string publication = Request.QueryString.Get("pub");
        DateTime date = DateTime.Parse(Request.QueryString.Get("date"));
        int pages = int.Parse(Request.QueryString.Get("pages"));
        int sort = int.Parse(Request.QueryString.Get("sort"));
        // fixed the statement below to key off of session
        if (Session["Record"] == null)
        {
            reportpath = Server.MapPath("IssuesReport.rpt");
            rpt.Load(reportpath);
            Session["Record"] = RetrievalProcedures.IssuesReport(date, publication, pages);
         }

         rpt.SetDataSource((DataTable)Session["Record"]);
         CrystalReportViewer1.ReportSource = rpt;
         // ....
    }
}
cfeduke
A: 

`Could it be that sort is not 0? If sort is not 0 and the user is accessing the page for the first time(Session["Record"] has not been set before) he might get the error. might want to try:

if(sort==0 || Session["Record"] == null)
{
// do your magic
}