c# - How to create PDF Report with data from code in list form, not directly from database with Microsoft Report Viewer -
i using microsoft.report.viewer generate pdf in asp .net web api application. far when report data directly database, there no problem. problem is, when try create report subreport within , subreport data got code in list form, didn't expected result. create report subreport tutorial. when generate pdf, message .
here code :
my web api generate pdf :
[route("generatepdffordailyprogram")] [httpget] [allowanonymous] public async task<ihttpactionresult> generatepdffordailyprogram(int id) { string guid = guid.newguid().tostring(); string filename = string.concat("booking_no" + convert.tostring(id) + "_" + guid.toupper() + ".pdf"); string filepath = httpcontext.current.server.mappath("~/content/temp/" + filename); string name = request.requesturi.getleftpart(uripartial.authority) + system.configuration.configurationmanager.connectionstrings[system.configuration.configurationmanager.appsettings["customport"]]; string name2 = request.getrequestcontext().virtualpathroot; list<tourdestination> destination = new list<tourdestination>(); tourtransactionsummaryviewmodel summaryviewmodel = new tourtransactionsummaryviewmodel(); try { //summaryviewmodel = await calcsummaryviewmodel(transaction, summaryviewmodel, db); summaryviewmodel.dailyprograms = dailyprogram(id); destination = tourdestination(summaryviewmodel.dailyprograms); list < movement > movementlist = summaryviewmodel.dailyprograms.firstordefault().movements.tolist(); reports.reportgenerator.generatepdffordailyprogram(summaryviewmodel.dailyprograms, destination, filepath); //await reports.reportgenerator.generatepdfformovement(movementlist, filepath); httpresponsemessage result = null; result = request.createresponse(httpstatuscode.ok); result.content = new streamcontent(new filestream(filepath, filemode.open)); result.content.headers.contentdisposition = new contentdispositionheadervalue("attachment"); result.content.headers.contentdisposition.filename = filename; result.dispose(); string convertedfilepath = filepath.replace(@"\\", @"\"); } catch (exception ex) { return badrequest(ex.message); } return ok(name + name2 + "/content/temp/" + filename); }
as can see code above, use generatepdffordailyprogram method generate pdf.
here generatepdffordailyprogram method :
public static bool generatepdffordailyprogram(tourtransactionsummaryviewmodel.dailyprogram[] dailyprograms, list<tourdestination> destination , string filepath) { try { string binpath = system.io.path.combine(system.appdomain.currentdomain.basedirectory, "bin"); var assembly = assembly.load(system.io.file.readallbytes(binpath + "\\tripplannerapi.dll")); using (stream stream = assembly.getmanifestresourcestream(dailyprogramreport)) { var viewer = new reportviewer(); viewer.localreport.enableexternalimages = true; viewer.localreport.loadreportdefinition(stream); warning[] warnings; string[] streamids; string mimetype; string encoding; string filenameextension; viewer.localreport.subreportprocessing += new microsoft.reporting.webforms.subreportprocessingeventhandler(localreport_subreportprocessing); viewer.localreport.datasources.add(new reportdatasource("dailyprogram", dailyprograms)); byte[] bytes = viewer.localreport.render( "pdf", null, out mimetype, out encoding, out filenameextension, out streamids, out warnings); using (filestream fs = new filestream(filepath, filemode.create)) { fs.write(bytes, 0, bytes.length); fs.flush(); } stream.flush(); } } catch (exception ex) { return false; } return true; }
to run subreport, use eventhandler code :
private static void localreport_subreportprocessing(object sender, subreportprocessingeventargs e) { datetime movementdate = convert.todatetime(e.parameters[0].values[0]); tourtransactionscontroller controller = new tourtransactionscontroller(); var movement = controller.movements(); list<movement> movementlist = new list<movement>(); movementlist.add(new movement { destination = "test", movementdescription = "test", datetime = convert.todatetime("2017-09-25") //hardcode test }); e.datasources.clear(); e.datasources.add(new microsoft.reporting.webforms.reportdatasource() { name = "dsmovements", value = movementlist }); //throw new notimplementedexception(); }
what did try :
- check sub report name, , change report name report file url. still same message.
- generate sub report directly, not main report. subreport generated.
ps : when debug code, , put breakpoint in event handler localreport_subreportprocessing
breakpoint not hitting while debugging
my questions :
- why breakpoint on eventhandler not hitting while debugging ?
- is possible eventhandler not hitting while debugging, reason got message
the subreport 'movementreport' not found @ specified location movementreport. please verify subreport has been published , name correct.
? - is there other way me create main report data db, , subreport data code in list form ?
any appreciated.
Comments
Post a Comment