Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Replacing text in textbox with parameter

I am attempting to replace text in various textboxes throughout a report in VB.NET. I am able to do all the text replacement I want, but anytime I pull out text with parameters in it, it seems the parameters are re-inserted as text as opposed to a dynamic parameter value.

For example, if I have a text box with the text "Message 1 - {?MyParam}" and replace it with the text "Message 2 - {?MyParam}", I will always get the literal text of {?MyParam} instead of the parameter's actual value.

I'm using Crystal Report 11.5 r2 and Visual Studio .NET 2003. This is a windows, VB.NET application.

Is there some special text I need to insert in order for the text box to act like it contains a parameter?

Thanks,

Chuck

replied

There is actually a way you can pull this off, but it does get pretty complex. You can use InProcess RAS if you are using Crystal Reports XI R2 SP3 or higher. This allows you to get into the Report Creation API (RC API) and manipulate the report at runtime.

The TextObjects we use are quite complex. They allow you to apply individual formatting to each letter, plus you can add database, parameter or formula fields to the TextObject. The next object contains a Paragraphs collection. This collection of Paragraphs will then contain ParagraphElements.

These ParagraphElements are what you actually need to manipulate. These could be simple text or fields from your field explorer.

Check out the sample below and the attached diagram for further clarification.

Note: There seems to be an issue when working with some parameter types. You may get the error "Object or reference not set to an instance of an object" if you try and add a Number, Date, Date Time, or Currency parameter. I am still investigating this issue.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ConfigureCrystalReports();
        }

        CrystalReportViewer1.ReportSource = Session["Report"];

    }

    protected void ConfigureCrystalReports()
    {
        ISCDReportClientDocument boReportClientDocument;        
        CrystalDecisions.ReportAppServer.Controllers.ReportObjectController boReportObjectController;
        CrystalDecisions.ReportAppServer.ReportDefModel.TextObject boOldTextObject, boNewTextObject;
        CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph boParagraph;
        CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphFieldElement boParagraphFieldElement;
        CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement boParagraphTextElement;
        
        boReportDocument = new ReportDocument();
        boReportDocument.Load(Server.MapPath("CrystalReport.rpt"));

        boReportClientDocument = boReportDocument.ReportClientDocument;        
        boReportObjectController = boReportClientDocument.ReportDefController.ReportObjectController;
               

        // Get a handle on the ReportObjectController so we can manipulate the TextObject        
        foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject boReportObject in boReportObjectController.GetAllReportObjects())
        {
            if (boReportObject.Kind == CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindText)
            {
                boOldTextObject = (CrystalDecisions.ReportAppServer.ReportDefModel.TextObject)boReportObject;
                boNewTextObject = (CrystalDecisions.ReportAppServer.ReportDefModel.TextObject)boOldTextObject.Clone(true);
                
                // Clear out all paragraphs from the current text object

                boNewTextObject.Paragraphs.RemoveAll();

                // Create a new Paragraph to add to our TextObject
                boParagraph = new CrystalDecisions.ReportAppServer.ReportDefModel.Paragraph();

                // Create a new ParagraphTextElement to be added to our paragraph
                
                boParagraphTextElement = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphTextElement();                
                boParagraphTextElement.Text = "The value of my parameter is: ";
                boParagraph.ParagraphElements.Add(boParagraphTextElement);                

                // Create a new ParagraphFieldElement (our parameter field) to be added to our paragraph                
                boParagraphFieldElement = new CrystalDecisions.ReportAppServer.ReportDefModel.ParagraphFieldElement();
                boParagraphFieldElement.Kind = CrystalDecisions.ReportAppServer.ReportDefModel.CrParagraphElementKindEnum.crParagraphElementKindField;
                boParagraphFieldElement.DataSource = "{?StringParam}";                

                boParagraph.ParagraphElements.Add(boParagraphFieldElement);
                
                boNewTextObject.Paragraphs.Add(boParagraph);
                
                boReportObjectController.Modify(boOldTextObject, boNewTextObject);

                break;
            }
        }

        Session.Add("Report", boReportDocument);
    }

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question