SAP Knowledge Base Article - Public

1430332 - How to add a report parameter or formula in Visual Studio .NET to a TextObject

Symptom

  • Using the Report Application Server, when a text property is populated with the correct parameters string, only plain text string is displayed.
  • Setting mytextobject.text = "Report from {?FRMDATE } to {?TODATE} " results in plain text on the report as:

Report from {?FRMDATE } to {?TODATE}

  • In the Crystal Reports designer this would result in "Report from  01/01/2010 to 31/01/2010"
  • Similarly, only a string value is displayed when trying to insert Fields and formulas into a text object.

Resolution

  • Using RAS, a TextObject a TextObject has a Paragraphs collection.
  • Paragraphs collection is comprised of Paragraph objects
  • Paragraph contains ParagraphElements
  • ParagraphElement can be a crParagraphElementKindField, crParagraphElementKindText or crParagraphElementKindTab
  • Starting with a ParagraphTextElement object and a ParagraphFieldElement object, it is possible to construct a TextObject containing an embedded field.

Sample code:

Dim boReport As CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim rasClientDocument As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument
        Dim rasObjectController As CrystalDecisions.ReportAppServer.Controllers.ReportObjectController
        Dim rasTextObject As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
        Dim rasOldTextObject As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
        Dim rasParagraph As CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph
        Dim rasParagraphTextElement As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement
        Dim rasParagraphFormulaElement As CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphFieldElement
        Dim rasReportObject As CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject
 
        ' Load the standard .NET SKD report document object
        boReport = New CrystalDecisions.CrystalReports.Engine.ReportDocument
        boReport.Load(Application.StartupPath & "\Report1.rpt")
 
        ' We're going to use InProc RAS to manipulate the report's client document.
        rasClientDocument = boReport.ReportClientDocument
        rasObjectController = rasClientDocument.ReportDefController.ReportObjectController
 
        ' Obtain objects for our target text box and the formula we're going to insert.
        ' The textbox is named t1 and the formula is named f1.  First we loop through text
        ' objects to find the textbox.
        For Each rasReportObject In rasObjectController.GetReportObjectsByKind(CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindText)
            If rasReportObject.Name = "t1" Then
                ' Save this as the "old text box".
                rasOldTextObject = rasReportObject
                Exit For
            End If
        Next
 
        ' We need to create a "text paragraph" with two "paragraph elements".  The first will be our text
        ' and the second will be the result of our formula.  The first is simply a textual element.
        rasParagraphTextElement = New CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement()
        rasParagraphTextElement.Text = "f1="
' Now we add a "field element" that will point to the formula using the text we're used to.
        rasParagraphFormulaElement = New CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphFieldElement()
        rasParagraphFormulaElement.DataSource = "{@f1}"
 
        ' Now add both elements to the paragraph
        rasParagraph = New CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph()
        rasParagraph.ParagraphElements.Add(rasParagraphTextElement)
        rasParagraph.ParagraphElements.Add(rasParagraphFormulaElement)
 
        ' Create a "new" text object that we will update and then use to replace our old one.
        rasTextObject = rasOldTextObject.Clone(True)
        rasTextObject.Paragraphs.RemoveAll()
        rasTextObject.Paragraphs.Add(rasParagraph)
 
        ' Perform the replacement of the old text field with our newly updated one
        rasObjectController.Modify(rasOldTextObject, rasTextObject)
 
        ' Now view the resulting report
        Me.CrystalReportViewer1.ReportSource = boReport

See Also

How to Use The RAS SDK .NET With In-Process RAS Server

Keywords

Forum thread , KBA , BI-DEV-NET-SDK , .NET SDK / COM SDK , How To

Product

Crystal Reports 2008 V0 ; Crystal Reports 2008 V1 ; SAP Crystal Reports 2011 ; SAP Crystal Reports XI ; SAP Crystal Reports XI R2 ; SAP Crystal Reports, developer version for Microsoft Visual Studio