c# - How to retrieve a single row from database and show it up in bill which is in .pdf form in asp.net? -


hi working on project in have button in grid view named generate bill , when clicked on generate bill button able download .pdf file .the problem facing getting rows retrieved table need need selected row of generate bill button clicked . here code :

[front end code]:

 <form id="form1" runat="server"> <div> </div> <table border="0" width="90%" height="300px" align="center" class="tableshadow">     <tr>         <td class="toptd" style="color: white; font-size: 24px; text-align: center; height: 40px;             background-color: #60b2e7">             view enquiry         </td>     </tr>     <tr>         <td align="center" valign="top" style="padding-top: 10px;">             <table border="0" align="center" width="95%">                 <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" onrowcommand="gridview1_rowcommand"                     font-bold="true" font-size="small" gridlines="none">                     <columns>                         <asp:boundfield itemstyle-width="150px" datafield="enquiryid" headertext="id">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="packname" headertext="package">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="name" headertext="name">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="gender" headertext="gender">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="mobileno" headertext="mobile no">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="email" headertext="email">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="noofdays" headertext="no. of days">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="child" headertext="no. of children">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:boundfield itemstyle-width="150px" datafield="adults" headertext="no of adults">                             <itemstyle width="150px"></itemstyle>                         </asp:boundfield>                         <asp:templatefield headertext="status field">                             <itemtemplate>                                 <asp:button id="button1" runat="server" causesvalidation="false" commandname="mycommand"                                     commandargument='<%#eval("enquiryid")%>' text='<%# bind("statusfield")%>' borderstyle="none"                                     font-bold="true" />                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield>                             <itemtemplate>                                 <asp:button id="button2" runat="server" causesvalidation="false" borderstyle="none"                                     onclick="generate_bill" text="generate bill" />                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield>                             <itemtemplate>                                 <asp:button id="button3" runat="server" causesvalidation="false" borderstyle="none"                                     text="delete enquiry" />                             </itemtemplate>                         </asp:templatefield>                     </columns>                 </asp:gridview>                 <tr>                     <td>                         <br />                     </td>                 </tr>             </table>         </td>     </tr> </table> </form> 

