Results 1 to 11 of 11

Thread: Passing a Variable as a Parameter Issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    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:
    1. Dim cryRpt As New ReportDocument()
    2. Dim reportPath As String = "ReportPath\ReportFile.rpt"
    3. Dim testInt As Integer
    4. testInt = TextBox1.Text
    5.  
    6. cryRpt.Load(reportPath)
    7. cryRpt.SetParameterValue("InspKey", 123456)
    8. CrystalReportViewer1.ReportSource = cryRpt
    9. CrystalReportViewer1.Refresh()
    10.  
    11. Try
    12. Dim CrExportOptions As ExportOptions
    13. Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
    14. Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
    15. CrDiskFileDestinationOptions.DiskFileName = "OutputPath\123456.pdf"
    16. CrExportOptions = cryRpt.ExportOptions
    17.  
    18. With CrExportOptions
    19.     .ExportDestinationType = ExportDestinationType.DiskFile
    20.     .ExportFormatType = ExportFormatType.PortableDocFormat
    21.     .DestinationOptions = CrDiskFileDestinationOptions
    22.     .FormatOptions = CrFormatTypeOptions
    23. End With
    24.  
    25. cryRpt.Export()
    26.  
    27. Catch ex As Exception
    28.     MsgBox(ex.ToString)
    29. 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:
    1. Dim cryRpt As New ReportDocument()
    2. Dim reportPath As String = "ReportPath\ReportFile.rpt"
    3. Dim testInt As Integer
    4. testInt = TextBox1.Text
    5.  
    6. cryRpt.Load(reportPath)
    7. cryRpt.SetParameterValue("InspKey", testInt)     'Changes here
    8. CrystalReportViewer1.ReportSource = cryRpt
    9. CrystalReportViewer1.Refresh()
    10.  
    11. Try
    12. Dim CrExportOptions As ExportOptions
    13. Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
    14. Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
    15. CrDiskFileDestinationOptions.DiskFileName = "OutputPath\" & testInt & ".pdf"     'Changes here
    16. CrExportOptions = cryRpt.ExportOptions
    17.  
    18. With CrExportOptions
    19.     .ExportDestinationType = ExportDestinationType.DiskFile
    20.     .ExportFormatType = ExportFormatType.PortableDocFormat
    21.     .DestinationOptions = CrDiskFileDestinationOptions
    22.     .FormatOptions = CrFormatTypeOptions
    23. End With
    24.  
    25. cryRpt.Export()
    26.  
    27. Catch ex As Exception
    28.     MsgBox(ex.ToString)
    29. 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

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    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"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    Re: Passing a Variable as a Parameter Issue

    Quote Originally Posted by wes4dbt View Post
    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!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    Re: Passing a Variable as a Parameter Issue

    Quote Originally Posted by Hack View Post
    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.

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    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.

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    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

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    31

    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?

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    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
  •  



Click Here to Expand Forum to Full Width