on 09-22-2008 8:44 PM
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
Hi Chuck,
Can you paste in your code. If you are passing the parameter name as is then we would interpret it as normal test.
It should be something like "start of text" + ToString(ParameterName) + " End of text"
Thank you
Don
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The line of code looks like this:
CType(oReport.ReportDefinition.ReportObjects(i), TextObject).Text = "Some Text {?MyParam}"
The name of the report parameter is "MyParam". Will I have to instantiate the equivalent parameter as an object and pull that name from that? It's not really convenient in the context of the app, so if there's a special format I have to use, I'd like to directly put it in the string.
The string data is taken literally when assigning a new value to the Text property of a TextBox. This is default behavior. I would suggest you use a formula instead.
- Create a formula in your report and drag and drop it in the position where you want your text to show up.
- Then you can edit the formula at runtime through the DataDefinition
Do you know of any documentation on text box data being interpreted literally when added through the .NET API? The more I've tested it, I certainly believe you, but I'd like to have it documented for work.
I'm in a situation where we're going to be dynamically translating hundreds of reports, so I won't be able to manually go in each one and replace textboxes containing the parameters. However, going by your suggestion, I may try hiding textboxes containing parameters, then creating a new formula in the location/size of the textbox that displays the text. A little hairy, but it should be workable if formulas parse parameters as I would expect.
Thanks for the help,
Chuck
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);
}
User | Count |
---|---|
89 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.