2 weeks ago
i have the following error when i try export to stream a report in crystalreport of visual studio
Error getting value from 'ReadTimeout' on 'CrystalDecisions.ReportSource.EromReportSourceBase+FileStreamDeleteOnClose'.
this is my code:
public FileStreamResult CrearReporte()
{
try
{
var Reporte = new ReportClass();
Reporte.FileName = Nombre_reporte;
//agrega los parametros usados en el reporte
if (ListaParametros.Count > 0)
{
for (int x = 0; x <= ListaParametros.Count - 1; x++)
{
Reporte.SetParameterValue(ListaParametros[x].NombreParametro,ListaParametros[x].ValorParamentro);
}
}
// Configura las credenciales de la base de datos
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = this.Nombre_servidor;
connectionInfo.DatabaseName = this.Nombre_db;
connectionInfo.UserID = this.Nombre_usuario;
connectionInfo.Password = this.Password_servidor;
// Obtén la colección de tablas del informe y actualiza las credenciales para cada tabla
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
Tables tables = Reporte.Database.Tables;
foreach (Table table in tables)
{
tableLogOnInfo = table.LogOnInfo;
tableLogOnInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogOnInfo);
}
Stream rptdoc = Reporte.ExportToStream(ExportFormatType.PortableDocFormat);
Reporte.Close();
Reporte.Dispose();
return new FileStreamResult(rptdoc, "application/pdf") ;
}
catch (Exception ex)
{
throw (ex);
}
}
Usually when exports fail it's likely due to database connection or parameter issue or your app doesn't have permission to the destination.
Note there is a lot of commented code but read it over, also note I used the ReportClientDocument (RAS) object as well
private void ExportToStream_Click(object sender, EventArgs e)
{
rptClientDoc = rpt.ReportClientDocument;
CrystalDecisions.Shared.MicrosoftMailDestinationOptions mailOpts = CrystalDecisions.Shared.ExportOptions.CreateMicrosoftMailDestinationOptions();
CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = CrystalDecisions.Shared.ExportOptions.CreateDiskFileDestinationOptions();
CrystalDecisions.Shared.ExportOptions exportOpts = new CrystalDecisions.Shared.ExportOptions();
CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions csvExpOpts = new CrystalDecisions.Shared.CharacterSeparatedValuesFormatOptions();
CrystalDecisions.Shared.HTMLFormatOptions HTMLExpOpts = new CrystalDecisions.Shared.HTMLFormatOptions();
CrystalDecisions.Shared.PdfFormatOptions PDFExpOpts = new CrystalDecisions.Shared.PdfFormatOptions();
CrystalDecisions.Shared.EditableRTFExportFormatOptions RTFExpOpts = new CrystalDecisions.Shared.EditableRTFExportFormatOptions();
CrystalDecisions.Shared.PdfRtfWordFormatOptions PDFRTFExpOpts = new CrystalDecisions.Shared.PdfRtfWordFormatOptions();
CrystalDecisions.Shared.ExcelDataOnlyFormatOptions XLSXExpOpts = new CrystalDecisions.Shared.ExcelDataOnlyFormatOptions();
CrystalDecisions.Shared.TextFormatOptions txtFmtOpts = new CrystalDecisions.Shared.TextFormatOptions();
CrystalDecisions.Shared.ReportFileFormat crExpFormat = new CrystalDecisions.Shared.ReportFileFormat();
CrystalDecisions.ReportAppServer.ReportDefModel.HTMLExportFormatOptions myHTML = new HTMLExportFormatOptions();
CrystalDecisions.ReportAppServer.ReportDefModel.RPTExportFormatOptions myRPTExp = new RPTExportFormatOptions();
CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions RASEptOpts = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();
//rpt.Load(@"c:\reports\b.rpt");
//rptClientDoc = new ReportClientDocument();
//ExportOptions exportOptions = new ExportOptions();
//exportOptions = exportOptions.createExportOptionsByFormat(CrReportExportFormatEnum.crReportExportFormatCharacterSeparatedValues);
string MyRptName = rpt.FileName.ToString();
MyRptName = MyRptName.Substring(MyRptName.LastIndexOf(@"\") + 1, (rpt.FileName.Length - 3) - (MyRptName.LastIndexOf(@"\") + 1)) + "pdf";
//MyRptName = MyRptName.Substring(MyRptName.LastIndexOf(@"\") + 1, (rpt.FileName.Length - 3) - (MyRptName.LastIndexOf(@"\") + 1)) + "xls";
//MyRptName = MyRptName.Substring(MyRptName.LastIndexOf(@"\") + 1, (rpt.FileName.Length - 3) - (MyRptName.LastIndexOf(@"\") + 1)) + "txt";
//MyRptName = MyRptName.Substring(MyRptName.LastIndexOf(@"\") + 1, (rpt.FileName.Length - 3) - (MyRptName.LastIndexOf(@"\") + 1)) + "html";
//RASEptOpts.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatMHTML;
//diskOpts.DiskFileName = @"c:\temp\html\" + MyRptName;
//diskOpts.DiskFileName = "c:\\temp\\ReportName.rpt";
//diskOpts.DiskFileName = "c:\\temp\\ReportName.csv";
//diskOpts.DiskFileName = @"c:\temp\html\b.html";
//diskOpts.DiskFileName = @"c:\reports\worldsales.pdf";
//diskOpts.DiskFileName = @"c:\reports\worldsales.rtf";
//diskOpts.DiskFileName = @"c:\reports\inventory.xlsx";
//diskOpts.DiskFileName = @"D:\Atest\54902\ExportReportDonNetStrem.txt";
//exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
////exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues;
////exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues;
////exportOpts.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.HTML40;
//exportOpts.ExportDestinationOptions = diskOpts;
//CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions exportOptions = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();
//This sets which format we will export to.
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;
//Create the FormatOptions class for PDF.
//PDFExportFormatOptions PDFexportOptions = new PDFExportFormatOptions();
//PDFexportOptions.StartPageNumber = 1;
//PDFexportOptions.EndPageNumber = 1;
//PDFexportOptions.CreateBookmarksFromGroupTree = true;
////This sets which format we will export to.
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatText;
////Create the FormatOptions class for TXT.
//TextExportFormatOptions TXTexportOptions = new TextExportFormatOptions();
//TXTexportOptions.CharactersPerInch = 16;
//TXTexportOptions.LinesPerPage = 0;
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;
////Create the FormatOptions class for Editable RTF.
//RPTExportFormatOptions RTFExpOptions = new RPTExportFormatOptions();
////Set the export format options with the page range we just set.
//exportOptions.FormatOptions = RTFExpOptions;
////This sets which format we will export to.
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatEditableRTF;
////RTFExpOptions.StartPageNumber = 1;
////RTFExpOptions.EndPageNumber = 2;
////RTFExpOptions.DiscardUnselectedRecords = false;
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatMHTML;
//HTMLExpOpts = new HTMLFormatOptions();
////HTMLExpOpts.HTMLFileName = @"c:\tmp\html\phonebook report that looks real purdy.html";
//HTMLExpOpts.HTMLFileName = @"C:\inetpub\wwwroot\foldername\" + MyRptName;
//HTMLExpOpts.HTMLBaseFolderName = @"C:\inetpub\wwwroot\foldername\" + MyRptName;
//HTMLExpOpts.HTMLEnableSeparatedPages = false;
//HTMLExpOpts.UsePageRange = false;
//HTMLExpOpts.HTMLHasPageNavigator = false;
//exportOpts.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
//exportOpts.ExportDestinationOptions = diskOpts;
//exportOpts.ExportFormatOptions = HTMLExpOpts;
//exportOpts.ExportFormatType = ExportFormatType.HTML40;
//rpt.Export(exportOpts);
diskOpts.DiskFileName = @"c:\reports\formulas.pdf";
try
{
if (File.Exists(diskOpts.DiskFileName))
{
File.Delete(diskOpts.DiskFileName);
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions exportOptions = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();
exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;
//exportOptions.FormatOptions(diskOpts);
//exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatMHTML;
//exportOptions.FormatOptions = myHTML;
//myHTML.PageNavigator = true;
//myHTML.SeparatePages = false;
//rptClientDoc.VerifyDatabase();
//This exports the report to a byte() that we will stream out.
Byte[] oByte = (Byte[])rptClientDoc.PrintOutputController.ExportEx(exportOptions).ByteArray;
//////csvExpOpts = new CharacterSeparatedValuesFormatOptions();
//////csvExpOpts.GroupSectionsOption = CsvExportSectionsOption.Export;
//////csvExpOpts.ReportSectionsOption = CsvExportSectionsOption.Export;
//////csvExpOpts.SeparatorText = ",";
//////csvExpOpts.Delimiter = "|";
System.IO.Stream oStream;
byte[] byteArray = null;
//oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues);
//oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Text);
MemoryStream ms = new MemoryStream(); // Soday says it's not supported. e-mail subject - RE: Export to Stream....
FileStream file = new FileStream(@"c:\reports\formulas.pdf", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
// this is used to verify the file so I saved it to disk
System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oByte.Length)).Close();
System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length));
System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);
oStream.Close();
GC.Collect();
MessageBox.Show("Export to Stream complete", "RAS", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
9 | |
9 | |
7 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.