We help IT Professionals succeed at work.

Need to know basics of printing a Cyrstal Report

PBLack
PBLack asked
on
Hi

I am using VB.Net 2005 and have created a crystal report successfully using the wizard. It is called standsummary.rpt.

I have no idea on how to proceed. What do I do to get this sucker to print? Please provide the basics. I have been teaching myself how to do the reports for hours now and have sucessfully created my first xml from a dataset. The Crystal Report is based on the XML. Where do I go next?

Here is my code in Form2 used to create the xml. No problems there. I just don't know how to tie the report printing process into this.

   'build report dataset
        Dim dt As New DataTable("StandInfo")
        dt.Columns.Add("StandNum")
        dt.Columns.Add("Area")
        dt.Columns.Add("SampleSize")
        dt.Columns.Add("TotalPoints")
        dt.Columns.Add("Intensity")

        Dim row As DataRow = dt.NewRow()
        Dim item As ListViewItem
        For Each item In lvStands.Items
            row("StandNum") = item.Text
            row("Area") = item.SubItems(1).Text
            row("SampleSize") = item.SubItems(2).Text
            row("TotalPoints") = item.SubItems(3).Text
            row("Intensity") = item.SubItems(4).Text
            dt.Rows.Add(row)
            row = dt.NewRow()
        Next

        Dim SourceDir As String = lblRPath.Text
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)

        If Not source.Exists Then
            source.Create()
        End If

        Dim fi As FileInfo

        For Each fi In source.GetFiles("*.xml")
            If fi.Name = "standsummary.xml" Then
                fi.Delete()
            End If
        Next

        dt.WriteXml(SourceDir & "\" & "standsummary.xml")

        MsgBox("table build complete")
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:

Author

Commented:
Hi

I am having a problem with:

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

I get an error that name Forms is not declared. So how do I fix that? Also I want all reports to reside in the folder Application.StartupPath & "\Reports". How do I programatically place the report there?

Thanks for the help

Author

Commented:
FYI here is my code. I changed the line:

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

mrptDoc.Load(Application.StartupPath & "\standsummary.rpt") and it throws the following error:

Load report failed.

All of this code is in the Load event of Form9 which contains CrystalReportViewer1

        'build report dataset
        Dim dt As New DataTable("StandInfo")
        dt.Columns.Add("StandNum")
        dt.Columns.Add("Area")
        dt.Columns.Add("SampleSize")
        dt.Columns.Add("TotalPoints")
        dt.Columns.Add("Intensity")

        Dim row As DataRow = dt.NewRow()
        Dim item As ListViewItem
        For Each item In Form2.lvStands.Items
            row("StandNum") = item.Text
            row("Area") = item.SubItems(1).Text
            row("SampleSize") = item.SubItems(2).Text
            row("TotalPoints") = item.SubItems(3).Text
            row("Intensity") = item.SubItems(4).Text
            dt.Rows.Add(row)
            row = dt.NewRow()
        Next

        Dim SourceDir As String = "c:\Temp"
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)

        If Not source.Exists Then
            source.Create()
        End If

        Dim fi As FileInfo

        For Each fi In source.GetFiles("*.xsd")
            If fi.Name = "standsummary.xsd" Then
                fi.Delete()
            End If
        Next

        dt.WriteXmlSchema("c:\Temp\standsummary.xsd")
        'Load the standalone report
        mrptDoc.Load(Application.StartupPath & "\standsummary.rpt")
        'Pass the dataset to the report
        mrptDoc.SetDataSource(dt)
        'Pass the Report Document object to the viewer
        CrystalReportViewer1.ReportSource = mrptDoc
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>mrptDoc.Load(Application.StartupPath & "\standsummary.rpt") and it throws the following error:
>>Load report failed.

if you do a:
messagebox.show(Application.StartupPath & "\standsummary.rpt")
it will give you the full path where the report should be. Is it there?

>>Also I want all reports to reside in the folder Application.StartupPath & "\Reports". How do I programatically place the report there?

Unless you have the designer to save directly to that folder (and I don't like this idea because if you have many reports, they are all in that same folder), there is no way. I have created a solution just to edit my reports. This solution saves the reports in a hierarchy of folders. I also have a batch file that copies from that hierarchy of folders to my single folder where the application is reading the reports from. When I modify a report, I run this batch file.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>dt.WriteXmlSchema("c:\Temp\standsummary.xsd")

You don't have to do it every time you print a report!

Author

Commented:
Hi again

I am sorry to be a pain in the butt. I am completely lost. I did get the report to open but it is void of data. I discovered that I had not set the build action to none...but now I have a host of other problems.

Can you please look everything over an point out to me where I went wrong. I know it is all newbie mistakes but I am trying hard to get a handle on this.

On form2 behind a print button I have:

   Dim frm As Form
        For Each frm In Me.MdiParent.MdiChildren
            If TypeOf frm Is Form9 Then
                frm.Close()
            End If
        Next

        'build report dataset
        Dim dt As New DataTable("StandInfo")
        dt.Columns.Add("StandNum")
        dt.Columns.Add("Area")
        dt.Columns.Add("SampleSize")
        dt.Columns.Add("TotalPoints")
        dt.Columns.Add("Intensity")

        Dim row As DataRow = dt.NewRow()
        Dim item As ListViewItem

        For Each item In lvStands.Items
            row("StandNum") = item.Text
            row("Area") = item.SubItems(1).Text
            row("SampleSize") = item.SubItems(2).Text
            row("TotalPoints") = item.SubItems(3).Text
            row("Intensity") = item.SubItems(4).Text
            dt.Rows.Add(row)
            row = dt.NewRow()
        Next

        Dim SourceDir As String = "c:\Temp"
        Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)

        If Not source.Exists Then
            source.Create()
        End If

        Dim fi As FileInfo

        For Each fi In source.GetFiles("*.xsd")
            If fi.Name = "standsummary.xsd" Then
                fi.Delete()
            End If
        Next

        dt.WriteXmlSchema("c:\Temp\standsummary.xsd")

Leave code for a side note:
+++++++++ I know you said not to do this but I do not think that this is the problem. From what I can gather this just makes an empty shell for the data to be stored in but does not actually house the data. So if I am correct is my above code for populating it not working?
How do I fix that part?++++++++++++

Back to code behind button:

        Dim F As New Form9
        F.MdiParent = Main
        F.Show()

On form 9 I have in the load event the following:

        'Load the standalone report

        mrptDoc.Load(Application.StartupPath & "\standsummary.rpt")
        'Pass the dataset to the report
        mrptDoc.SetDataSource("c:\Temp\standsummary.xsd")
        'Pass the Report Document object to the viewer
        CrystalReportViewer1.ReportSource = mrptDoc

This form also has CrystalReportView1 added and

Imports System.Data.DataSet
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form9
Private mrptDoc As New ReportDocument

So Eric if you would be so kind has to show me where I have gone wrong I would really appreciate it.

Thanks

PBlack
Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>mrptDoc.SetDataSource("c:\Temp\standsummary.xsd")

a .xsd file is a schema. it doesn't contain data.

Go through the example in my article. It will work.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.