Link to home
Start Free TrialLog in
Avatar of rohini_pb29
rohini_pb29

asked on

assign parameters in crystal report VB.net

how do i use date parameters in crystal report VB.net ? i am new to this so i need step by step instructions .
kindly assist
Avatar of frodoman
frodoman
Flag of United States of America image

To pass a date value to a date parameter:

1) Add a report to your project.

2) Add a date parameter to the report. Call it "DateParam".

3) Add the following lines of code to the top of the code page to import the relevant namespaces:


'Import namespaces in general declarations
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

4) Add the following code to pass a date parameter to a Report at runtime using VB.net

Dim crReport As New CrystalReport1()

'ParameterFieldDefinitions and ParameterFieldDefinition are off the
'CrystalDecisions.CrystalReports.Engine Namespace
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition

'ParameterValues and ParameterDiscreteValue are off the CrystalDecisions.Shared
'namespace
Dim crParameterValues As New ParameterValues()
Dim crParameterDiscreteValue As New ParameterDiscreteValue()

'Set Date Parameter

'Set discrete value
crParameterDiscreteValue.Value = CDate("8/19/2005")

'Access first parameter field definition
crParameterFieldDefinitions = crReport.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("DateParam")

' Add parameter value
crParameterValues = crParameterFieldDefinition.CurrentValues

crParameterValues.Add(crParameterDiscreteValue)

' Apply the current value to the parameter definition
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

5) Pass the report object to the WinForm or WebForm viewer and run the application

--------------------------------------------------------------------------------

