0% found this document useful (0 votes)
107 views

Generate Invoice in PDF

The document defines methods for generating a PDF invoice, retrieving order details from a database, and displaying order details in a grid view. It includes namespaces for database access, PDF generation, and HTML parsing. The exportpdf method generates a PDF from HTML panel content. The findorderdate and findaddress methods retrieve order date and address from the database. The showgrid method populates a data table from order details and binds it to a grid view, calculating totals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Generate Invoice in PDF

The document defines methods for generating a PDF invoice, retrieving order details from a database, and displaying order details in a grid view. It includes namespaces for database access, PDF generation, and HTML parsing. The exportpdf method generates a PDF from HTML panel content. The findorderdate and findaddress methods retrieve order date and address from the database. The showgrid method populates a data table from order details and binds it to a grid view, calculating totals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Important Namespaced Used

---------------------------

using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

---------------------------

Pageload Coding
---------------------------

Label3.Text = Request.QueryString["orderid"];
Panel1.Visible = true;
Label5.Text = Label3.Text;
findorderdate(Label5.Text);
findaddress(Label5.Text);
showgrid(Label5.Text);

----------------------------

Download Invoice PDF Button Click Event Coding


-----------------------------------------------

exportpdf();

--------------------------------------------------

Userdefined Methods Coding


----------------------------------------------------

private void exportpdf()


{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=OrderInvoice.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
private void findorderdate(String Orderid)
{
String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial
Catalog=ShoppingData; Integrated Security=True";
String myquery = "Select * from OrderDetails where orderid='" +
Orderid+"'";
SqlConnection con = new SqlConnection(mycon);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{

Label6.Text = ds.Tables[0].Rows[0]["dateoforder"].ToString();

con.Close();
}
private void findaddress(String Orderid)
{
String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial
Catalog=ShoppingData; Integrated Security=True";
String myquery = "Select * from OrderAddress where orderid='" + Orderid +
"'";
SqlConnection con = new SqlConnection(mycon);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{

Label7.Text = ds.Tables[0].Rows[0]["address"].ToString();

con.Close();
}
private void showgrid(String orderid)
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add("sno");
dt.Columns.Add("productid");
dt.Columns.Add("productname");
dt.Columns.Add("quantity");
dt.Columns.Add("price");
dt.Columns.Add("totalprice");
String mycon = "Data Source=HP-PC\\SQLEXPRESS;Initial
Catalog=ShoppingData;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from orderdetails where orderid='" +
orderid+"'";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
int totalrows = ds.Tables[0].Rows.Count;
int i = 0;
int grandtotal = 0;
while(i<totalrows)
{
dr = dt.NewRow();
dr["sno"] = ds.Tables[0].Rows[i]["sno"].ToString();
dr["productid"] = ds.Tables[0].Rows[i]["productid"].ToString();
dr["productname"] = ds.Tables[0].Rows[i]["productname"].ToString();
dr["quantity"] = ds.Tables[0].Rows[i]["quantity"].ToString();
dr["price"] = ds.Tables[0].Rows[i]["price"].ToString();
int price = Convert.ToInt16(ds.Tables[0].Rows[i]["price"].ToString());
int quantity = Convert.ToInt16(ds.Tables[0].Rows[i]
["quantity"].ToString());
int totalprice = price * quantity;
dr["totalprice"] = totalprice;
grandtotal = grandtotal + totalprice;
dt.Rows.Add(dr);
i = i + 1;
}
GridView1.DataSource = dt;
GridView1.DataBind();

Label9.Text = grandtotal.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

--------------------------------

You might also like