c# - Why when I download gridview in pdf file, it shows error -
i want download pdf file contain pid, , month january december.
the month contain float number has 9 number after comma, makes gridview wider should be. , when download pdf file, shows error 'system.web.ui.htmltextwriter'
what happening here?
this code
connection.open(); sqlcommand dm = new sqlcommand("select id,sum (case when [month] = 1 demand else 0.0 end) january, sum(case when [month] = 2 demand else 0.0 end) february,sum(case when [month] = 3 demand else 0.0 end) march ,sum(case when [month] = 4 demand else 0.0 end) april ,sum(case when [month] = 5 demand else 0.0 end) may ,sum(case when [month] = 6 demand else 0.0 end) june ,sum(case when [month] = 7 demand else 0.0 end) july ,sum(case when [month] = 8 demand else 0.0 end) august ,sum(case when [month] = 9 demand else 0.0 end) september ,sum(case when [month] = 10 demand else 0.0 end) october ,sum(case when [month] = 11 demand else 0.0 end) november ,sum(case when [month] = 12 demand else 0.0 end) december reorder group id", connection); sqldatareader fdm = dm.executereader(); if (fdm.hasrows) { fdm.read(); gridview3.datasource = fdm; } connection.close(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment;filename=demand.pdf"); response.cache.setcacheability(httpcacheability.nocache); stringwriter wsfdm = new stringwriter(); htmltextwriter fdmws = new htmltextwriter(wsfdm); gridview3.allowpaging = false; gridview3.rendercontrol(fdmws); gridview3.headerrow.style.add("width", "20%"); gridview3.headerrow.style.add("font-size", "8x"); gridview3.style.add("text-decoration", "none"); gridview3.style.add("font-family", "arial, helvetica, sans-serif;"); gridview3.style.add("font-size", "8px"); stringreader fgdm = new stringreader(fdmws.tostring()); document documentpdf = new document(pagesize.a4, 10f, 10f, 10f, 0f); htmlworker htmlparser = new htmlworker(documentpdf); pdfwriter.getinstance(documentpdf, response.outputstream); documentpdf.open(); htmlparser.parse(fgdm); documentpdf.close(); response.write(documentpdf); response.end(); }
you have use stringwriter
object in stringreader
class instead of htmltextwriter
try this
connection.open(); sqlcommand dm = new sqlcommand("select id,sum (case when [month] = 1 demand else 0.0 end) january, sum(case when [month] = 2 demand else 0.0 end) february,sum(case when [month] = 3 demand else 0.0 end) march ,sum(case when [month] = 4 demand else 0.0 end) april ,sum(case when [month] = 5 demand else 0.0 end) may ,sum(case when [month] = 6 demand else 0.0 end) june ,sum(case when [month] = 7 demand else 0.0 end) july ,sum(case when [month] = 8 demand else 0.0 end) august ,sum(case when [month] = 9 demand else 0.0 end) september ,sum(case when [month] = 10 demand else 0.0 end) october ,sum(case when [month] = 11 demand else 0.0 end) november ,sum(case when [month] = 12 demand else 0.0 end) december reorder group id", connection); sqldatareader fdm = dm.executereader(); if (fdm.hasrows) { fdm.read(); gridview3.datasource = fdm; } connection.close(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment;filename=demand.pdf"); response.cache.setcacheability(httpcacheability.nocache); stringwriter wsfdm = new stringwriter(); htmltextwriter fdmws = new htmltextwriter(wsfdm); gridview3.allowpaging = false; gridview3.rendercontrol(fdmws); gridview3.headerrow.style.add("width", "20%"); gridview3.headerrow.style.add("font-size", "8x"); gridview3.style.add("text-decoration", "none"); gridview3.style.add("font-family", "arial, helvetica, sans-serif;"); gridview3.style.add("font-size", "8px"); stringreader fgdm = new stringreader(wsfdm.tostring()); document documentpdf = new document(pagesize.a4, 10f, 10f, 10f, 0f); htmlworker htmlparser = new htmlworker(documentpdf); pdfwriter.getinstance(documentpdf, response.outputstream); documentpdf.open(); htmlparser.parse(fgdm); documentpdf.close(); response.write(documentpdf); response.end(); }
source:
take @ here export-gridview-to-pdf
Comments
Post a Comment