on 09-24-2008 6:02 PM
I'm using VS 2008 and VB 2008 with the bundled CR.
I want to be able to show data from an application setting string in a text box in the report header.
Is this possible? If so, can it be done in the report itself, or do I have to code it into the parent form?
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was afraid that would be the answer.
I'm not sure I know how to apply that code to my situation, though. I am dynamically loading the reports into my viewer and I can't seem to use the same code as in the example (I'm not sure if my using VB instead of C is causign this)>
Here's what I've come up with so far:
Following the reportrefresh event-
Dim strTest As CrystalDecisions.CrystalReports.Engine.TextObject
For Each Field As CrystalDecisions.CrystalReports.Engine.ReportDocument In Me.CrystalDecisions.CrystalReports.Engine.********
If Field.Name = "txtTest" Then
strTest = Field.ReportDefinition.ReportObjects("txtTest")
strTest.Text = "Testing Testing"
End If
Next
The issue I'm having is in the IN half of the FOR statement I think. What should I be citing in the IN statement here?
Could someone help me modify this code?
Edited by: Will Smith on Sep 24, 2008 10:04 PM
I'm not sure where you got that code from but it isn't right.
- In your For Each loop you are declaring your Field object as a ReportDocument. Your ReportDocument is the actual Crystal Report so you definitely don't want to do that.
- I'm not sure what you are trying to loop through either Me.CrystalDecisions.CrystalReports ?
- You are on the right track in your If statement though
Here is a quick sample. Note this assumes the TextObject is the only, or first item in your object collection. You'll need more robust code to work in your situation.
Dim strNewText As String
Dim crTextObject As CrystalDecisions.CrystalReports.Engine.TextObject
crReportDocument = New ReportDocument()
crReportDocument.Load("C:\path to my report\myreport.rpt")
' change the text of the object
strNewText = "New text here"
crTextObject = CType(crReportDocument.ReportDefinition.ReportObjects.Item(0), TextObject)
' set the text of the text object on the report.
crTextObject.Text = strNewText
HI, David,
Thanks for the response, I know I'm heading in the right direction now. I have two follow-up questions, at this point.
First, should I insert this code prior to loading the report into the report viewer, or after?
Second, I think I need to import something, but I'm not sure what. The reason I say that is because I'm getting some strange behavior from the IDE when I write the code provided. The Intellisense is not autocorrecting any of the code as I type, but it also isn't giving me any errors. When I run the program the code doesn't cause any exceptions, but it also doesn't change the text in the text object, either.
Any ideas?
Thanks very much for your help!
-Will
I didn't have two of the imports you suggested (.Windows.Forms and .ReportSource), and that solved the Intellisense issues. Also, I moved the code to fire after the report is loaded in the report viewer.
Unfortunately, I still haven't had any luck getting the text field to change. Could this have something to do with how I'm loading the file?
When I load the file into the viewer, I have to add a prefix to the file path ("rassdk://") and I don't undertsand what that prefix is all about. However, when I load the file into crReportDocument I have to use the actual file path- minus the prefix- or I get an exception when I load the report.
Will,
I think there is a better approach to your issue that will simplify the solution. You can use a Crystal Report parameter to add values from your configuration file to your report. I recommend you add a Crystal Report parameter to you report for each value you have from your configuration file. You can then use the SetParameterValue() method to add that value to your report at runtime.
I also recommend you start with a very basic report and get it to display successfully before you try anything else. Use a report with no database connection and one TextBox. Make sure that works. Add your database and make sure that works. Then you can add the code to pass your parameter.
Please review this posting for some great resources on the Crystal Reports .NET SDK.
Unfortunately this leaves me with the same problem. I am dynamically loading a report based on the user's selection of a file name. For some reason, I can't access the report object in the manner that I'm selecting the report. Every example I see shows the code creating a new object based on the report, but I can't seem to accomplish that. (Actually, that's why I was looping through the controls in my earlier post- it was the only way I was able to create any type of object that would let me access the text property of the text fields).
I think the SetParameterValue will work, but I also think I will be able to set the text fields direwctly, if I can only find a way to access the report object.
Let me post my entire code from the "Select Report" button click event:
Private Sub btnSelectReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectReport.Click
If Me.txtReportName.Text = "" Then
MessageBox.Show("Please select a report file before proceeding.")
Exit Sub
End If
strFilename = "rassdk://" + Me.txtReportName.Text
Me.ReportFile.FileName = strFilename
'This was my first attempt at just changing the text object's text
Dim crReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim strFieldName As String = "Testing Testing"
crReportDocument.Load(Me.txtReportName.Text)
crTextObjectFieldName = CType(crReportDocument.ReportDefinition.ReportObjects.Item("txtFieldName"), CrystalDecisions.CrystalReports.Engine.TextObject)
crTextObjectFieldName.Text = strFieldName
'For clarity I removed the code for loading the datasets and assigning the tables to the report
'This was my second attempt at modifying the parameter value
Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = strFileName 'Also doesn't work if I use Me.txtReportName.Text
Report.SetParameterValue("parFieldName", "Testing Testing")
Try
Me.CrystalReportViewer1.ReportSource = Me.ReportFile
Catch ex As Exception
MessageBox.Show(String.Format("{0}{1}This may not be a valid report file", ex.Message, Environment.NewLine), "Invalid Report", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
End Sub
I really think my issue is in how I'm referencing the report in the CrystalReportViewer, but I'm not familiar enough with the Viewer or the ReportDocument objects to know for sure.
Well Dave is abosolutely correct, parameters are the way to go, they are REALLY easy to set. Looking at your code in the first set you use
Dim crReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
crReportDocument.Load(Me.txtReportName.Text)
Like the sample Dave's code sample. However in your parameter code you use:
Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = strFileName
'Also doesn't work if I use Me.txtReportName.Text
Report.SetParameterValue("parFieldName", "Testing Testing")
Try
Me.CrystalReportViewer1.ReportSource = Me.ReportFile
This code is, well lacking. The parameter setting is correct if "parFieldName" is the name of the parameter. however you are trying to set a reportdocument variable to a string variable. You are then setting the viewer to Me.ReportFile instead of the Report variable you attempted to open.
Open the report the way you do in the first sample and it should work. Otherwise take a look at these samples
https://smpdl.sap-ag.de/~sapidp/012002523100006252822008E/net_win_smpl.exe
However some of the samples use the more complex method of setting parameters then Dave showed you.
Trevor
Okay, that did it. I'm able to modify the parameter fields using my application settings- and you're right, it is really easy.
I have a follow-up question now. How can I confirm that a parameter value does or does not exist in the report before trying to set the parameter object value? I ask because I would like to have some flexibility in what parameter fields I add to the report and what I leave out.
Thanks!
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.