Thursday, October 15, 2009

hi kali, this is the code to load report at runtime da


//code to load report with runtime data source

//dsReportSource -- we need to fill this dataset with our filtered data

CrystalDecisions.CrystalReports.Engine.ReportDocument rptdoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
rptdoc.Load(Application.StartupPath + "\\Reports\\ReportFile.rpt");
rptdoc.SetDataSource(dsReportSource);
rptdoc.PrintOptions.PrinterName = PrinterName;
rptdoc.PrintToPrinter(1, false, 0, 0);
rptdoc.Close();
rptdoc.Dispose();
rptdoc = null;
GC.Collect();
GC.WaitForPendingFinalizers();








//strImage0 -- field name having file path
//Image0 -- field name which save image to datatable

//function to load image file in to data table
private void LoadImage(DataTable dtImage)
{
int i = 0;
try
{
for (int nRec = 0; nRec < dtImage.Rows.Count; nRec++)
{

try
{
String strFile = dtImage.Rows[nRec]["strImage" + i.ToString()].ToString();

dtImage.Rows[nRec]["strImage" + i.ToString()] = strFile ;
if (strFilePath != "")
{
dtImage.Rows[nRec]["Image" + i.ToString()] = GetByteArray(strFilePath);
}
}
catch (Exception ex)
{

}

}
}
catch (Exception exception)
{

}

}
public static byte[] GetByteArray(String strFileName)
{
System.IO.FileStream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Open);
// initialise the binary reader from file streamobject
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
// define the byte array of filelength

byte[] imgbyte = new byte[fs.Length + 1];
// read the bytes from the binary reader

imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
// add the image in bytearray

br.Close();
// close the binary reader

fs.Close();
// close the file stream
return imgbyte;
}