0

I Need simple code on button click that will save my current rdlc report into pdf format and will ask user via savedialog to save the file in desired location. I have searched already everywhere and cant find any solution.

My ShowReport button code is here:

SqlConnection S_Conn = new SqlConnection(strConnString);
        S_Conn.Open();
        string query_1 = "";
        query_1 = "SELECT Record_Id, Select_Ward, Mr_No, Patient_Name, Date_Of_Admission, Date_Of_Dsch_Death, Disease from EO_System_RecordRoomData WHERE Date_Of_Admission = '" + txtbx_DateForReport.Text.Trim() + "'";
        SqlCommand Command_1 = new SqlCommand(query_1, S_Conn);
        SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command_1);
        DataSet1 Data_Set = new DataSet1();
        Data_Adapter.Fill(Data_Set);
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", Data_Set.Tables[1]));
        this.reportViewer1.RefreshReport();

Help will be highly appreciated.

adil sharif
  • 55
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [Creating a PDF from a RDLC Report in the Background](http://stackoverflow.com/questions/2684221/creating-a-pdf-from-a-rdlc-report-in-the-background) – Dr. Stitch Jun 10 '16 at 05:53
  • It doesn't show where the file is going to be saved. I want to show saveasdialog in order to save the file wherever user wants. – adil sharif Jun 10 '16 at 06:39

2 Answers2

0

You can use this library named : ItextSharp

You can create the pdf using the method given in below link. You can donwload the itextsharp through Nuget Package Manager.

http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables

rahlrokks
  • 451
  • 1
  • 4
  • 12
0

Try this:

SqlConnection S_Conn = new SqlConnection(strConnString);
S_Conn.Open();
string query_1 = "";
query_1 = "SELECT Record_Id, Select_Ward, Mr_No, Patient_Name, Date_Of_Admission, Date_Of_Dsch_Death, Disease "
        + "from EO_System_RecordRoomData WHERE Date_Of_Admission = '" + txtbx_DateForReport.Text.Trim() + "'";
SqlCommand Command_1 = new SqlCommand(query_1, S_Conn);
SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command_1);
DataSet1 Data_Set = new DataSet1();
Data_Adapter.Fill(Data_Set);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", Data_Set.Tables[1]));
reportViewer1.RefreshReport();

SaveFileDialog saveFileDialogPDF = new SaveFileDialog();
saveFileDialogPDF.Filter = "PDF|*.pdf";
saveFileDialogPDF.Title = "Save report to PDF";
saveFileDialogPDF.ShowDialog();

if (saveFileDialogPDF.FileName != "")
{
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string filenameExtension;
    byte[] bytes = reportViewer.LocalReport.Render(
               "PDF", null, out mimeType, out encoding, out filenameExtension,
               out streamids, out warnings);
    using (FileStream fs = new FileStream(saveFileDialogPDF.FileName, FileMode.Create))
    {
        fs.Write(bytes, 0, bytes.Length);
    }
}

Source:

https://msdn.microsoft.com/en-us/library/sfezx97z(v=vs.110).aspx

Creating a PDF from a RDLC Report in the Background

Community
  • 1
  • 1
Dr. Stitch
  • 908
  • 6
  • 15