Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi my name is vishal for past 10 days i was breaking my head on how to create PDF report from c# windows forms with sql server data using itextsharp.

I have two tables in sql server 2008.
table name: Technician
Column Name DataType AllowNulls
technician_id(primary key) Int No
technician_dob date Yes
technician_sex nvarchar(10) Yes
row_upd_date datetime Yes
user_id Int Yes

table name:Techniciandetail
Column Name DataType AllowNulls

Technician_first_name nvarchar(50) Yes
Technician_middle_name nvarchar(50) Yes
Technician_last_name nvarchar(50) Yes
row_upd_date datetime Yes
status bit Yes
agn(primary key) Int No
technician_id Int Yes
user_id Int Yes
technician_type Int Yes

Given below is my c# code with sql server 2008 using itextsharp:
<pre lang="c#">
 using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data;
 using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DRRS_CSharp
{
    public partial class frmPDFTechnician : Form
    {
        public frmExcelTechnician()
        {
            InitializeComponent();
        }
        public DataTable GetDataTable()
        {

            DataTable table = new DataTable("Techniciandetail" + "Technician");
            DataColumn datacolumn_ID = new DataColumn("ID", typeof(Int32));
            table.Columns.Add(datacolumn_ID);
            DataColumn dataFname = new DataColumn("First Name", typeof(string));
            table.Columns.Add(dataFname);
            DataColumn dataMname = new DataColumn("Middle Name", typeof(string));
            table.Columns.Add(dataMname);
            DataColumn dataLname = new DataColumn("Last Name", typeof(string));
            table.Columns.Add(dataLname);
            DataColumn DOB = new DataColumn("DOB", typeof(DateTime));
            table.Columns.Add(DOB);
            DataColumn Sex = new DataColumn("Gender", typeof(string));
            table.Columns.Add(Sex);
            DataColumn Type = new DataColumn("Designation", typeof(Int32));
            table.Columns.Add(Type);
            DataRow drow = table.NewRow();
                string connect = "Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true";
               SqlConnection conn = new SqlConnection(connect);
                    string query = "Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1";
                    SqlCommand cmd = new SqlCommand(query, conn);
                      conn.Open();
                        SqlDataReader rdr = cmd.ExecuteReader();
                            while (rdr.Read())
                            {
                                drow["ID"] = rdr[0].ToString();
                                drow["First Name"] = rdr[1].ToString();
                                drow["Middle Name"] =rdr[2].ToString();
                                drow["Last Name"] =  rdr[3].ToString();
                                drow["DOB"] = rdr[4].ToString();
                                drow["Gender"] = rdr[5].ToString();
                                drow["Designation"] = rdr[6].ToString();
                            }
                            table.AcceptChanges();
                            return table;
                        }
private void btnExport_Click(object sender, EventArgs e)
        {
            Document doc = new Document(PageSize.A4.Rotate());
                PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
                doc.Open();
                Font font10 = FontFactory.GetFont("Arial", 10, BaseColor.BLACK);
                Paragraph para = new Paragraph("Technician Details in PDF Document");
                DataTable dt = GetDataTable();
                    PdfPTable table = new PdfPTable(dt.Columns.Count);
                    PdfPCell cell = null;
                    cell = new PdfPCell(new Phrase(new Chunk("ID", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("First Name", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("Middle Name", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("Last Name", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("DOB", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("Gender", font10)));
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase(new Chunk("Designation", font10)));
                    table.AddCell(cell);
                    for (int rows = 0; rows < dt.Rows.Count; rows++)
                    {
                        for (int column = 0; column < dt.Columns.Count; column++)
                        {
                            cell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font10)));
                            table.AddCell(cell);
                        }
                    }
                    table.SpacingBefore = 15f;
                    doc.Add(para);
                    doc.Add(table);
                doc.Close();
            }

Given below is my sql select query:
"Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1";

The above query executes to some extent as i get column names in table in PDF report. But the problem is that i get column names only and below it is not being filled by sql data. I know there is some mistake in public DataTable GetDataTable() function in my coding but i dont know how to fix it?
What i want is when user click btnExport i want datas/values from Technician table-technician_id,technician_dob,technician_sex and values from Techniciandetail table-Technician_first_name,Technician_middle_name,Technician_last_name and technician_type to be present or embedded or binded into appropriate columns in PDF Report.
Can anyone help me please! I have tried through net but with no success. Any help or guidance in solving this problem would be greatly appreciated.
Posted
Comments
Maciej Los 3-Apr-14 16:37pm    
What kind of problem? What's the question?

you can follow this link for help in your code, if you still stuck somewhere then post your problem here.

http://www.aspsnippets.com/Articles/How-to-generate-and-download-PDF-Report-from-database-in-ASPNet-using-iTextSharp-C-and-VBNet.aspx[^]
 
Share this answer
 
C#
private void btnExport_Click(object sender, EventArgs e)
        {
           
           string SqlQuery = "select * from Techniciandetail";
           SqlConnection conn = new SqlConnection("Data Source=WIN-A90OU0EVN8T;Initial Catalog=snsimpexpdb;User ID=sa;Password=sa");
           conn.Open();
           SqlCommand cmd = new SqlCommand(SqlQuery,conn);
           cmd.Connection = conn;
           cmd.CommandType = CommandType.Text;
           SqlDataReader dr = cmd.ExecuteReader();
           string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/pdfTechnician.pdf";
           Document DC = new Document(PageSize.A4, 25, 25, 30, 30);
           FileStream FS = File.Create(path);
           PdfWriter.GetInstance(DC, FS);
           DC.Open();
           
           while (dr.Read())
           {  
           SqlQuery = dr[0].ToString() + ' ' + dr[1].ToString() + ' ' + dr[2].ToString() + ' ' + dr[3].ToString() + ' ' + dr[4].ToString() + ' ' + dr[5].ToString() +' '+  dr[6].ToString()+' '+dr[7].ToString() +' '+  dr[8].ToString();
           DC.Add(new Paragraph(SqlQuery));
           }
           DC.Close();
        }



//R.RAJESH
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900