I have already created Crystal Report for this example. You can read my previous article for creating Crystal Report,
Step By Step Creating Grouping In Crystal Report

Now follow the below steps for calling Crystal Report from ASP.NET code.
Step 1: Create a new ASP.NET project and add report in your project, here I have created Report folder and added 'CrystalReport2.rpt' file in it.
Step 2: Now add WebForm in your project by right click on Project, Add, then clicking Webform. Give it a name,
Step 3: Now, let's design your page, Drag and Drop 'RadioButtonList' and add 2 Items 'PDF' and 'Excel' add button, give a name 'btnSubmit' .
  1. <div>
  2. <asp:RadioButtonList ID="RadioButtonList1" runat="server">
  3. <asp:ListItem Selected="True">PDF</asp:ListItem>
  4. <asp:ListItem>Excel</asp:ListItem>
  5. </asp:RadioButtonList>
  6. <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  7. </div>
Step 5: For Crystal Report you need 2 packages from NuGet Package manager: CrystalDecisions.Shared and CrystalDecisions.CrystalReports.Engine,
Step 6: After that we have to add assemblies in our code.
  1. using CrystalDecisions.CrystalReports.Engine;
  2. using CrystalDecisions.Shared;
  3. using System.IO;
  4. using System.Configuration;
Step 7: For Connection String I have added Keys in Web.Config file, Add the following code in your web.config in <appSettings> tab.

Here I have created 4 keys for Server name, Database name, User Id, and Password.
Step 8: On Button Click event, add the following code:
  • Create object for crystal report Export option class.
  • Create object for destination option - for set path of your pdf/excel file save.
  • Map your crystal report path.
  • Set database Connection.
  • Select report format (pdf/excel).
  1. protected void btnSubmit_Click(object sender, EventArgs e)
  2. {
  3. //Create object for crystal report Export option class
  4. ExportOptions exopt = default(ExportOptions);
  5. //create object for destination option - for set path of your pdf file save
  6. DiskFileDestinationOptions dfdopt = new DiskFileDestinationOptions();
  7. ReportDocument RptDoc = new ReportDocument();
  8. //Map your crystal report path
  9. RptDoc.Load(Server.MapPath("~/Report/CrystalReport2.rpt"));
  10. //Set database Connection
  11. setDbInfo(RptDoc, ConfigurationManager.AppSettings["V_DSN"], ConfigurationManager.AppSettings["V_DB"], ConfigurationManager.AppSettings["V_DBUserID"], ConfigurationManager.AppSettings["V_DBPwd"]);
  12. if (RadioButtonList1.SelectedItem.Text.ToString() == "PDF")
  13. {
  14. //Report pfd name
  15. string fname = "Report1.pdf";
  16. dfdopt.DiskFileName = Server.MapPath(fname);
  17. exopt = RptDoc.ExportOptions;
  18. exopt.ExportDestinationType = ExportDestinationType.DiskFile;
  19. //for PDF select PortableDocFormat for excel select ExportFormatType.Excel
  20. exopt.ExportFormatType = ExportFormatType.PortableDocFormat;
  21. exopt.DestinationOptions = dfdopt;
  22. //finally export your report document
  23. RptDoc.Export();
  24. //To open your PDF after save it from crystal report
  25. string Path = Server.MapPath(fname);
  26. FileInfo file = new FileInfo(Path);
  27. Response.ClearContent();
  28. Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  29. Response.AddHeader("Content-Length", file.Length.ToString());
  30. Response.ContentType = "application/pdf";
  31. Response.TransmitFile(file.FullName);
  32. Response.Flush();
  33. RptDoc.Dispose();
  34. RptDoc.Close();
  35. RptDoc = null;
  36. ////End
  37. }
  38. else
  39. {
  40. // Excel Code goes here...
  41. string fname = "Report1.xls";
  42. dfdopt.DiskFileName = Server.MapPath(fname);
  43. exopt = RptDoc.ExportOptions;
  44. exopt.ExportDestinationType = ExportDestinationType.DiskFile;
  45. exopt.ExportFormatType = ExportFormatType.Excel;
  46. exopt.DestinationOptions = dfdopt;
  47. RptDoc.Export();
  48. string Path = Server.MapPath(fname);
  49. FileInfo file = new FileInfo(Path);
  50. Response.ClearContent();
  51. Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  52. Response.AddHeader("Content-Length", file.Length.ToString());
  53. Response.ContentType = "application/excel";
  54. Response.TransmitFile(file.FullName);
  55. Response.Flush();
  56. RptDoc.Dispose();
  57. RptDoc.Close();
  58. RptDoc = null;
  59. }
  60. }
Step 9 : Here I have created common function for connecting database, and tables. Copy and paste the following code in your .cs file.
  1. /// <summary>
  2. /// setDbInfo - for connecting database table
  3. /// </summary>
  4. /// <param name="rptDoc">Crystal Report </param>
  5. /// <param name="ServerName">Database Server name </param>
  6. /// <param name="DatabaseName">Database name </param>
  7. /// <param name="UserName">DB Username </param>
  8. /// <param name="Password">DB Password </param>
  9. protected void setDbInfo(ReportDocument rptDoc, string Server, string dbName, string UserId, string Pwd)
  10. {
  11. TableLogOnInfo logoninfo = new TableLogOnInfo();
  12. // connect multiple tabel
  13. foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in rptDoc.Database.Tables)
  14. {
  15. logoninfo = tbl.LogOnInfo;
  16. logoninfo.ReportName = rptDoc.Name;
  17. logoninfo.ConnectionInfo.ServerName = Server;
  18. logoninfo.ConnectionInfo.DatabaseName = dbName;
  19. logoninfo.ConnectionInfo.UserID = UserId;
  20. logoninfo.ConnectionInfo.Password = Pwd;
  21. logoninfo.TableName = tbl.Name;
  22. tbl.ApplyLogOnInfo(logoninfo);
  23. tbl.Location = tbl.Name;
  24. }
  25. }
Step 10 : Now run your application, select PDF and click 'Submit'. It will generate PDF report and if you want Excel Report then select Excel and click on 'Submit' Button.
Here, we have't used CrystalReportViewer for Load and Display Report, Instead we have directly loaded PDF or excel report.

Hope you liked the article.