I need help to figure out what might be wrong in my code that I was trying to pupolate a result from SQL table to crystal report. My web form has Date range (Start Date and End Date) for the search data but the crystal return everything that I designed it through the crystal report with the defual value. The paramaters were not passing in or filtering for some reason.
Here are some of my source code below:
----------------------------------------------------------------
Utility.ReportTool myReport = new Utility.ReportTool( strAccessFolder );
switch( strReport )
{
case "1":
strReportName = "IndDetailReport.pdf";
strCrystalReportFolder = "Reports\\IndDetailReport.rpt";
strQuery = "dbo.rp_IndDetailReport '" + strBeginDate + "','" + strEndDate + "','" + strID + "','" + AccessCodes + "'";
myReport.SendToCrystalSql(strCrystalReportFolder, strReportName, strQuery);
break;
default:
break;
}
Response.Clear();
Response.Redirect( strAccessFolder + strReportName, true);
------------------------------------------------------------
public class ReportTool
{
//public string SenderName = "";
//public string SenderEmail = "";
private string FileName = "";
private string Folder = "";
private ReportDocument Report = new ReportDocument(); //create the report document
private ExportOptions myOptions = new ExportOptions();
private HttpContext Context = HttpContext.Current;
public void ToFile( string fileName ){ FileName = fileName; }
public void ToAcrobat(){ myOptions.ExportFormatType = ExportFormatType.PortableDocFormat; }
public void ToExcel(){ myOptions.ExportFormatType = ExportFormatType.Excel; }
public void ToWord(){ myOptions.ExportFormatType = ExportFormatType.WordForWindows; }
public void ToRichText(){ myOptions.ExportFormatType = ExportFormatType.RichText; }
public void ToHTML(){ myOptions.ExportFormatType = ExportFormatType.HTML40; }
public ReportTool( ) : this(".") { }
public ReportTool( string name )
{
Folder = Context.Server.MapPath( name );
// Ensure the folder Exists. Parent Folder MUST Exist.
if ( !Directory.Exists( Folder ) )
Directory.CreateDirectory( Folder );
ClearFolder();
}
public void SendToCrystalSql(string reportName, string destination, string sql )
{
Report.Load( Context.Server.MapPath( reportName ));
if (sql != "")
{
SqlConnection Connection = null;
string strConn = Context.Application.Get("PrimaryConnection").ToString();
Connection = new SqlConnection( strConn );
Connection.Open();
DataSet dataSet = new DataSet();
SqlDataAdapter myCmd = new SqlDataAdapter(sql, Connection);
myCmd.Fill (dataSet,"Table");
try
{
Report.SetDataSource (dataSet.Tables["Table"]);
}
catch (Exception xxxx)
{
string a = xxxx.Message;
}
Connection.Close();
}
//SetParameters();
ToFile( Folder + destination );
ToAcrobat();
SetExportOptions();
try
{
Report.Export();
}
catch (Exception E) {string t = E.Message;}
Report.Close();
}
private void SetExportOptions( )
{
ExcelFormatOptions ExcelOptions = new ExcelFormatOptions();
HTMLFormatOptions HTMLOptions = new HTMLFormatOptions();
PdfRtfWordFormatOptions DocOptions = new PdfRtfWordFormatOptions();
DiskFileDestinationOptions DestinationOptions = new DiskFileDestinationOptions();
DestinationOptions.DiskFileName = FileName;
Report.ExportOptions.DestinationOptions = DestinationOptions;
Report.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
Report.ExportOptions.ExportFormatType = myOptions.ExportFormatType;
switch(myOptions.ExportFormatType)
{
case ExportFormatType.HTML32:
case ExportFormatType.HTML40:
HTMLOptions.HTMLFileName = FileName;
Report.ExportOptions.FormatOptions = HTMLOptions;
break;
case ExportFormatType.PortableDocFormat:
case ExportFormatType.RichText:
case ExportFormatType.WordForWindows:
Report.ExportOptions.FormatOptions = DocOptions;
break;
case ExportFormatType.Excel:
// Set the excel format options.
//ExcelOptions.ExcelUseConstantColumnWidth = true;
ExcelOptions.ExcelTabHasColumnHeadings = true;
ExcelOptions.ExcelUseConstantColumnWidth = false;
Report.ExportOptions.FormatOptions = ExcelOptions;
break;
}
}
}
--------------------------------------------------------------
I am using C# with SQL 2000 server.
Thank you!
Su
Here are some of my source code below:
----------------------------------------------------------------
Utility.ReportTool myReport = new Utility.ReportTool( strAccessFolder );
switch( strReport )
{
case "1":
strReportName = "IndDetailReport.pdf";
strCrystalReportFolder = "Reports\\IndDetailReport.rpt";
strQuery = "dbo.rp_IndDetailReport '" + strBeginDate + "','" + strEndDate + "','" + strID + "','" + AccessCodes + "'";
myReport.SendToCrystalSql(strCrystalReportFolder, strReportName, strQuery);
break;
default:
break;
}
Response.Clear();
Response.Redirect( strAccessFolder + strReportName, true);
------------------------------------------------------------
public class ReportTool
{
//public string SenderName = "";
//public string SenderEmail = "";
private string FileName = "";
private string Folder = "";
private ReportDocument Report = new ReportDocument(); //create the report document
private ExportOptions myOptions = new ExportOptions();
private HttpContext Context = HttpContext.Current;
public void ToFile( string fileName ){ FileName = fileName; }
public void ToAcrobat(){ myOptions.ExportFormatType = ExportFormatType.PortableDocFormat; }
public void ToExcel(){ myOptions.ExportFormatType = ExportFormatType.Excel; }
public void ToWord(){ myOptions.ExportFormatType = ExportFormatType.WordForWindows; }
public void ToRichText(){ myOptions.ExportFormatType = ExportFormatType.RichText; }
public void ToHTML(){ myOptions.ExportFormatType = ExportFormatType.HTML40; }
public ReportTool( ) : this(".") { }
public ReportTool( string name )
{
Folder = Context.Server.MapPath( name );
// Ensure the folder Exists. Parent Folder MUST Exist.
if ( !Directory.Exists( Folder ) )
Directory.CreateDirectory( Folder );
ClearFolder();
}
public void SendToCrystalSql(string reportName, string destination, string sql )
{
Report.Load( Context.Server.MapPath( reportName ));
if (sql != "")
{
SqlConnection Connection = null;
string strConn = Context.Application.Get("PrimaryConnection").ToString();
Connection = new SqlConnection( strConn );
Connection.Open();
DataSet dataSet = new DataSet();
SqlDataAdapter myCmd = new SqlDataAdapter(sql, Connection);
myCmd.Fill (dataSet,"Table");
try
{
Report.SetDataSource (dataSet.Tables["Table"]);
}
catch (Exception xxxx)
{
string a = xxxx.Message;
}
Connection.Close();
}
//SetParameters();
ToFile( Folder + destination );
ToAcrobat();
SetExportOptions();
try
{
Report.Export();
}
catch (Exception E) {string t = E.Message;}
Report.Close();
}
private void SetExportOptions( )
{
ExcelFormatOptions ExcelOptions = new ExcelFormatOptions();
HTMLFormatOptions HTMLOptions = new HTMLFormatOptions();
PdfRtfWordFormatOptions DocOptions = new PdfRtfWordFormatOptions();
DiskFileDestinationOptions DestinationOptions = new DiskFileDestinationOptions();
DestinationOptions.DiskFileName = FileName;
Report.ExportOptions.DestinationOptions = DestinationOptions;
Report.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
Report.ExportOptions.ExportFormatType = myOptions.ExportFormatType;
switch(myOptions.ExportFormatType)
{
case ExportFormatType.HTML32:
case ExportFormatType.HTML40:
HTMLOptions.HTMLFileName = FileName;
Report.ExportOptions.FormatOptions = HTMLOptions;
break;
case ExportFormatType.PortableDocFormat:
case ExportFormatType.RichText:
case ExportFormatType.WordForWindows:
Report.ExportOptions.FormatOptions = DocOptions;
break;
case ExportFormatType.Excel:
// Set the excel format options.
//ExcelOptions.ExcelUseConstantColumnWidth = true;
ExcelOptions.ExcelTabHasColumnHeadings = true;
ExcelOptions.ExcelUseConstantColumnWidth = false;
Report.ExportOptions.FormatOptions = ExcelOptions;
break;
}
}
}
--------------------------------------------------------------
I am using C# with SQL 2000 server.
Thank you!
Su