code_junkie

How to use Crystal Reports with ASP.NET?

Nov 13th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. 'Generate the Report
  2. Dim oRpt As New ReportDocument
  3. Dim reportPath As String = Server.MapPath("crtTAL.rpt")
  4. oRpt.Load(reportPath)
  5.  
  6. oRpt.SetDataSource(dsTAL)
  7.  
  8. If Not IO.Directory.Exists(tempLocation) Then
  9. IO.Directory.CreateDirectory(tempLocation)
  10. End If
  11.  
  12. If IO.File.Exists(tempLocation & filename) Then
  13. IO.File.Delete(tempLocation & filename)
  14. End If
  15.  
  16. ' ********************************
  17.  
  18. ' First we must create a new instance of the diskfiledestinationoptions class and
  19. ' set variable called crExportOptions to the exportoptions class of the reportdocument.
  20. Dim crDiskFileDestinationOptions As New DiskFileDestinationOptions
  21. Dim crExportOptions As ExportOptions = oRpt.ExportOptions
  22.  
  23. 'Export to Word
  24.  
  25. 'append a filename to the export path and set this file as the filename property for
  26. 'the DestinationOptions class
  27. crDiskFileDestinationOptions.DiskFileName = tempLocation + filename
  28.  
  29. 'set the required report ExportOptions properties
  30. With crExportOptions
  31. .DestinationOptions = crDiskFileDestinationOptions
  32. .ExportDestinationType = ExportDestinationType.DiskFile
  33. .ExportFormatType = ExportFormatType.WordForWindows
  34. End With
  35.  
  36. 'Once the export options have been set for the report, the report can be exported. The Export command
  37. 'does not take any arguments
  38. Try
  39. ' Export the report
  40. oRpt.Export()
  41. oRpt.Close()
  42. oRpt.Dispose()
  43. projectCount = projectCount + 1
  44. Catch err As Exception
  45. Response.Write("<BR>")
  46. Response.Write(err.Message.ToString)
  47. errorList = errorList & dtrProjects.Item("Title") & "; "
  48. End Try
  49.  
  50. public partial class _Default : System.Web.UI.Page
  51. {
  52. protected void Page_Load(object sender, EventArgs e)
  53. {
  54.  
  55. ///create instance of class first
  56. ReportDocument rpDoc = new ReportDocument();
  57. ///load the report
  58. rpDoc.Load(@"TicketingBasic.rpt");
  59.  
  60. ///pass the report to method for dataInfo
  61. getDBInfo(rpDoc);
  62. /// et the source for report to be displayed
  63. CrystalReportViewer1.ReportSource = rpDoc;
  64. }
  65.  
  66. protected static void getDBInfo(ReportDocument rpDoc)
  67. {
  68. ///Connection Onject
  69. ConnectionInfo cn = new ConnectionInfo();
  70. ///DataBase,Table, and Table Logon Info
  71. Database db;
  72. Tables tbl;
  73. TableLogOnInfo tblLOI;
  74.  
  75. ///Connection Declaration
  76. cn.ServerName = "??????";
  77. cn.DatabaseName = "???????";
  78. cn.UserID = "???????";
  79. cn.Password = "????????";
  80.  
  81. //table info getting from report
  82. db = rpDoc.Database;
  83. tbl = db.Tables;
  84.  
  85. ///for each loop for all tables to be applied the connection info to
  86. foreach (Table table in tbl)
  87. {
  88. tblLOI = table.LogOnInfo;
  89. tblLOI.ConnectionInfo = cn;
  90. table.ApplyLogOnInfo(tblLOI);
  91. table.Location = "DBO." + table.Location.Substring(table.Location.LastIndexOf(".") + 1);
  92.  
  93. }
  94.  
  95. db.Dispose();
  96. tbl.Dispose();
  97. }
  98.  
  99. <CR:CrystalReportViewer
  100. ID="CrystalReportViewer1"
  101. runat="server"
  102. AutoDataBind="true"
  103. EnableDatabaseLogonPrompt="false"
  104. />
Add Comment
Please, Sign In to add comment