Solved

Trouble using a CrystalReports viewer on a VB2010 form.

Posted on 2010-11-29
59
3,521 Views
Last Modified: 2013-11-27
I have been having trouble using a Crystal Reports viewer on my VB2010 form.
I have tried a number of different methods, so I think it's best that I just start from scratch.

Please have patience with me, but can someone please talk me through step-by-step
how to bind my CrystalReport viewer control to an active dataset.

I have a command button on the form that should refresh the query.

txtName = the name you are searching for
Date1 = DateTimePicker that selects the start date
Date2 = DateTimePicker that selects the end date

When the user clicks the command button "Search" the report viewer should display all fields
relevant to the criteria entered.

I have deleted all my code and decided to just start with the report viewer on the form.
You can use any method you feel most comfortable with the talk me through.
Please, any help would be greatly appreciated.
Thank you.
0
Comment
Question by:--TripWire--
  • 31
  • 19
  • 8
  • +1
59 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 350 total points
ID: 34232137
0
 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 150 total points
ID: 34232313
Are you using the Crystal for VS2010.  They just released the final product.  It is a free download.
From what I have read, no other version of Crystal is supported on VS2010 or VB2010

http://www.businessobjects.com/jump/xi/crvs2010/default1.asp

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34233156
mlmcc,
There version I'm using is called Crystal Reports for Visual Studio 2010 from SAP Business Objects.
Is that the same program?

I am currently uninstalling it and going to install the version in the link.

Eric,
I get a problem when I get to this part of the code....

'Implies you have imported CrystalDecisions.CrystalReports.Engine
Private mrptDoc As New ReportDocument
'Load the standalone report
mrptDoc.Load(Forms.Application.StartupPath & "\rptTest.rpt")
'Pass the dataset to the report
mrptDoc.SetDataSource(mdsData)
'Pass the Report Document object to the viewer
CrystalReportViewer1.ReportSource = mrptDoc

The private identifier is not valid.  Would Dim be the equivalent?
Also, mrptDoc.Load isn't a selectable method.  It won't allow me to use the parameter Forms.Application.xxx
Is there some other library that I'm not including that I should be?
0
 

Author Comment

by:--TripWire--
ID: 34233402
Another problem I get is with the CrystalReport viewer on the form, when the form loads, I get prompted for a Database Login.
It asks for .....Login ID and Password
It has an option for "use integrated security".
My options are Finish or Cancel.  Cancel just brings up a blank report with none of the fields queried and Finish brings up another dialog that says "Logon Failed. Please try again"
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34233548
have you tried to convert the demo to VS2010 / CR2010? I didn't had the chance to convert it to the new CR2010 yet
0
 

Author Comment

by:--TripWire--
ID: 34233667
Yes.  Is there a different way of doing it?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34233706
I don't. As I told, I haven't played with CR2010 yet (and won't have time probably before the weekend).
0
 

Author Comment

by:--TripWire--
ID: 34233719
Okay.  But what about the login?  It was giving me this error even before I upgraded.  Know how to get rid of it?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34233758
if you push a dataset (like I have been suggesting for years), you will never been ask for credentials.
0
 

Author Comment

by:--TripWire--
ID: 34233861
What do you mean 'push' a dataset?
Do you mean like using push and pop methods? (as in a stack?)
Please explain
0
 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 150 total points
ID: 34233873
Here is a method of changing the data source and passing the login credentials.

http://bytes.com/topic/visual-basic-net/answers/861922-change-datasource-crystalreport-vb-net

I would recommend looking into the passing the dataset method.  Since you will never have the issue of changing the data source or login credentials.

mlmcc
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34233878
Yes, the link is to the production version of what you are currently using.

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34233976
I tried this in the Form_Load...

CrystalReportViewer1.ReportSource = "CrystalReport6.rpt"
CrystalReportViewer1.Zoom(1)

But when I run it, I get an error message - Load report failed.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34234045
What other code are you using?

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34234080
The only other event in the Form_Load is...
Me.TblMyReportTableAdapter.Fill(Me.MyTestDataSet.tblTable1)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34235033
>>What do you mean 'push' a dataset?

My article explains exactly what I mean. In short, instead of having CR retreiving data from the database, you pass a dataset to the report.
0
 

Author Comment

by:--TripWire--
ID: 34236056
I downloaded your sample project and when I tried running the methods in order, however it ran an error saying....Load report failed on this line  <------



Private Sub btnViewReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        'Insure that the dataset is filled
        If mdsData Is Nothing Then
            MessageBox.Show("You first need to create the DataSource!", "Not ready to show the report", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        'Load the standalone report
        mrptDoc.Load(Forms.Application.StartupPath & "\rptTest.rpt") <---------------
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34237636
if you read the article, you will find that you need to copy the .rpt into the same folder where your application is compiling/running
0
 

Author Comment

by:--TripWire--
ID: 34242031
Ok, that worked.  I now tried using it in my case.
Using the code from the example, I keep getting an error on this line of code...

mrptDoc.Load(Forms.Application.StartupPath & "\CrystalReport5.rpt")

NullReferenceException was unhandled - Object reference not set to an instance of object

I've already checked to see if the dataset was null before going to that line of code.  What could be wrong?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34242080
is mrptDoc null? it should exist at that point.
0
 

Author Comment

by:--TripWire--
ID: 34242999
shouldn't the ....

if mrptDoc is Nothing Then
     messagebox("...")
end if

account for that?
----

Is there an easier way to do this?  I really don't need anything complicated.  I just want the report to run the query that's already in the datasource.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34243800
the messagebox is for the dataset not the datareport. go back to the article and the example. everything is in there!
0
 

Expert Comment

by:fleets9876
ID: 34244280
If you are getting Load Report Failed, make sure you have the full file path of the report.  It doesn't like if you use a server name (\\servername\c$\xxxx)...you have to use C:\xxxx
0
 

Author Comment

by:--TripWire--
ID: 34244336
Thanks for the suggestion, but unfortunately it still didn't work.  Are spaces allowed?  I'm assuming so since its in quotes.

It's still the same error...Load Report Failed.

Eric - I've used up to this part of your code so far.
--------------------------------
You now need to write a couple of lines of code in order to load a report, pass a dataset to the report and display it into the viewer. This is the code you need:

'Load the standalone report
mrptDoc.Load(Forms.Application.StartupPath & "\rptTest.rpt")
'Pass the dataset to the report
mrptDoc.SetDataSource(mdsData)
'Pass the Report Document object to the viewer
CrystalReportViewer1.ReportSource = mrptDoc

You also need to define your mrptDoc variable at the top of your class like this:

'Implies you have imported CrystalDecisions.CrystalReports.Engine
Private mrptDoc As New ReportDocument
--------------
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34244422
The report is in the applicatiion directory, right?

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34244471
Yes.  To make sure, I copied it to string variable and output the value to a textbox.
I navigated to the folder using that string and placed a copy in there.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34244631
have you been able to get the sample to run?
0
 

Author Comment

by:--TripWire--
ID: 34245376
Yes.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34246936
if the sample is working, duplicate the steps and your own report should as is
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:--TripWire--
ID: 34249469
That's what I've been working on, but it still seems to not want to work with me.
Could it have anything to do with the fact that you populated your data manually and I'm migrating mine from a backend MDB?
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34250197
Was the report built using an XSD file as the source or did you use the tables directly?

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34250485
I've tried both.

I think this report is getting far too complicated right now for what I want to do.  Maybe I should change my question.

Can I change the layout of a (regular) ReportViewer?  As in does it strictly have to be rows and columns of data, or can I reorganize the fields in the designer somehow?
Also, I want all of my fields to appear on just one sheet of paper for easy printing.  Can I restrict how it expands?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34250554
the source of the dataset has nothing to do. if you created your xsd correctly it will be fine. have you been able to see the fields from your XSD in the CR designer?
0
 

Author Comment

by:--TripWire--
ID: 34250579
Yes, I can see the fields in the designer itself...however I'm not able to launch the report with the actual fields showing.
(I'm still getting the database login)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34250591
have you recreated your report from the XSD without ever plugging it to your MDB?
0
 

Author Comment

by:--TripWire--
ID: 34250799
I'm not sure what you mean.
When I open the xsd file by itself I see a  designer version of the MDB
I see the table's fields with the PK symbol next to the appropriate fields.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34250928
when you create your report, you need to connect to your XSD to get the structure (and not to the database itself).
0
 

Author Comment

by:--TripWire--
ID: 34251093
I thought that is what's happening when I entered this line into my Form_Load event?

mdsData.WriteXmlSchema("c:\MyTemp\DemoSchema.xsd")
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34251555
No.  Create a new report, and use the XSD file as the data source rather than a database.

If you use a database for the report then Crystal wants to open and connect to the database even if you have changed the data source for the report.

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34251563
In that case, I don't think a standalone report is going to work for me because my data is generated on the form of the first project.

Do you have a blog that outlines how to do it in one project?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34251762
>>I thought that is what's happening when I entered this line into my Form_Load event?
>>mdsData.WriteXmlSchema("c:\MyTemp\DemoSchema.xsd")

As I wrote in my article, this line is required to generate the XSD file. It doesn't have to be generated everytime you want to populate the report. You only need to create once when you have the dataset and just before starting to create the report. This is why I have demoed it into a separate button.

>>In that case, I don't think a standalone report is going to work for me because my data is generated on the form of the first project.

The XSD DOES NOT contain data. Your data structure cannot change. So you only need to create the XSD once, then create your report based on this XSD. Finally, push the same dataset at runtime to your newe report.
0
 

Author Comment

by:--TripWire--
ID: 34252787
I'm sorry if this is information overload....but I just feel like I've tried everything and nothing's working.  Here is all the code in my main app.  Can you see anything glaringly wrong?


Option Strict On
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Windows

Public Class frmSearchByRef

    Private mdsData As DataSet
    Private mrptDoc As New ReportDocument


    Private Sub frmSearchByRef_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'GOVBODY_Assessment_TestDataSet.tblRefReport' table. You can move, or remove it, as needed.
        Me.TblRefReportTableAdapter.Fill(Me.GOVBODY_Assessment_TestDataSet.tblRefReport)
        mdsData = GOVBODY_Assessment_TestDataSet

        'mdsData.WriteXmlSchema("c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\GOVBODY MyOfficial Assessor\GOVBODY MyOfficial Assessor\bin\Debug\DemoSchema.xsd")

        If GOVBODY_Assessment_TestDataSet Is Nothing Then
            MessageBox.Show("There is not data!")
        End If
        'Load the standalone report
        mrptDoc.Load(Forms.Application.StartupPath & "c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\GOVBODY MyOfficial Assessor\GOVBODY MyOfficial Assessor\bin\Debug\CrystalReport2.rpt")
        'Pass the dataset to the report
        mrptDoc.SetDataSource(mdsData)
        'Pass the Report Document object to the viewer
        CrystalReportViewer1.ReportSource = mrptDoc


    End Sub

    Private Sub ApplyTextObject(ByVal vstrTextObject As String, ByVal vstrTextValue As String)
        Try
            CType(mrptDoc.ReportDefinition.ReportObjects(vstrTextObject), CrystalDecisions.CrystalReports.Engine.TextObject).Text = vstrTextValue
        Catch ex As Exception
            MessageBox.Show("ApplyTextObject" & Environment.NewLine & "There is no >>" & vstrTextObject & "<< object in this report!")
        End Try
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        'Filters the binding source to diplay only records that match
        'the MyOfficial's name AND date range, specified by the user
        Me.TblRefReportBindingSource.Filter = _
            String.Format("(Official LIKE '%{0}%' OR Second_Official LIKE '%{0}%' OR Third_Official LIKE '%{0}%' OR Fourth_Official LIKE '%{0}%') AND Date >= #{1:M/dd/yyyy}# AND Date <= #{2:M/dd/yyyy}#", _
                Me.txtOfficial.Text, DateTimePicker1.Value, DateTimePicker2.Value)

    End Sub

    Private Sub cmdGoBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGoBack.Click
        Me.Close()
        frmMain.Show()
    End Sub
End Class
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34252888
How did you create this report CrystalReport2.rpt?

Did you use the XSD file as the data source?
If it was built against a database rather than the XSD file you need to recreate the report.

mlmcc
0
 

Author Comment

by:--TripWire--
ID: 34253222
I created a new project (CrystalReports Application)
I added the XSD to the project and went through the report wizard selecting
Standard Report > Project Data/ADO .Net DataSets

I then copied the .rpt file to the same directory as my main project.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34253908
>>mrptDoc.Load(Forms.Application.StartupPath & "c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\GOVBODY MyOfficial Assessor\GOVBODY MyOfficial Assessor\bin\Debug\CrystalReport2.rpt")

this can't work for sure!

mrptDoc.Load(Forms.Application.StartupPath & "\CrystalReport2.rpt")
0
 

Author Comment

by:--TripWire--
ID: 34253976
Someone in an earlier post said that sometimes VB doesn't like when you give a relative path...but instead use absolute paths just to be sure.  Is this not so?
0
 

Author Comment

by:--TripWire--
ID: 34253992
nevermind my last comment, I just realized what you're saying.
0
 

Author Comment

by:--TripWire--
ID: 34254041
Thanks for that, I think I might be closer.
The error is on the next line now...

mrptDoc.SetDataSource(mdsData)

datasource exception was unhandled
"the report has no tables"
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34254199
your report is not created correctly!
0
 

Author Comment

by:--TripWire--
ID: 34254875
I created it in a different project according to the article.
What's wrong with it?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34256199
and you have added the XSD as the datasource, added fields from the XSD to the report, saved the report, copied the report?
0
 

Author Comment

by:--TripWire--
ID: 34257956
Yes.
But just to be sure. When you say added the xsd datasource, you mean to the project with the report, correct? and not the main project.

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34258031
when you create your report, you need to select a datasource. this datasource needs to be your xsd. never connect to any database in CR
0
 

Author Comment

by:--TripWire--
ID: 34259270
Whenever I try to run my report (in the other project) I keep getting this error....

Visual Stuidio cannot start debugging because the debug target 'C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\CrystalReportsApplication2\bin\Debug\CrystalReportsApplication2.exe' is missing. Please build the proejct and retry, or set the OutputPath and AssemblyName properties appropriately to point at the correct location for the target assembly.
0
 

Author Comment

by:--TripWire--
ID: 34259483
System.IO.FileNotFoundException was unhandled
  Message=Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll
  FusionLog==== Pre-bind state information ===
LOG: User = NDC_PC2\Administrator
LOG: Where-ref bind. Location = C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll
LOG: Appbase = file:///C:/Documents and Settings/Administrator/My Documents/Visual Studio 2010/Projects/GOVBODY MyOfficial Assessor/GOVBODY MyOfficial Assessor/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\GOVBODY MyOfficial Assessor\GOVBODY MyOfficial Assessor\bin\Debug\GOVBODY MyOfficial Assessor.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/Program Files/SAP BusinessObjects/Crystal Reports for .NET Framework 4.0/Common/SAP BusinessObjects Enterprise XI 4.0/win32_x86/dotnet1/crdb_adoplus.dll.

  StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
       at System.Reflection.Assembly.LoadFrom(String assemblyFile)
       at CrystalDecisions.ReportAppServer.DataSetConversion.DataSetConverter.DataSetProcessingDelegate(IntPtr arg)
  InnerException:
0
 

Author Comment

by:--TripWire--
ID: 34259493
Above is the exception I get when I try to run the report from my main project...
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34261470
if you followed my article, the project that contains the reports is not a project that you compile and run. it is only useful to contain the reports and edit them in the designer.

Can I suggest that you step back to my article and try to reproduce it. It should have been working since a long time now!
0
 

Author Comment

by:--TripWire--
ID: 34275763
The project would not run as a separate entity.  I had to change my App.Config file xml header, change the .net framework and run the report within the original project for it to run.

However, my report still coming up empty.  I know this has been frustrating so far, but I feel as if it's very close now.  Any advice would be appreciated.
0
 

Author Closing Comment

by:--TripWire--
ID: 34284041
This thread provides lots of code to that really helps with my problems solution.  The only reason I didn't award this solution an A is because one of my other problems was that I had to change the app.config files xml headers and change my .net framework to basic.

I do thank everyone who participated and hope that we can work together soon.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now