locked
Crystal report in vb.net RRS feed

  • Question

  • Hi All,

    I am in the process of building a windows form with vb.net 2008 and would like to add crystal reports to it but in a way that i dont have to keep re compiling the code every time i add a new report for a client i would like to simply add the report to a folder within the main application start up folder called reports and then when the system loads it adds the report name to the submenu button called reports and then when clicked brings up the standard parameter input form and then display the report.

    i have one thought of doing it is by adding the report name and paramater name to an access database and then have the winform load the values from the database on loading and when the relevant report button is pressed.

    but there is a way that the system can do it all for you by creating an non-embeded report and simply adding this to the report folder then when loaded it will show in the report (i know this cos i have seen it done in booking system i used to use but never found out how) all i was told was they use a report generator and this then converts the report in to 3 files;

    • .rpt = the design and layout
    • .sql = the sql statement
    • .par = parametar name, the location where the button sits and the name to be displayed

    these are then placed in to the folder and the system picks them up and stores the details accordingly.

    i have been searching for months and still never found the answer or clue. i can add a embeded report and non-embeded report and have it display based on a parameter just the way of adding the report to the system dynamicly.

    any help or pokes in the right direction would be greatly appriciated.

    Regards

    Aidan

    Wednesday, June 1, 2011 4:25 PM

Answers

  • Hi Bpell,

    Thank you so much for your response back and useful information, I like the thought of the first one as I think this would be easier for a client to just add the .rpt file in and then away they go. Is there a way you could control where the report cmd button would be add on the menu bar or would you have to just put every one under reports through code.

    Do you have any sample code of this if not no problems will figure it out.

    Thank you so much again.

    Regards

    Aidan

     

    • Marked as answer by Aidanb79 Friday, June 3, 2011 7:47 AM
    Thursday, June 2, 2011 7:43 AM

All replies

  • You have a few options.

    1. You can scan the report directory and loop through all of the .rpt files. Then, you can load each one using the Crystal Report objects and read in it's parameters and then dynamically display them on a form. I've done this before, it's not that much code. You'll want to dynamically lay out form fields and then map them to the parameters.
    2. You can create some form of metadata like you mentioned. This can be an XML file, a database, whatever.

    I've gone both routes. With option #2 I've included additional things in the metadata like the actualy control to use when laying it out (so if it were on an ASP.NET web form I could make the control a TextBox and then attach specific JavaScript to it).

    After you load the ReportDocument, there is a property called "ParameterFields" that you can loop through to see all of the parameter fields in a report. This doesn't finish the solution, but it gives you an idea of how to work with the ParameterFields:

     

        ''' <summary>
        ''' Checks to see if a parameter name exists in the reports parameter fields. This only checks the top level of
        ''' the report. The top level should propagate down any parameters that need to be passed down.
        ''' </summary>
        ''' <param name="paramName"></param>
        ''' <param name="report"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function DoesParameterExist(ByVal paramName As String, ByVal report As ReportDocument) As Boolean
    
          If report Is Nothing Or report.ParameterFields Is Nothing Then
            Return False
          End If
    
          For Each param As ParameterField In report.ParameterFields
            If paramName = param.Name Then
              Return True
            End If
          Next
    
          Return False
    
        End Function
    

     

    • Proposed as answer by Mike Feng Friday, June 3, 2011 5:39 AM
    Wednesday, June 1, 2011 6:21 PM
  • Hi Bpell,

    Thank you so much for your response back and useful information, I like the thought of the first one as I think this would be easier for a client to just add the .rpt file in and then away they go. Is there a way you could control where the report cmd button would be add on the menu bar or would you have to just put every one under reports through code.

    Do you have any sample code of this if not no problems will figure it out.

    Thank you so much again.

    Regards

    Aidan

     

    • Marked as answer by Aidanb79 Friday, June 3, 2011 7:47 AM
    Thursday, June 2, 2011 7:43 AM
  • When I've done it, I've read the reports in from a directory somewhere and then added them into a some sort of list box (which would vary depending on VB vs. ASP.NET).  Then, when the user clicks on a list item, I open the report, get the ParameterFields and lay them out on a form (and actually, now that remembering, I used ASP.NET when I did this).  The idea would be the same though.  Loop through the ParameterFields, create dynamic form elements (name them with the parameterName so they're easy to map back and forth) and place them on your form.  Then you can have a generic execute button that will map your form elements back to the parameter values.  This way, all you'd have to do is drop a report in a directory for it to be picked up and used.

    Since you know the parameter type (date, string, integer) from the ReportParameters you can put validation on your dynamic elements.  The beauty is, once you do it once, it will work for that type for any report. 

    Friday, June 3, 2011 7:37 PM
  • Hi Bpell,

    I can figure out the directory part and adding to the menu but i am unsure on the passing the paremters to the vb.net form from the .rpt and google will only show passing the parameters from vb.net form to .rpt how would this be done if you have any code or what the best way to search for the way to do it i would be ever so greatfull, its just one of those things that does your head in.

    regards

    Aidan

    Monday, June 6, 2011 10:08 AM