C# Programming
12-14-2009, 11:42 AM
I am using thecode below to generate the reports but I want to know how to modify it to export to PDF file directly without a need of the report viewer control?
private void generate_invoices()
{
this.Cursor = Cursors.WaitCursor;
// sql_connection = new SqlConnection("Data Source=.\\JASSIMSQL;initial catalog=takhlees;integrated security=true");
sql_connection = new SqlConnection(public_var.sql_server_connection);
try
{
sql_connection.Open();
sql_command = new SqlCommand("sp_get_account_book_by_book_guid", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
// sql_command.Parameters.Add("@accounts_date", SqlDbType.DateTime).Value = current_date.ToShortDateString();
// sql_command.Parameters.Add("@account_number", SqlDbType.Int).Value = dateCurrentDate.SelectionRange.Start;
sql_command.Parameters.Add("@book_guid", SqlDbType.UniqueIdentifier).Value = book_guid;
DataSet data_set = new dsReports();
reportTICS.Reset();
sql_reader = sql_command.ExecuteReader();
data_set.Tables[0].Load(sql_reader);
// reportViewer1.LocalReport.ReportEmbeddedResource = "The_Internet_Cafe_System.ReportDaily.rdlc";
reportTICS.LocalReport.ReportEmbeddedResource = "dsReports_sp_get_account_book_by_book_guid";
// reportTICS.LocalReport.ReportPath = TICS_registry_current_user_reports.GetValue("ReportsFolder").ToString() + "\\" + TICS_registry_current_user_reports.GetValue("DailyReportFile").ToString();
reportTICS.LocalReport.ReportPath = @"C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Takhlees\Takhlees\envoice.rdlc";
ReportDataSource rds = new ReportDataSource();
rds.Name = "dsReports_sp_get_account_book_by_book_guid";
rds.Value = data_set.Tables[0];
reportTICS.LocalReport.DataSources.Add(rds);
ReportDataSource rds2 = new ReportDataSource();
rds2.Name = "dsReports_sp_get_account_book_by_account_number";
rds2.Value = data_set.Tables[0];
reportTICS.LocalReport.DataSources.Add(rds2);
reportTICS.LocalReport.*******();
reportTICS.*******Report();
if (chkSaveInvoice.Checked == true)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = reportTICS.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
string file_path = "c:\\temp\\envoice\\" + DateTime.Now.ToString("dd MMM yyyy");
System.IO.Directory.CreateDirectory(file_path);
// string file_namr = "c:\\temp\\envoice\\" + data_set.Tables[0].Rows.Count.ToString() + ".pdf";
string file_namr = file_path + "\\" + System.Guid.NewGuid() + ".pdf";
using (System.IO.FileStream fs = new System.IO.FileStream(file_namr, System.IO.FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
Process.Start(file_path);
}
// this.Text = TICS_registry_current_user_reports.GetValue("DailyReportTitle").ToString();
// event_log.create_event("Daily Report was generated for " + dateCurrentDate.SelectionRange.Start.ToString("dd MMM yyyy"), EventLogEntryType.Information, 1000);
// sql_reader.Close();
// sql_connection.Close();
progressInvoice.Minimum = 0;
progressInvoice.Maximum = data_set.Tables[0].Rows.Count;
}
catch (Exception exp)
{
event_log.create_event("Problem preparing or viewing daily report." + Environment.NewLine + Environment.NewLine + exp.Message + Environment.NewLine + Environment.NewLine + exp.Data + Environment.NewLine + Environment.NewLine + exp.StackTrace + Environment.NewLine + Environment.NewLine + exp.InnerException, EventLogEntryType.Error, 9999);
MessageBox.Show("Problem preparing or viewing daily report!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
finally
{
if (sql_reader != null) sql_reader.Close();
if (sql_connection != null)
{
if (sql_connection.State == ConnectionState.Open)
sql_connection.Close();
}
}
this.Cursor = Cursors.Default;
}
private void generate_invoices()
{
this.Cursor = Cursors.WaitCursor;
// sql_connection = new SqlConnection("Data Source=.\\JASSIMSQL;initial catalog=takhlees;integrated security=true");
sql_connection = new SqlConnection(public_var.sql_server_connection);
try
{
sql_connection.Open();
sql_command = new SqlCommand("sp_get_account_book_by_book_guid", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
// sql_command.Parameters.Add("@accounts_date", SqlDbType.DateTime).Value = current_date.ToShortDateString();
// sql_command.Parameters.Add("@account_number", SqlDbType.Int).Value = dateCurrentDate.SelectionRange.Start;
sql_command.Parameters.Add("@book_guid", SqlDbType.UniqueIdentifier).Value = book_guid;
DataSet data_set = new dsReports();
reportTICS.Reset();
sql_reader = sql_command.ExecuteReader();
data_set.Tables[0].Load(sql_reader);
// reportViewer1.LocalReport.ReportEmbeddedResource = "The_Internet_Cafe_System.ReportDaily.rdlc";
reportTICS.LocalReport.ReportEmbeddedResource = "dsReports_sp_get_account_book_by_book_guid";
// reportTICS.LocalReport.ReportPath = TICS_registry_current_user_reports.GetValue("ReportsFolder").ToString() + "\\" + TICS_registry_current_user_reports.GetValue("DailyReportFile").ToString();
reportTICS.LocalReport.ReportPath = @"C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Takhlees\Takhlees\envoice.rdlc";
ReportDataSource rds = new ReportDataSource();
rds.Name = "dsReports_sp_get_account_book_by_book_guid";
rds.Value = data_set.Tables[0];
reportTICS.LocalReport.DataSources.Add(rds);
ReportDataSource rds2 = new ReportDataSource();
rds2.Name = "dsReports_sp_get_account_book_by_account_number";
rds2.Value = data_set.Tables[0];
reportTICS.LocalReport.DataSources.Add(rds2);
reportTICS.LocalReport.*******();
reportTICS.*******Report();
if (chkSaveInvoice.Checked == true)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = reportTICS.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
string file_path = "c:\\temp\\envoice\\" + DateTime.Now.ToString("dd MMM yyyy");
System.IO.Directory.CreateDirectory(file_path);
// string file_namr = "c:\\temp\\envoice\\" + data_set.Tables[0].Rows.Count.ToString() + ".pdf";
string file_namr = file_path + "\\" + System.Guid.NewGuid() + ".pdf";
using (System.IO.FileStream fs = new System.IO.FileStream(file_namr, System.IO.FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
Process.Start(file_path);
}
// this.Text = TICS_registry_current_user_reports.GetValue("DailyReportTitle").ToString();
// event_log.create_event("Daily Report was generated for " + dateCurrentDate.SelectionRange.Start.ToString("dd MMM yyyy"), EventLogEntryType.Information, 1000);
// sql_reader.Close();
// sql_connection.Close();
progressInvoice.Minimum = 0;
progressInvoice.Maximum = data_set.Tables[0].Rows.Count;
}
catch (Exception exp)
{
event_log.create_event("Problem preparing or viewing daily report." + Environment.NewLine + Environment.NewLine + exp.Message + Environment.NewLine + Environment.NewLine + exp.Data + Environment.NewLine + Environment.NewLine + exp.StackTrace + Environment.NewLine + Environment.NewLine + exp.InnerException, EventLogEntryType.Error, 9999);
MessageBox.Show("Problem preparing or viewing daily report!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
finally
{
if (sql_reader != null) sql_reader.Close();
if (sql_connection != null)
{
if (sql_connection.State == ConnectionState.Open)
sql_connection.Close();
}
}
this.Cursor = Cursors.Default;
}