|
-
Aug 31st, 2011, 10:08 AM
#1
Thread Starter
Junior Member
Passing a Variable as a Parameter Issue
I'm developing a program in VS02 that displays and exports a CR11 report at a certain screen. It works fine when I hard code the report like this:
vb.net Code:
Dim cryRpt As New ReportDocument()
Dim reportPath As String = "ReportPath\ReportFile.rpt"
Dim testInt As Integer
testInt = TextBox1.Text
cryRpt.Load(reportPath)
cryRpt.SetParameterValue("InspKey", 123456)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
Try
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
CrDiskFileDestinationOptions.DiskFileName = "OutputPath\123456.pdf"
CrExportOptions = cryRpt.ExportOptions
With CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = CrFormatTypeOptions
End With
cryRpt.Export()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
But if I change the parameter to a variable as well as the name of the exported report it will show up with no data:
vb.net Code:
Dim cryRpt As New ReportDocument()
Dim reportPath As String = "ReportPath\ReportFile.rpt"
Dim testInt As Integer
testInt = TextBox1.Text
cryRpt.Load(reportPath)
cryRpt.SetParameterValue("InspKey", testInt) 'Changes here
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
Try
Dim CrExportOptions As ExportOptions
Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
CrDiskFileDestinationOptions.DiskFileName = "OutputPath\" & testInt & ".pdf" 'Changes here
CrExportOptions = cryRpt.ExportOptions
With CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = CrDiskFileDestinationOptions
.FormatOptions = CrFormatTypeOptions
End With
cryRpt.Export()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I know the report is still receiving the variable as I placed the parameter itself on the report for testing. When the report is run/exported the correct parameter shows up fine. I also have an OLE object on the report that references a dynamic picture file (signature of customer is obtained and created into a .jpg). THAT shows up fine as well and is based on the passed parameter of the report! Anyone have an idea as to why this works hard coded but not with a variable? It's the last part of a project that needs to be done but I'm being driven nuts trying to solve this last issue.
Last edited by llDayo; Aug 31st, 2011 at 11:09 AM.
Reason: Changed output path and added comments at targeted code
-
Aug 31st, 2011, 11:46 AM
#2
Re: Passing a Variable as a Parameter Issue
It looks like your assigning a String to "testInt". "TextBox1.Text" is a String. Try,
Code:
testInt =CInt( TextBox1.Text)
and
Code:
"OutputPath\" & Trim(TextBox1.Text) & ".pdf"
-
Aug 31st, 2011, 12:04 PM
#3
Thread Starter
Junior Member
Re: Passing a Variable as a Parameter Issue
 Originally Posted by wes4dbt
It looks like your assigning a String to "testInt". "TextBox1.Text" is a String. Try,
Code:
testInt =CInt( TextBox1.Text)
and
Code:
"OutputPath\" & Trim(TextBox1.Text) & ".pdf"
Thank you, wes, but that is something I had already thought of. I went ahead and just tried what you posted above to be on the safe side but the result is the same. The number that appears in the textbox is actually a global variable of an integer type. I've also tried setting testint to that variable with the same result. Interestingly enough, if I hard code testint with a valid number the report runs just fine. I don't understand how this is even possible if an integer variable can't work!
-
Aug 31st, 2011, 12:11 PM
#4
Re: Passing a Variable as a Parameter Issue
Perhaps testint does not contain what you think it contains.
Send testint to the immediate window (or a message box if you perfer) and see what gets displayed.
-
Aug 31st, 2011, 12:13 PM
#5
Thread Starter
Junior Member
Re: Passing a Variable as a Parameter Issue
I just did a little test by using a formula field that outputs the parameter plus 10000000 directly on the first page of the report just to see the result. It added it up just fine, meaning, it is registering as an integer variable.
-
Aug 31st, 2011, 12:17 PM
#6
Thread Starter
Junior Member
Re: Passing a Variable as a Parameter Issue
 Originally Posted by Hack
