Solved

Crystal Report in C#

Posted on 2011-03-14
12
1,124 Views
Last Modified: 2012-05-11
I am trying to open a Crystal Report in C# 2008.  I have a report named rptJournal and a form named frmJournal with a viewer named crystalreportviewer1.  The report's data comes from a stored procedure I've linked with SQLServer.  I'm running the same thing in vb6 but seems like the syntax is different.  :)

Thanks,
Jerry
0
Comment
Question by:JDL129
12 Comments
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 35127934
What you want?
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35128169
That is very true.  The .Net API is much different from the VB6 .COM API

This article will help you migrate from the RDC to the .NET assemblies
http://www.sdn.sap.com/irj/boc/index?rid=/library/uuid/e0eb394f-931e-2b10-3b82-9413bfc3f457

mlmcc
0
 
LVL 3

Expert Comment

by:brd24gor
ID: 35128629
These two procedures should help you out. The first one I use to feed information into the report like the credentials, server, report location, etc. The second is needed to feed the credentials to each table to actually read in the report. Don't forget you'll have to import CrystalDecisions.CrystalReports.Engine and CrystalDecisions.Shared.
private void ConfigureCrystalReports()
        {
            ReportDocument report = new ReportDocument();
            report.FileName = Server.MapPath(@"your report location");

            ConnectionInfo connectionInfo = new ConnectionInfo();
            connectionInfo.ServerName = "your database server";
            connectionInfo.DatabaseName = "your database name";
            connectionInfo.UserID = "your database user id";
            connectionInfo.Password = "your database password";

            SetDBLogonForReport(connectionInfo, report);

            CrystalReportViewer1.ReportSource = report;
        }

        private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
        {
            Tables tables = reportDocument.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
            {
                TableLogOnInfo tableLogonInfo = table.LogOnInfo;
                tableLogonInfo.ConnectionInfo = connectionInfo;
                table.ApplyLogOnInfo(tableLogonInfo);
            }
        }

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:JDL129
ID: 35129267
brd24gor!!!!!!!!!!!!
Thanks for the response!!

I get:
'Error      52      The type or namespace name 'ReportDocument' could not be found (are you missing a using directive or an assembly reference?)      C:\POSiTrackC\POSiTrackC\POSiTrackC\POSiTrackC\frmJournal.cs      23      13      POSiTrackC
After adding the references.

Thanks,
Jerry
0
 
LVL 3

Expert Comment

by:brd24gor
ID: 35129599
In Visual Studio (with your project open), right-click your project in the Solution Explorer and click Property Pages. Under References, do you have any Crystal Decisions entries? If so, what is the version number? If not, click on Add Reference in the Property Pages window. Find and add CrystalDecisions.CrystalReports.Engine, CrystalDecisions.ReportSource, CrystalDecisions.Shared, CrystalDecisions.Web (if using ASP.NET), and CrystalDecisions.Forms (if programming desktop application).

What version of Crystal Reports are your reports?
0
 

Author Comment

by:JDL129
ID: 35130046
brd24gor!!!!!!!!
Thanks for the response!!

I have .CrystalReports.Engine, .Enterprise.framework, .Enterprise.InfoStore, .ReportSource,
.Shared, and .Windows.Forms.  All are version 10.5.3700.0.

I have created the report with C# Crystal Reports.

I have used a stored procedure in SQLServer 2005 express for the datasource so I shouldn't need to use the data connection info should I?

Thanks again,
Jerry
0
 
LVL 3

Expert Comment

by:brd24gor
ID: 35130454
I believe you do because when you connect via Crystal Reports, the ODBC driver handles the connection. In code, you need to handle it.

ReportDocument is in CrystalDecisions.CrystalReports.Engine. As long as you have a 'using CrystalDecisions.CrystalReports.Engine' clause and the reference present, it should work.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data.SqlClient;

Open in new window

0
 

Author Comment

by:JDL129
ID: 35130955
brd24gor!!!!!!!!!!
Thanks for hanging with me!!

After adding the System.Data.SqlClient ( I already had added the others) I'm getting an error:
The type or namespace name 'ConnectionInfo' could not be found.  What other directive or assembly reference do I need to add?

Thanks,

Jerry
0
 

Author Comment

by:JDL129
ID: 35131045
brd24gor!!!

I am also having problems with the line:
report.FileName = Server.MapPath(@"your report location");

I'm getting an error at 'Server' and I'm not quite sure how to put in my report location.  

Thanks,
Jerry
0
 
LVL 3

Expert Comment

by:brd24gor
ID: 35131125
First part: ConnectionInfo is in the CrystalDecisions.Shared library. If you have that assembly and directive then I'm not sure why the namespace isn't being recognized. If you  start typing Conn, does Intellisense bring anything up?

As for the filename, you specify the path in relation to your project root. For example, if my project is in directory Foo and my report is in a subfolder called Bar, then my code would be:

report.FileName = Server.MapPath(@"\Foo\Bar\report.rpt");
0
 

Author Comment

by:JDL129
ID: 35138174
brd24gor!!!!!!!!1
Thanks again for hangin' in here with me!!!

I still have a few errors:
SetDBLogonForReport and Server do not exist in the current context.  

Thanks for your help!!
Jerry
0
 
LVL 3

Accepted Solution

by:
brd24gor earned 125 total points
ID: 35138336
That is actually for a website, so if you're doing a Forms app then you can just declare the full path to the report on the hard drive:

report.FileName = @"C:\xxx\xxx\xxx\report.rpt"

As for SetDBLogonForReport, that's the second user-defined procedure in my code sample above. Make sure both procedures, SetDBLogonForReport and ConfigureCrystalReports, are in the same namespace and class. If you are calling them from a different class, then you'll have to declare one or both protected or public.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question