[back end code]:

  protected void page_load(object sender, eventargs e) {     if (!ispostback)     {         list<string> lst = new list<string>() { "asd", "xxx" };         gridview1.datasource = lst;         this.bindgrid();     } } private void bindgrid() {      using (sqlconnection con = new sqlconnection(@"data source=.\sqlexpress;attachdbfilename=d:\project sem6\online tours , travels\app_data\toursandtravels.mdf;integrated security=true;user instance=true"))     {          using (sqlcommand cmd = new sqlcommand("select enquiryid,packname,name, gender,mobileno,email,noofdays,child,adults,statusfield enquiry"))         {             using (sqldataadapter sda = new sqldataadapter())             {                 cmd.connection = con;                 sda.selectcommand = cmd;                 using (datatable dt = new datatable())                 {                     sda.fill(dt);                     gridview1.datasource = dt;                     gridview1.databind();                 }             }         }     } } protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e) {     if (e.commandname == "mycommand")     {         int enquiryid = int32.parse(e.commandargument.tostring());          button button1 = (button)e.commandsource;         if (button1 != null)             button1.text = "confirm";         string status = button1.text.tostring();         string query = string.format("update enquiry set statusfield= '{0}' enquiryid={1}", status, enquiryid);          using (sqlconnection con = new sqlconnection(@"data source=.\sqlexpress;attachdbfilename=d:\project sem6\online tours , travels\app_data\toursandtravels.mdf;integrated security=true;user instance=true"))         {             sqlcommand comm = new sqlcommand(query, con);             con.open();             comm.executenonquery();         }     }    } protected void generate_bill(object sender, eventargs e) {      string companyname = "dream holidays";     int enquiryid = 1234;     sqlconnection con = new sqlconnection(@"data source=.\sqlexpress;attachdbfilename=d:\project sem6\online tours , travels\app_data\toursandtravels.mdf;integrated security=true;user instance=true");     sqlcommand cmd = new sqlcommand("select e.[enquiryid],e.[packname],e.[name],e.[mobileno],e.[email],p.[packageprice] [enquiry] e inner join package p on e.[packname]=p.[packname]");     sqldataadapter sda = new sqldataadapter();     cmd.connection = con;     sda.selectcommand = cmd;     datatable dt = new datatable();     sda.fill(dt);       gridview1.datasource = dt;       using (stringwriter sw = new stringwriter())     {         using (htmltextwriter hw = new htmltextwriter(sw))         {             stringbuilder sb = new stringbuilder();              //generate invoice (bill) header.             sb.append("<table width='100%' cellspacing='0' cellpadding='2'>");             sb.append("<tr><td align='center' style='background-color: #18b5f0' colspan = '2'><b>invoice</b></td></tr>");             sb.append("<tr><td colspan = '2'></td></tr>");             sb.append("<tr><td><b>enquiry id: </b>");             sb.append(enquiryid);             sb.append("</td><td align = 'right'><b>date: </b>");             sb.append(datetime.now);             sb.append(" </td></tr>");             sb.append("<tr><td colspan = '2'><b>company name: </b>");             sb.append(companyname);             sb.append("</td></tr>");             sb.append("</table>");             sb.append("<br />");              //generate invoice (bill) items grid.             sb.append("<table border = '1'>");             sb.append("<tr>");             foreach (datacolumn column in dt.columns)             {                 sb.append("<th style = 'background-color: #d20b0c;color:#ffffff'>");                 sb.append(column.columnname);                 sb.append("</th>");             }             sb.append("</tr>");             foreach (datarow row in dt.rows)             {                 sb.append("<tr>");                 foreach (datacolumn column in dt.columns)                 {                     sb.append("<td>");                     sb.append(row[column]);                     sb.append("</td>");                 }                 sb.append("</tr>");             }             sb.append("<tr><td align = 'right' colspan = '");             sb.append(dt.columns.count - 1);             sb.append("'>package price:</td>");             sb.append("<td>");             sb.append(dt.compute("sum(packageprice)", ""));             sb.append("</td>");             sb.append("</tr></table>");              //export html string pdf.             stringreader sr = new stringreader(sb.tostring());             document pdfdoc = new document(pagesize.a4, 10f, 10f, 10f, 0f);             htmlworker htmlparser = new htmlworker(pdfdoc);             pdfwriter writer = pdfwriter.getinstance(pdfdoc, response.outputstream);             pdfdoc.open();             htmlparser.parse(sr);             pdfdoc.close();             response.contenttype = "application/pdf";             response.addheader("content-disposition", "attachment;filename=invoice_" + enquiryid + ".pdf");             response.cache.setcacheability(httpcacheability.nocache);             response.write(pdfdoc);             response.end();         }     } } 

pdf downloading looks this

you should not use separate click event generatebill button. set commandname "generatebill" , commandargument enquiryid of generatebill button , use them in gridview1_rowcommand event single row db , print pdf data.

change generatebill button html following.

<asp:templatefield>     <itemtemplate>         <asp:button id="button2" runat="server" causesvalidation="false"             borderstyle="none" commandargument='<%#eval("enquiryid")%>'              commandname="generatebill" text="generate bill" />     </itemtemplate> </asp:templatefield> 

change gridview1_rowcommand event handle commandargument "generatebill" too.

protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e) {     if (e.commandname == "mycommand")     {         //existing code related "mycommand"     }     //handle "generatebill" command following.     else if(e.commandname == "generatebill")     {         int enquiryid = int32.parse(e.commandargument.tostring());         generatebill(enquiryid) //calling generate bill method enquiryid.     } } 

change

protected void generate_bill(object sender, eventargs e) 

as following.

protected void generatebill(int enquiryid) {     string companyname = "dream holidays";     sqlconnection con = new sqlconnection(@"data source=.\sqlexpress;         attachdbfilename=d:\project sem6\online tours , travels\app_data\toursandtravels.mdf;         integrated security=true;user instance=true");      sqlcommand cmd = new sqlcommand("select e.[enquiryid],e.[packname],         e.[name],e.[mobileno],e.[email],p.[packageprice]          [enquiry] e inner join package p on e.[packname]=p.[packname]          e.[enquiryid] = @enquiryid");      sqldataadapter sda = new sqldataadapter();     cmd.connection = con;     cmd.parameters.add(new sqlparameter("@enquiryid", enquiryid));     sda.selectcommand = cmd;     datatable dt = new datatable();     sda.fill(dt);      // rest of code of printing pdf } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -