Hello. I've always developed in vb.net and windows forms, but now i'm required to migrate some things to asp.net / c#. One of the problems i'm facing is Crystal Reports. In one of my projects there is a form that gets all reports in a network location and puts their names in a combo box, then the user selects one and it displays in a crystal reports viewer. Easily done... before! The problem is now in asp. I've tried several techniques and nothing works. Not even exporting.
I'm using these references in the project:
using System;
using System.IO;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Reflection;
using System.Drawing;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Net.Mail;
This is the code at page load that populates the combo box:
ReportDocument report;
Database reportDB;
Tables reportTables;
TableLogOnInfo reportLogOnInfo;
private string dirpath = @"\\NetworkLocation\";
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(dirpath);
foreach (FileInfo files in dir.GetFiles())
{
dd_Reports.Items.Add(files.Name);
}
dd_Reports.SelectedIndex = 0;
}
This is the code that populates the crystal report viewer:
private void ChangeRPT(string link)
{
try
{
CrystalReportViewer1.Visible = true;
if (IsPostBack)
{
CrystalReportViewer1.ReportSource = null;
ReportDocument report = new ReportDocument();
report.Load(link);
reportDB = report.Database;
reportTables = reportDB.Tables;
SetTableLocation(report.Database.Tables);
CrystalReportViewer1.ReportSource = report;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportTables)
{
reportLogOnInfo = table.LogOnInfo;
reportLogOnInfo.ConnectionInfo.ServerName = "server";
reportLogOnInfo.ConnectionInfo.DatabaseName = "db";
reportLogOnInfo.ConnectionInfo.UserID = "user";
reportLogOnInfo.ConnectionInfo.Password = "apass";
table.ApplyLogOnInfo(reportLogOnInfo);
}
CrystalReportViewer1.DataBind();
}
}
catch (Exception)
{
throw;
}
}
I also have this to export to PDF:
private void ExportReport(string link)
{
ReportDocument report = new ReportDocument();
report.SetDatabaseLogon("user", "pass", "server", "db");
report.Load(link);
DiskFileDestinationOptions dFileDOpts = new DiskFileDestinationOptions();
ExportOptions eOpts = new ExportOptions();
dFileDOpts.DiskFileName = @"C:\rep.pdf";
eOpts = report.ExportOptions;
eOpts.DestinationOptions = dFileDOpts;
eOpts.ExportDestinationType = ExportDestinationType.DiskFile;
eOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
report.ExportToDisk(ExportFormatType.PortableDocFormat, @"C:\rep.pdf");
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.WriteFile(@"C:\rep.pdf");
Response.Flush();
Response.Close();
}
This is the code for the button:
protected void Button1_Click(object sender, EventArgs e)
{
ChangeRPT(dirpath + dd_Reports.SelectedItem.Value);
}
This is page code:
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td height="20">
<asp:DropDownList ID="dd_Reports" runat="server" Width = "350px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<td>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true" />
</td>
</tr>
</table>
</div>
</form>
</body
Can anyone please just take a look at what i may be doing wrong? I'm a bit in the dark here! Any help is appreciated...
Thank you very much
[Moved from Answer - Henry]
There is no output. I found part of the problem, changed the crystal reports solution references from crystal reports 2008 to XI and it loads the reports. The problem now is with login parameters. Even though the login matches the one in the report, it still asks for credentials after the load.
I've tried both these methods but none works. It always asks for the password.
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;
cryRpt.Load(link);
crConnectionInfo.ServerName = "server";
crConnectionInfo.DatabaseName = "db";
crConnectionInfo.UserID = "user";
crConnectionInfo.Password = "pass";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
CrystalReportViewer1.ReportSource = cryRpt;
CrystalReportViewer1.RefreshReport();
ReportDocument report = new ReportDocument();
report.Load(link);
report.SetDatabaseLogon("user", "pass", @"server", "db");
CrystalReportViewer1.ReportSource = report;
This is called from a button click event.
[/Moved]