Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hello. I've always developed in and windows forms, but now i'm required to migrate some things to / 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.SelectedIndex = 0;

This is the code that populates the crystal report viewer:
private void ChangeRPT(string link)

CrystalReportViewer1.Visible = true;

if (IsPostBack)
CrystalReportViewer1.ReportSource = null;

ReportDocument report = new ReportDocument();


reportDB = report.Database;
reportTables = reportDB.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";

//report.SetDatabaseLogon("user", "pass", "server", "db");

catch (Exception)


I also have this to export to PDF:
private void ExportReport(string link)

ReportDocument report = new ReportDocument();
report.SetDatabaseLogon("user", "pass", "server", "db");

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.ContentType = "application/pdf";

This is the code for the button:
protected void Button1_Click(object sender, EventArgs e)
ChangeRPT(dirpath + dd_Reports.SelectedItem.Value);
//ExportReport(dirpath + dd_Reports.SelectedItem.Value);

This is page code:
    <form id="form1" runat="server">
        <table width="100%">
                <td height="20">
                    <asp:DropDownList ID="dd_Reports" runat="server" Width = "350px">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
                        AutoDataBind="true" />

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;


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;

CrystalReportViewer1.ReportSource = cryRpt;


ReportDocument report = new ReportDocument();
report.SetDatabaseLogon("user", "pass", @"server", "db");
CrystalReportViewer1.ReportSource = report;

This is called from a button click event.
Updated 31-Jan-11 2:50am
shreekar 31-Jan-11 7:54am    
What is the output?
Have you checked that the path of the reports directory is accessible from where the web page is located?
Henry Minute 31-Jan-11 8:55am    
The OP has responded to your comment. I have added his response at the bottom of the original question.

1 solution

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900