1

How can I pass a dataset to a generic handler? I don't want to use session or viewstate to do that.

I'm trying to return an excel file converted from the input dataset as the response.

I'm already showing the dataset content as a report in one grid filtered with some criteria set by user. Since the query is expensive, I don't want to execute the same in the handler too.

It would be even better if I could pass the dataset by reference to the handler?

Sen Jacob
  • 3,384
  • 3
  • 35
  • 61
  • convert dataset to xml schema and send it to generic hendelar via post might be it works – Anant Dabhi Nov 06 '12 at 12:05
  • as I commented in reply to ArseMkrt, the dataset is of large size, so converting it to xml schema, retrieving it and process the excel conversion all takes much time.. that is why I like to pass it as reference. – Sen Jacob Nov 06 '12 at 13:01

2 Answers2

0

You can't pass it, you need to cache the result

Any reason you don't want to use session?

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • I want the dataset to be passed only once and the dataset will be of large size. So I feel adding it to session or HttpContext Items is not a good idea since it might keep that until the session ends. – Sen Jacob Nov 06 '12 at 11:58
  • You know, I think sending big dataset back converting it to excel and send it back will take longer than to run a query on server side with the same conditions, or to cache it and send it again... if you don't want to cache in session because of memory usage, you can dump it to file and delete file on next request of the same screen... – Arsen Mkrtchyan Nov 06 '12 at 14:43
  • I could take the dataset from already bound gridview's datasource. So it might not take much time to pass the same object to somewhere in the same web application, right? – Sen Jacob Nov 06 '12 at 14:52
  • Is it possible to set the dataset to any internal property of the handler? I mean something like create a handler object, assign the dataset and invoke it.. ? – Sen Jacob Nov 06 '12 at 14:54
  • yes, you can, but you need to keep it until next user request right? ASP.Net doesn't keep any state, so it will be lost. an on the other hand, if you even assign to some property and want to keep it in memory, it is the same as to keep it in session, because again you will not know if it will be called or not, and when to delete... – Arsen Mkrtchyan Nov 06 '12 at 15:05
  • I don't want to keep that.. once user clicks the download link, I'm taking the current gridview datasource and sending the dataset to the handler for converting it to excel. Can't I inherit the IDisposible for the handler object and free up memory after finishing the request ? If I can pass the dataset like that, could you please suggest any example link with related code snippet? – Sen Jacob Nov 06 '12 at 15:05
  • one more question, where do you have current gridview? do you want to do a query for getting the datagrid again and pass it to handler? – Arsen Mkrtchyan Nov 06 '12 at 15:09
  • the gridview is in a page on my web application, the same page is having a download button, on click event of the download button, I'm taking the gridview's datasource for conversion. so I don't think there will be another query executing. Grid binding only happens when user filters the data for displaying into gridview. – Sen Jacob Nov 06 '12 at 15:13
  • 1
    I believe you can create a property in your handle class, and to convert handle you have to concrete type and assign the property data-source you have – Arsen Mkrtchyan Nov 06 '12 at 15:28
0

I couldn't find any way to pass dataset to ashx other than creating an object of the handled class and assign to it. Then I called the object's ProcessRequest method. It worked fine and the build was succeeded. But when I tried to publish the website, the code gave error that the classname could not be found. The code is just like given below.

//this is working fine in VS (build succeeded and ProcessRequest returned the file!) 
//but didn't compile only on publishing
MyHandler handlerObj = new myHandler();
handlerObj.DataSource = myDataset;
handlerobj.ProcessRequest(Context);

I solved the problem by replacing the generic handler with a normal c# class which has a method accepting the context and dataset. Then I called the method in the class, where I wrote my file stream into the Context.Response and it worked just fine. The class did just the same as the handler and no need to inherit from IHttpContext

public void ProcessDownload(HttpContext context, DataSet DataSource)
{
    context.Response.Clear();
    context.Response.ContentType = "application/vnd.ms-excel";
    MemoryStream file = getExcelMemStream(DataSource);
    context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "myFile.xls"));
    context.Response.BinaryWrite(file.GetBuffer());
    context.Response.End();
}
Sen Jacob
  • 3,384
  • 3
  • 35
  • 61