Reporting class
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
namespace prjstart.Components
{
/// <summary>
/// Summary description for
ReportingClass.
/// </summary>
public class
ReportingClass
{
public string
CreateReport(string SqlString, string FileName)
{
try
{
fileName
= FileName.Trim();
ds
= getData(SqlString); // This function gets the Data
string path
=@"C:\inetpub\wwwroot\prjstart\reports\" + FileName +
".rpt";// This is used to store the path
where the report is being built
// Load the report
crReportDocument.Load(path); // Loads the
report from the path given above.
//declare the tables
crTables
= crReportDocument.Database.Tables;
//assign tables
crReportDocument.Database.Tables[0].SetDataSource(ds.Tables[0]); //Asigns the
DataSet to the object
//assign report source
CrvMain.ReportSource
= crReportDocument; //crReportdocument is the DataSource.
//call the export options - can be modified to export to
other formats in this case PDF was used
exportOptions();
// return s;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
return "Report successfully stored in
C:\finalreports folder";
}
private void
exportOptions()
{
_saveTo=
@"c:\finalreport\"; // this path saves the report to the intended destination.
//provide a basic naming convention
_date
= System.DateTime.Now.Date.ToString("MM") +
System.DateTime.Now.Date.ToString("dd") +
System.DateTime.Now.Date.ToString("yyyy");
//where do u want the report to be saved?
_exportPath
= _saveTo ;
//check if the directory exists if not make one
if (Directory.Exists(_exportPath) == false) Directory.CreateDirectory(_exportPath);
//prepare for export
crDiskFileDestinationOptions
= new DiskFileDestinationOptions();
crExportOptions
= crReportDocument.ExportOptions;
fileName
= fileName + _date +
".pdf"; // Assign the extension you want to convert.
crDiskFileDestinationOptions.DiskFileName
= _exportPath + fileName;
crExportOptions.DestinationOptions
= crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType
= ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType
= ExportFormatType.PortableDocFormat;
try
{
crReportDocument.Export();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private DataSet getData(string
SqlString)
{
conn
= new SqlConnection(connString);
try
{
conn.Open();
da
= new SqlDataAdapter(SqlString, conn);
ds
= new DataSet();
da.Fill(ds);
return ds;
}
catch(Exception e)
{
throw new
Exception("Error getting data: " + e.Message);
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
private void
getDataset()
{
try
{
ds
= new DataSet();
ds
= getData(sqlString);
}
catch(Exception ex)
{
string msg = ex.Message;
}
}
// Below are the main classes under Crystal Decisions
namespace.
ReportDocument
crReportDocument = new ReportDocument();
CrystalDecisions.Web.CrystalReportViewer
CrvMain = new CrystalReportViewer();
ExportOptions
crExportOptions; // ExportOptions present under CrystalDecisions.Shared
DiskFileDestinationOptions
crDiskFileDestinationOptions; // This too present under CrystalDecisions.Shared.
Tables
crTables;
ParameterFields
ParamFields = new ParameterFields (); // Ignore
this
ParameterField
ParamField = new ParameterField (); // meant for further testing and development.
ParameterDiscreteValue
DValue = new ParameterDiscreteValue ();
//**//
string sqlString = "";
string fileName = ""; //**//
string _saveTo = "";
string _date = "";
string _exportPath = "";
private DataSet ds;
private SqlConnection conn;
private SqlDataAdapter da;
string connString =
"server=localhost;uid=sa;pwd=;database=prjstart";
}
}