Perhaps testint does not contain what you think it contains.
Send testint to the immediate window (or a message box if you perfer) and see what gets displayed.
Remember though, I'm displaying the parameter directly on the report and can see that when it is run. I'm also using the parameter to link* to a .jpg that is being used in an OLE object on the report and that is showing up fine.
*The parameter is a 'key' field within a SQL database. The signature file (.jpg) is named using this key so that it can match up to the correct report.
-
Aug 31st, 2011, 07:47 PM
#7
Re: Passing a Variable as a Parameter Issue
"TextBox1.Text" is a string no matter what.
This line would throw an error if "testInt" was an integer
Code:
"OutputPath\" & testInt & ".pdf"
Maybe try,
Code:
cryRpt.SetParameterValue("InspKey", CInt(TextBox1.Text))
I don't know if VS02 has the Compiler option "Option Strict". If it does then you should turn it on
Last edited by wes4dbt; Aug 31st, 2011 at 07:52 PM.
-
Sep 1st, 2011, 09:24 AM
#8
Thread Starter
Junior Member
Re: Passing a Variable as a Parameter Issue
Turning on Option Strict created a lot of errors to fix. I wasn't even sure what it was for so had to look it up and I'll definitely keep that on in the future. Thanks! It also indeed threw an error when creating the .pdf.
I'm still having an issue in trying to get the report to show up with the data. I tried your suggestion using CInt for the value and the result was the same. I also tried (with Option Strict now turned on) the global variable integer I originally was using. It didn't give me any errors but the report still wouldn't work. The last thing I've tried was changing "testInt" to an Object variable and plugging that in but to no avail.
Any other ideas? I'm totally out at the moment and completely frustrated.
*EDIT*
I've just now tried changing the parameter on the report to a string and using ToNumber in the selection formula. Then I used ToString within the VB code. Still no luck.
Last edited by llDayo; Sep 1st, 2011 at 09:39 AM.
Reason: Tried something else.
-
Sep 1st, 2011, 10:49 AM
#9
Re: Passing a Variable as a Parameter Issue
You say this works,
Code:
cryRpt.SetParameterValue("InspKey", 123456)
This doesn't work,
Code:
TextBox1.Text="123456"
cryRpt.SetParameterValue("InspKey", CInt(TextBox1.Text))
If thats true then maybe try converting TextBox1 to a Double, Cdbl() or to a Decimal, CDec(). What data type is the field in your database?
If that doesn't work then I would start using Google to find an answer. I don't use parameters with my CR reports, I use Typed datasets to load the datasource.
Good Luck
-
Sep 1st, 2011, 04:34 PM
#10
Thread Starter
Junior Member
Re: Passing a Variable as a Parameter Issue
No, neither of those worked either. I have been searching desperately through Google but can't seem to find anyone that's had a similar problem (maybe have to redefine my search parameters).
Do you have an example of doing a report using a typed dataset? I use CR11 developer edition and the report was set up through that. Would I have to recreate the report with a version only available for Visual Studios?
-
Sep 1st, 2011, 08:20 PM
#11
Re: Passing a Variable as a Parameter Issue
There are lots of example on Google of creating the parameter and assigning its value and adding it to the report at runtime. Google "Crystal Reports parameter runtime". You might want to that.
If you want to try using Typed datasets there are alot of examples of that also.
Heres what I do,
1. Add a datasource to the project
2. Add a Tableadapter(with parameters if necessary) to the datasource that will contain the reports data.
3. Add a new crystal report to the project. Use the Database Expert.
4. When it ask you to to select the data, choose "Project Data", then select ADO .Net Datasets, then select the Dataset you created for your report.
5. design your report layout
6. then when you run the report you do something like this,
Code:
Dim ta As New DiversionByInTakeDateTableAdapters.DiverHistTableAdapter
Dim ds As New DiversionByInTakeDate
ta.FillByIntakeDate(ds.DiverHist, CDate(strRptStartDate), CDate(strRptEndDate))
Dim rpt As New rptDiversions
rpt.SetDataSource(ds.Tables("DiverHist"))
Dim objT As TextObject
objT = DirectCast(rpt.Section2.ReportObjects("Title"), CrystalDecisions.CrystalReports.Engine.TextObject)
objT.Text = "Diversions From " & strRptStartDate & " To " & strRptEndDate
Me.CrystalReportViewer1.ReportSource = rpt
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|