Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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);
}
 

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

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);
//ExportReport(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]
Posted 28-Jan-11 1:51am
Edited 31-Jan-11 3:50am
Henry Minute223.9K
v3
Comments
shreekar at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 Sergey Alexandrovich Kryukov 170
4 PIEBALDconsult 160
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 3 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100