[ Above copied from: http://support.businessobjects.com/library/kbase/articles/c2010247.asp ]

HTH

frodoman
Avatar of rohini_pb29
rohini_pb29

ASKER

in the selection expert i have given the formula as
  {A.NAM}='Ro' and
{B.DATE} >={?DateParam}

and under page_load function after connection to the database code , i have pasted the code given by u.
here is the error i got ...

Server Error in '/test1' Application.
--------------------------------------------------------------------------------

Missing parameter field current value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: Missing parameter field current value.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[ParameterFieldCurrentValueException: Missing parameter field current value.]
   .F(String       , EngineExceptionErrorID 
) +574
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalSetReportInfo(RequestContext reqContext) +1293
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalGetViewContext(ReportPageRequestContext reqContext, * viewContext) +217
   CrystalDecisions.CrystalReports.Engine.FormatEngine.GetPage(PageRequestContext reqContext) +280
   CrystalDecisions.ReportSource.LocalReportSourceBase.GetPage(PageRequestContext pageReqContext) +189
   CrystalDecisions.Web.ReportAgent.u(Boolean N) +164
   CrystalDecisions.Web.CrystalReportViewer.OnPreRender(EventArgs e) +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +62
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Page.ProcessRequestMain() +1489

 ------------------------------------
there is another line of code in the page_load fn. given as

CrystalReportViewer1.ReportSource = crpt
CrystalReportViewer1.ReportSource = Server.MapPath("all_benj.rpt")

crystalreport1 in the report name generated by VS.net and i have added all_benj. rpt which has date  parameter.
should this b crystalreport1.rpt or all_benj? i tried both but the error  remained the same.

any clue ?
Thanx,
Can you paste the code that you put in (copied from here) - I suspect something minor prior to this line:

   ' Add parameter value
   crParameterValues = crParameterFieldDefinition.CurrentValues

frodoman
this is webform1.aspx.vb

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared


Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim crReport As New CrystalReport1

    'ParameterFieldDefinitions and ParameterFieldDefinition are off the
    'CrystalDecisions.CrystalReports.Engine Namespace
    Dim crParameterFieldDefinitions As ParameterFieldDefinitions
    Dim crParameterFieldDefinition As ParameterFieldDefinition

    'ParameterValues and ParameterDiscreteValue are off the CrystalDecisions.Shared
    'namespace
    Dim crParameterValues As New ParameterValues
    Dim crParameterDiscreteValue As New ParameterDiscreteValue


#Region " Web Form Designer Generated Code "
    Dim crpt As CrystalReport1
    Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
    Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents CrystalReportViewer1 As CrystalDecisions.Web.CrystalReportViewer

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        crpt = New CrystalReport1

        For Each myTable In crpt.Database.Tables
            myLogin = myTable.LogOnInfo
            myLogin.ConnectionInfo.Password = "abcdef"
            myLogin.ConnectionInfo.UserID = "usedId"
            myTable.ApplyLogOnInfo(myLogin)
        Next

        CrystalReportViewer1.ReportSource = crpt

        CrystalReportViewer1.ReportSource = Server.MapPath("all_benj.rpt")

        Dim crReport As New CrystalReport1

       
        'Set Date Parameter

        'Set discrete value
        crParameterDiscreteValue.Value = CDate("8/04/2004")

        'Access first parameter field definition
        crParameterFieldDefinitions = crReport.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("DateParam")

        ' Add parameter value
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Add(crParameterDiscreteValue)

        ' Apply the current value to the parameter definition
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

    End Sub

End Class

thanx,
At the beginning of your page load section you have:  

   crpt = New CrystalReport1

And then you apply login info and set the viewer to use crpt.

Then you have:

   Dim crReport As New CrystalReport1

And from this point on you use crReport to set your parameter values.


So as far as I can tell it looks like you're doing half the work to the crpt object and half the work to the crReport object.  Use one or the other throughout and I think you'll be okay.

frodoman
i did as  u said.


Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared


Public Class WebForm1
    Inherits System.Web.UI.Page
    ' Dim crReport As New CrystalReport1

    'ParameterFieldDefinitions and ParameterFieldDefinition are off the
    'CrystalDecisions.CrystalReports.Engine Namespace
    Dim crParameterFieldDefinitions As ParameterFieldDefinitions
    Dim crParameterFieldDefinition As ParameterFieldDefinition

    'ParameterValues and ParameterDiscreteValue are off the CrystalDecisions.Shared
    'namespace
    Dim crParameterValues As New ParameterValues
    Dim crParameterDiscreteValue As New ParameterDiscreteValue


#Region " Web Form Designer Generated Code "
    Dim crpt As CrystalReport1
    Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
    Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents CrystalReportViewer1 As CrystalDecisions.Web.CrystalReportViewer

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        crpt = New CrystalReport1
        ' Dim crReport As New CrystalReport1
        For Each myTable In crpt.Database.Tables
            myLogin = myTable.LogOnInfo
            myLogin.ConnectionInfo.Password = "abcdef"
            myLogin.ConnectionInfo.UserID = "usedId"
            myTable.ApplyLogOnInfo(myLogin)
        Next

        CrystalReportViewer1.ReportSource = crpt

        CrystalReportViewer1.ReportSource = Server.MapPath("all_benj.rpt")

        'Set Date Parameter

        'Set discrete value
        crParameterDiscreteValue.Value = CDate("8/04/2004")

        'Access first parameter field definition
        crParameterFieldDefinitions = crpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = crParameterFieldDefinitions.Item("DateParam")

        ' Add parameter value
        crParameterValues = crParameterFieldDefinition.CurrentValues

        crParameterValues.Add(crParameterDiscreteValue)

        ' Apply the current value to the parameter definition
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

    End Sub

End Class


but again :(
here is the error

Server Error in '/test1' Application.
--------------------------------------------------------------------------------

Missing parameter field current value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: Missing parameter field current value.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[ParameterFieldCurrentValueException: Missing parameter field current value.]
   .F(String       , EngineExceptionErrorID 
) +574
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalSetReportInfo(RequestContext reqContext) +1293
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalGetViewContext(ReportPageRequestContext reqContext, * viewContext) +217
   CrystalDecisions.CrystalReports.Engine.FormatEngine.GetPage(PageRequestContext reqContext) +280
   CrystalDecisions.ReportSource.LocalReportSourceBase.GetPage(PageRequestContext pageReqContext) +189
   CrystalDecisions.Web.ReportAgent.u(Boolean N) +164
   CrystalDecisions.Web.CrystalReportViewer.OnPreRender(EventArgs e) +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +62
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Page.ProcessRequestMain() +1489

 

hope there is a solution soon .... i am left with only today...
thanx,
Is your CR parameter a Date or a DateTime?  If it's DateTime you'll need to include a time in your CDate() function.
i am sorry if this sounds like a silly qiestion...
i gave
crParameterDiscreteValue.Value = CDate("8/04/2004 00:00:00")

but i got error:
Missing parameter field current value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: Missing parameter field current value.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

as server error.


but i had one question.
in database the date is in the form of  date time.
but in the report (with out .net  just run locally) it just takes date.like 08/03/2004
in parameter i changed it to date time , (it was date before when i used to run the same report locally without.net, it works fine.)


making parameter as date time i tried crParameterDiscreteValue.Value = CDate("8/04/2004 00:00:00")
and
crParameterDiscreteValue.Value = CDate("8/04/2004") both  gave the same error


thanx,
could there be any problem with the permissions given for the browser ???

i could open a normal report with out having a parameter in the browser though....

thanx,
Do you have subreports in this report?  I'm thinking that this error may be because there's a parameter in a subreport that isn't being set.  If this is possible look at subreports and make sure any parameters are linked to main report or you'll have to pass in those parameters as well.

If no subreports then I'll keep digging.  I don't think this has anything to do w/ the browser.

frodoman
yes there is a subreport.

i am sorry for not giving the full detail.

in subreport also there is same date but in my report it goes this way

{A.ACC_NUM} = {?Pm-A.ACC_NUM}
and
{B.DATE} >= {?Pm-?Start Date}


and in the main report it is this way as i told u before

{A.NAM}='Ro' and
{B.DATE} >={?DateParam}

should i just rename start Date as DateParam ?

thanx, u r giving me hopes!! and i am sorry again...
Good news - I think we're on the right track...

Okay, in your main report you have {?DateParam} and this is the value that must be passed to the subreport, right?  Is {?StartDate} the parameter in your subreport?  If so then you should be linked to {?StartDate} not to {?pm-StartDate}...

Best way to do this (in my humble opinion) is in your subreport create a parameter named "SubDateParam" (I always append a 'sub' prefix to the same parameter name - helps me keep it straight).  Rt-click the subreport and 'change links'.  In the "Container Report Fields to Link To" box you should have {?DateParam} and in the "Subreport Parameter field to use" you should have {?SubDateParam}.

Let me know if that does it.

frodoman
no it dint work,

i just made a quick report which has no subreport
in the main report select expert it has a simple formula
{B.DATE} >={?DateParam}

it works fine with out .net environment.
so i added this report as myname.rpt

dint work !!!

gave server error as :

Server Error in '/test1' Application.
--------------------------------------------------------------------------------

Missing parameter field current value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: CrystalDecisions.CrystalReports.Engine.ParameterFieldCurrentValueException: Missing parameter field current value.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[ParameterFieldCurrentValueException: Missing parameter field current value.]
   .F(String       , EngineExceptionErrorID 
) +574
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalSetReportInfo(RequestContext reqContext) +1293
   CrystalDecisions.CrystalReports.Engine.FormatEngine.internalGetViewContext(ReportPageRequestContext reqContext, * viewContext) +217
   CrystalDecisions.CrystalReports.Engine.FormatEngine.GetPage(PageRequestContext reqContext) +280
   CrystalDecisions.ReportSource.LocalReportSourceBase.GetPage(PageRequestContext pageReqContext) +189
   CrystalDecisions.Web.ReportAgent.u(Boolean N) +164
   CrystalDecisions.Web.CrystalReportViewer.OnPreRender(EventArgs e) +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +62
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Control.PreRenderRecursiveInternal() +125
   System.Web.UI.Page.ProcessRequestMain() +1489
------------------------------------------------

i  want to know y this is not working so i took the previous report which is complex and has subreport.
i expected this simple report to work.

i have 2 question...

CDate("8/04/2004") '  is different from CDate ("2004,08,01") ?
i dont think so but asking just to clarify.

DateParam parameter field i gave it as Date Time .

and

CrystalReportViewer1.ReportSource = Server.MapPath("CrystalReport1.rpt")

should it be myname.rpt or CrystalReport1.rpt  and what is the  difference ?

and what do u think is the problem ??
now how ever long it takes i need to find out the problem.. so plz help me...
???
 i am trying and trying and tryin...
no use all the time i see the  same error :(

u tooo ????
hi,
any luck from your side ?


Sorry rohini - I'm in the middle of something here at work and haven't been able to look at this again.  I'll try to get to it tonight if I can. - frodoman
ASKER CERTIFIED SOLUTION
Avatar of frodoman
frodoman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial