hi i was wondering and doing code in c# with sql server 2008 using itextsharp tool or reference in creating pdf report from c# windows forms with sql server 2008(sql select query).
Given below is code on how i create pdf report in c# windows forms with sql server 2008 using itextsharp.
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 frmPDFTechnician()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
string SqlDataPull = ("Select p.technician_id as technician_id,n.Technician_first_name as technician_fname,n.Technician_last_name as technician_lname,n.Technician_middle_name as technician_mname from Techniciandetail n,Technician p where n.technician_id=p.technician_id and n.status=1");
SqlCommand cmd = new SqlCommand(SqlDataPull);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
SqlDataPull = dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString();
cboTechnician.Items.Add(SqlDataPull);
}
dr.Close();
}
private DataTable GetData(string query)
{
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
SqlCommand cmd = new SqlCommand(query);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
private void btnExportToPDF_Click(object sender, EventArgs e)
{
DataRow dr = GetData("Select t.technician_id, td.Technician_first_name+' '+td.Technician_middle_name+' '+td.Technician_last_name as Name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail ts,Technician t where t.technician_id="+cboTechnician.SelectedItem.ToString()).Rows[0];
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial",12,BaseColor.BLACK);
using (System.IO.MemoryStream memoryStream=new System.IO.MemoryStream())
{
PdfWriter writer=PdfWriter.GetInstance(document,memoryStream);
Phrase phase=null;
PdfPCell cell=null;
PdfPTable table=null;
document.Open();
table=new PdfPTable(2);
table.TotalWidth=500f;
table.LockedWidth=true;
table.SetWidths(new float[] { 0.3f, 0.7f });
cell = ImageCell("D:\\Workarea\vishal\\DRRS in CSharp\\DRRS CSharp\\DRRS CSharp\\Images\\logo4.png", 30f, PdfPCell.ALIGN_LEFT);
table.AddCell(cell);
phase=new Phrase();
phase.Add(new Chunk("Tuscano Equipments Private Limited",FontFactory.GetFont("Arial",16,BaseColor.RED)));
phase.Add(new Chunk("Royapettah High Road",FontFactory.GetFont("Arial",10,BaseColor.BLACK)));
phase.Add(new Chunk("Chennai, Tamilnadu",FontFactory.GetFont("Arial",10,BaseColor.BLACK)));
cell = PhraseCell(phase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment =PdfPCell.ALIGN_TOP;
table.AddCell(cell);
BaseColor color= new BaseColor(System.Drawing.ColorTranslator.FromHtml("#A9A9A9"));
DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color);
DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color);
document.Add(table);
table=new PdfPTable(2);
table.HorizontalAlignment=Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.3f, 1f });
table.SpacingBefore = 20f;
cell=PhraseCell(new Phrase("Technician Record",FontFactory.GetFont("Arial",12,BaseColor.BLACK)),PdfPCell.ALIGN_CENTER);
cell.Colspan=2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
document.Add(table);
DrawLine(writer,160f,80f,160f,690f,BaseColor.BLACK);
DrawLine(writer,115f,document.Top-200f,document.PageSize.Width-100f,document.Top-200f,BaseColor.BLACK);
table = new PdfPTable(2);
table.SetWidths(new float[] { 0.5f, 2f });
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(PhraseCell(new Phrase("Technician Id:",FontFactory.GetFont("Arial",10,BaseColor.BLACK)),PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell((new Phrase("000"+dr["technician_id"],FontFactory.GetFont("Arial",10,BaseColor.BLACK))),PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Name:",FontFactory.GetFont("Arial",10,BaseColor.BLACK)),PdfPCell.ALIGN_LEFT));
phase.Add(new Chunk(dr["Technician_first_name"] + "\n", FontFactory.GetFont("Arial",10,BaseColor.BLACK)));
phase.Add(new Chunk(dr["Technician_last_name"] + " " + dr["Technician_middle_name"], FontFactory.GetFont("Arial",10,BaseColor.BLACK)));
table.AddCell(PhraseCell(phase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial",10,BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(Convert.ToDateTime(dr["technician_dob"]).ToString("dd MMMM, yyyy"), FontFactory.GetFont("Arial",10,BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Gender:",FontFactory.GetFont("Arial",10,BaseColor.BLACK)),PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["technician_sex"].ToString(), FontFactory.GetFont("Arial",10,BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("Designation:", FontFactory.GetFont("Arial",10,BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["technician_type"].ToString(), FontFactory.GetFont("Arial",10,BaseColor.BLACK)), PdfPCell.ALIGN_JUSTIFIED));
document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
}
}
}
private PdfPCell PhraseCell(Phrase phase, int p)
{
PdfPCell cell = new PdfPCell(phase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
private void DrawLine(PdfWriter writer, float p1, float p2, float p3, float p4, BaseColor color)
{
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetColorStroke(BaseColor.BLACK);
contentByte.MoveTo(p1, p2);
contentByte.LineTo(p3, p4);
contentByte.Stroke();
}
private PdfPCell ImageCell(string p1, float p2, int p3)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("D:\\Workarea\vishal\\DRRS in CSharp\\DRRS CSharp\\DRRS CSharp\\Images\\logo4.png");
image.ScalePercent(40);
PdfPCell cell = new PdfPCell(image);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.PaddingBottom = 0f;
cell.PaddingTop = 0f;
return cell;
}When i execute the above code i get error message telling "Incorrect syntax near (Technician First Name and Technician last name);
In above code i populate my combobox named: cboTechnician through following code:
public partial class frmPDFTechnician : Form
{
public frmExcelTechnician()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
string SqlDataPull = ("Select p.technician_id as technician_id,n.Technician_first_name as technician_fname,n.Technician_last_name as technician_lname,n.Technician_middle_name as technician_mname from Techniciandetail n,Technician p where n.technician_id=p.technician_id and n.status=1");
SqlCommand cmd = new SqlCommand(SqlDataPull);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
SqlDataPull = dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString();
cboTechnician.Items.Add(SqlDataPull);
}
dr.Close();
}what i want is when user selects a technician(Id,technician_first_name,technician_last_name) from a combobox named: cboTechnician and when i click btnExportToPDF button i want that information of Technician(Technician Id,Name(Technician_first_name,Technician_last_name and Technician middle name),Date of Birth,Gender and Designation) to be displayed in PDF report from c# windows forms.
Given below is structure of my tables:
table name: Technician
Column Name DataType AllowNulls
technician_id(primary key) Int(auto-increment primary key) 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(auto-increment) No
technician_id Int Yes
user_id Int Yes
technician_type Int Yes
Can anyone help me please. Any help or guidance in solving this problem would be greatly appreciated.
vishal