Solved

ASP.net Crystal Report Subreport in PDF not using Selection Formula

Posted on 2004-08-23
17
998 Views
Last Modified: 2008-01-09
I am exporting a Report to PDF the selection criteria works in the sub report with crystal report viewer included with .net. However when I export to PDF the selection criteria only seems to apply to the main report and not any subreports. How do I set the selection criteria for the subreport before exporting to the pdf?

here is the code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim crReportDocument As ReportDocument
        Dim crExportOptions As ExportOptions
        Dim crDiskFileDestinationOptions As New DiskFileDestinationOptions
        Dim Fname As String

        crReportDocument = New RFMSQuote
        crReportDocument.DataDefinition.RecordSelectionFormula = "{QuoteKitchen.QuoteID}=" & Int(Request.QueryString("ID"))


        Fname = Server.MapPath("Exports\" & Session.SessionID.ToString & ".pdf")
        crDiskFileDestinationOptions = New DiskFileDestinationOptions
        crDiskFileDestinationOptions.DiskFileName = Fname
        crExportOptions = crReportDocument.ExportOptions
        With crExportOptions
            .DestinationOptions = crDiskFileDestinationOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
        End With
        crReportDocument.Export()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Response.WriteFile(Fname)
        Response.Flush()
        Response.Close()
        System.IO.File.Delete(Fname)


    End Sub
0
Comment
Question by:o0JoeCool0o
  • 9
  • 7
17 Comments
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
why dont you create a parameter in the sub report and then link the main report to sub report
using that variable. Then open the sub report and use the linked field to create your sleection criteria. From your code, set the sub report parameter value like this

Reportdocument.setparametervalue(value,"subreportname")
Also you should set the main report parameter values as well

Regards
Emre
0
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
Also you can use the recordselectioncriteria formula in code to set the record selection criteria of your subreport

check this link out too. Do whatever is best for you
http://support.businessobjects.com/library/kbase/articles/c2002292.asp

Regards
Emre
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
"Also you can use the recordselectioncriteria formula in code to set the record selection criteria of your subreport"

this is what im trying to do but as u can see in my above code, this does not work because i cant declare the subreport for all I know.
I was hoping theres some thing like crReportDocument.Subreport("MySubReport").SelectionFormula = ""
0
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
ohh I m srry/ Yu can get a reference to your subreport then treat it like the report document . I didnt see the record selection code. but still I can help,

This is how You get a reference to your subreport from the main report by code
check the code below but key is this line
MySubReport = MyReport.OpenSubreport(MySubReportObject.SubreportName);

Regards
Emre
private void GetSubReport(CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport)
{                  
CrystalDecisions.CrystalReports.Engine.ReportDocument MySubReport;
CrystalDecisions.CrystalReports.Engine.SubreportObject MySubReportObject;
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject in MyReport.ReportDefinition.ReportObjects)
{
if (MyReportObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
{
MySubReportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;
if (MySubReportObject.SubreportName != _subreportname)
{
MySubReport = MyReport.OpenSubreport(MySubReportObject.SubreportName);                               
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
that doesnt appear to be working, I tried using the opensubreport as well as tried Just declaringthe subreport as a report in the project
IE
Dim MySubReport as New Rooms

Rooms being the name of the subreport

directly after I called Mysubreport.datadefinition.selectionformula
after that I called the crReportDocument = New RFMSQuote to create an instance of the main report
then I set the selection formula for that report

I think im just not understanding...

maybe if u could post ur answer in vb.net?

0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
Its like the main report selection criteria works fine, but the subreport is listing all entries in teh table isntaead of Just the one row of data
0
 
LVL 42

Expert Comment

by:frodoman
Comment Utility
JoeCool -

Just to be clear - the report works fine in the viewer and it's only when exported that the additional data appears, correct?  What happens if you export to another format other than PDF - does it work correctly then?

In the subreport are you suppressing the unwanted data or is it filtered out with the selection criteria?  Also, to eliminate possible known bugs, does your subreport contain any BLOBS and/or hidden (not suppressed) sections?

frodoman
0
 
LVL 10

Accepted Solution

by:
ebolek earned 500 total points
Comment Utility
use it like this

private void GetSubReport(CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport)
{              
CrystalDecisions.CrystalReports.Engine.ReportDocument MySubReport;
CrystalDecisions.CrystalReports.Engine.SubreportObject MySubReportObject;
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject in MyReport.ReportDefinition.ReportObjects)
{
if (MyReportObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
{
MySubReportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;
MySubReport = MyReport.OpenSubreport(MySubReportObject.SubreportName);                          
MySubReport.DataDefinition.RecordSelectionFormula = "{QuoteKitchen.QuoteID}=" & Int(Request.QueryString("ID"))
}
else
MySubReport = null;
}
}
The this with the subreports are you loop through the main report document and then find it and get a reference to the subreport you found with Opensubreport method

In this code Mysubreport is the subreport report document You can set the selection criteria klike this after that. Hope it helps. If you have more questions let me know

Regards
Emre
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
use it like this

private void GetSubReport(CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport)
{              
CrystalDecisions.CrystalReports.Engine.ReportDocument MySubReport;
CrystalDecisions.CrystalReports.Engine.SubreportObject MySubReportObject;
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject in MyReport.ReportDefinition.ReportObjects)
{
if (MyReportObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject)
{
MySubReportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;
MySubReport = MyReport.OpenSubreport(MySubReportObject.SubreportName);                          
MySubReport.DataDefinition.RecordSelectionFormula = "{QuoteKitchen.QuoteID}=" & Int(Request.QueryString("ID"))
}
else
MySubReport = null;
}
}


which class are you typing this code for? the webform main page?
or in the codebehindfor the report?
0
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
I have a class on my own but for right now you can out it as a method on the form and then test it.

Did it work for you. It should. Your problem was, not being able t oget a reference to subreport. Now you are getting it and setting record selection criteria from the sub report documeny

Regards
Emre
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
Im getting specified Cast is not valid, im sure its just an error in my translation from C to VB,
crashign on this line
For Each MySubReportObject In MyReport.ReportDefinition.ReportObjects

heres the revisedcode

Private Sub GetSubReport(ByVal MyReport As CrystalDecisions.CrystalReports.Engine.ReportDocument)

        Dim MySubReport As CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim MySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
        For Each MySubReportObject In MyReport.ReportDefinition.ReportObjects

            If MySubReportObject.Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then


                MySubReport = MyReport.OpenSubreport(MySubReportObject.SubreportName)
                MySubReport.DataDefinition.RecordSelectionFormula = "{QuoteKitchen.QuoteID}=" & Int(Request.QueryString("ID"))

            Else
                MySubReport = Nothing
            End If
        Next
    End Sub
0
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
you get the error because you didnt cast the report to subreport
MySubReportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;


add this before you open the subreport. The report document has to be casted as subreport document. It is the implicit conversion of .net

Regards
Emre
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
Sorry for the silly questions but MySubReportObject is not defined anywhere, I am wondering if your meaning MyReport decalared in the ByVal paramter of the sub
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
sorry MyReportObject not mysubreportobject
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
nevermind, Pre Coffee moment
0
 
LVL 4

Author Comment

by:o0JoeCool0o
Comment Utility
IT WORKS!!! You are Awesome Emre Full points to u
0
 
LVL 10

Expert Comment

by:ebolek
Comment Utility
Glad to help man

Regards
Emre
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

762 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

9 Experts available now in Live!

Get 1:1 Help Now