Extract data from database and print in the document ! [Resolved]

Posted by Akiii under C# on 6/8/2011 | Points: 10 | Views : 5651 | Status : [Member] | Replies : 14
Hi,

The scenario is this:-

(1) I have a database named EMPLOYEE
(2) In that database, there is one table named EMP
(3) There are 5 fields in that table which are id, name, age, salary and company
(4) Now, i want to extract data from the database depending on the selection of the id of the employee from the front-end
(5) There is a document in which i want to embed this value (that is), it maybe a pdf or a doc directly from the database
(6) How will i bind the data from the database to the document file ?

Please help in this. If you want further clarification, let me know !

Thanks and Regards
Akiii




Responses

Posted by: Ndebata on: 6/8/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Akiii,

Modified the code for you,
Try to run the application, after clicking the button go to the PDF_ITEXT folder to see whether pdf has been created or not with your data.

Else
try to put a breakpoint to see whether you are getting a proper datatable r not.

Thanks,
Debata
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
if you want print the details of employee why you need document or pdf .if you want to print only then you can use crystal reports using stored procedures.
then you can easily print that document directly.

Thanks&Regards
LakshmiNarayana Nalluri.

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
i have a template made in doc, i want want the data to be inserted in the doc and send it to client...

Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Akiii,

Take a look at the attached sample, in which i am creating a pdf document from datatable( loading from xml). In your case just load the datatable with data from db then use it.

Thanks,
Debata
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
http://koivi.com/fill-pdf-form-fields/tutorial.php

Nitha Deepak

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
@Ndebata....
Thanks for the reply
I am looking into your code, i will reply you as soon as i am done....!

@Nitha......
Thanks for the reply
The code you have given is in php........i want it in c#.....
Please refer me the link if there is any, meanwhile i am also searching...

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 6/8/2011 [Member] Bronze | Points: 25
Posted by: Susanthampy on: 6/8/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Try this,

http://csharpdotnetfreak.blogspot.com/2008/12/export-gridview-to-pdf-using-itextsharp.html

Regards,
Susan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
hi ndebata.....

I am working on your code.....but its not giving me results....
i am attaching a file please have a look and also pasting the whole code...
Please let me know if there is any wrong that i am doing!

protected void btnItext_Click(object sender, EventArgs e)

{
Document doc = new Document(PageSize.A4);
string path = Server.MapPath("PDF_ITEXT");
PdfWriter.GetInstance(doc, new FileStream(path + "/SampleText.pdf", FileMode.Create));
try
{
doc.Open();
doc.Add(new Paragraph("Hello Everybody!"));
doc.Close();

//Just for display purpose
Response.WriteFile(path + "/SampleText.pdf");
Response.End();
}
catch
{ }

}

protected void btnTable_Click(object sender, EventArgs e)
{
using (Document doc = new Document(PageSize.A4))
{
string path = Server.MapPath("PDF_ITEXT");
using (PdfWriter pwr = PdfWriter.GetInstance(doc, new FileStream(path + "/SamplePDF.pdf", FileMode.Create)))
{
try
{
doc.Open();
PdfPTable ptable = new PdfPTable(4);
PdfPCell pcell = new PdfPCell(new Paragraph("All My Employee Data"));
pcell.Colspan = 4;
pcell.BackgroundColor = BaseColor.GRAY;
pcell.HorizontalAlignment = 1;
ptable.AddCell(pcell);
DataTable dt = GetSomeData();
foreach (DataColumn item in dt.Columns)
{
pcell = new PdfPCell(new Phrase(item.ColumnName));
pcell.HorizontalAlignment = 1;
pcell.BackgroundColor = BaseColor.LIGHT_GRAY ;
ptable.AddCell(pcell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
foreach (DataColumn item in dt.Columns)
{
pcell = new PdfPCell(new Phrase(dt.Rows[i][item].ToString()));
pcell.HorizontalAlignment = 1;
pcell.BackgroundColor = i % 2 == 1 ? BaseColor.LIGHT_GRAY : BaseColor.WHITE;
ptable.AddCell(pcell);
}

}
doc.Add(ptable);
doc.Close();

Response.WriteFile(path + "/SamplePDF.pdf");
Response.End();
}
catch (Exception)
{

}
}
///Just for display purpose
}
}

private DataTable GetSomeData()
{
// in your case you may get your data from a different source in different format
string str = "Data Source=ARKA-PC;Initial Catalog=StepSample;User Id = sa;password = 12345";
SqlConnection conn = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("select * from PersonalDetail", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "PersonalDetail");
//ds.ReadXml(Server.MapPath(@"App_Data\MyXMLFile.xml"));
return ds.Tables[0];
}



Thanks for ur help..
Regards
Akiii
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/8/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Akiii,

The above code is meant for creating a table of 4 Columns,
Try replace that number with dt.Columns.Count ( The number of columns you want )

Remove the part, its not required.

Response.WriteFile(path + "/SamplePDF.pdf");
Response.End();

Thanks,
Debata

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
@ndebata...

it says "document has no pages"....IOException..!!

Akiii


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
hi ndebata....

i am attaching a image,please check it....

Thank you for ur constant support..

Regards
Akiii
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/8/2011 [Member] Bronze | Points: 25

Up
0
Down
@ndebata......the pdf created successfully......

it was my mistake..your code is absolutely fine......
thank you for support......you have been great!
I mark ur code as an answer.....!

@susan....
Thank you for the link......itextsharp dll is of great use....

@nitha...
the link that you provided contains a tool..and that tool is good.....thanks for recommending that!

thanks everybody for helping me out!
Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/8/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Welcome Akiii

Regards,
Susan

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response