in

ASP.NET Weblogs

Gregory Rubinstein

  • Passing Parameter to Crystal Report

    Recently, I was developing a pretty sophisticated Crystal Report that included multiple sub-reports and used multiple stored procedures as data sources. All of those stored procedures accepted the same parameter, which made my life a little easier, since I only had to deal with one parameter that I needed to pass to my Crystal Report.

    In order for all sub-reports to be able to recognize the parameter, which I am passing to the main report, I had to link all of my sub-reports to the main report using that parameter.

    In order to link a sub-report to the main report, the following actions should be performed:

    To link a subreport to the data in the primary report

    1. If you are creating a new subreport or importing an existing report as a subreport, from the Insert menu, click Subreport. Choose or create a report and click the Link tab.

      - or -

      If you have already placed a subreport in the primary report, but did not create a link at setup, navigate to the Subreport Links dialog box by choosing Subreport Links from the Edit menu.

      The Subreport Links dialog box appears.

    2. Choose the subreport you want to link from the For subreport list (if it is not already selected).
    3. Select the field you want used as a link field in the primary (containing) report from the Available Fields list.
    4. Click the > arrow.

      The field is added to the "Field(s) to link to" list box, and is now selected as a link field.

    5. Repeat steps 3 and 4 for each additional link, as desired.
    6. Use the Field link section (which will only appear if you have selected a link field) to set up the link for each link field:
      • Select the field you want linked to the primary report from the "Subreport parameter field to use."
      • Select the "Select data in subreport based on field" check box on and select a field from the adjacent drop-down list to organize the subreport data based on a specific field (this is the quick equivalent of using the Select Expert). If nothing is specified here, the subreport will adopt the organization of the primary report.
    7. Click OK.

    When you run the report, the program will coordinate the data in the primary report with the data in the subreport.

    Below is the code that I used to pass my parameter to the Crystal Report and then display it in PDF format to the end-user:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    GenerateReport(Session("EmpId"))
    End Sub

    Private Sub GenerateReport(ByVal EmpId As Integer)
    Dim theReport As New ReportDocument
    theReport.FileName = "C:\myProject\Reports\myReport.rpt"
    theReport.SetDatabaseLogon(PCON.APP.UserName, PCON.APP.Pass, PCON.APP.db_Server, PCON.APP.dbName)

    'Set the required parameter for Crystal Report
    theReport.SetParameterValue("@Id", EmpId)

    'Now, present report in PDF format
    theReport.ExportToHttpResponse ( ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")
    End Sub
  • Crystal Reports with ASP.NET

    When it comes to developing a report for an ASP.NET application, one might find that there is very little information available on how it's done using Crystal Reports.

    Visual Studio 2003 comes with a built-in version of Crystal Reports (CR 9). Using that built-in version of Crystal Reports might save you some money, since it's a part of VS 2003, however, from the Crystal Reports design prospective it is much more convenient to use a stand-alone version of Crystal Reports.

    The latest version of Crystal Reports at the time of writing this post is Crystal Reports XI, which provides some new features, not available in previous versions (see the www.businessobjects.com website for more info), so in my last development I used that version of Crystal Reports.

    In order for the report to display as a webpage, VS 2003 provides CrystalReportViewer control. This is how it works:

    Instantiate the DataSet object

    Dim ds As New DataSet

    ds = GetDataForReport() 'some method that retrieves data for the report

    Instantiate the ReportDocument object:

    Dim rpt As New ReportDocument

    'set the ReportObject as the report you want to display
    rpt = New CrystalReport1
    'Define the datasource of your report
    rpt.SetDataSource(ds)

    'define your report as a reportsource for the crystalreportviewer
    CrystalReportViewer1.ReportSource = rpt

    Below is C# version of this code:

    DataSet ds = new DataSet();
    ReportDocument rpt = new ReportDocument();
    rpt = new CrystalReport1();
    rpt.SetDataSource(ds);
    CrystalReportViewer1.ReportSource = rpt;


    In order for this code to work, the following namespaces must be imported/used:

    CrystalDecisions.CrystalReports.Engine
    CrystalDecisions.Shared
    CrystalDecisions.Web.Design

    The following namespaces should be used only if you are planning to convert the report into a PDF, Word, or Excel format.

    CrystalDecisions.Shared.ExportDestinationType
    CrystalDecisions.Shared.ExportFormatType
    CrystalDecisions.Shared.ExportDestinationOptions

  • Converting ASP.NET to PDF

     I probably wouldn't be wrong if I said that most developers every now and then are tasked to present specific data contained in a webpage as a PDF document. There are two ways to do it:

    1. using the functionality of CrystalReportViewer's toolbar - this toolbar provides a button, which does exactly that. However, to use this approach you would have to actually display the crystal report on the page.

    2. programmatically convert the Crystal Report object into a PDF document using the Crystal Reports API

    Below is the code for the second approach:

    Dim report As CrystalReport1 = New CrystalReport1
    SqlDataAdapter1.Fill(DataSet11)
    report.SetDataSource(DataSet11)
    report.ExportToHttpResponse (ExportFormatType.PortableDocFormat, Response, False, "ExportedReport")
  • Remaining Characters Counter

    Recently I was developing a webform (asp.net) where the user could submit his feedback, and one of the requirements was that the user should not enter more than a set number of characters in his message, so I thought it would be real nice for the end user to know how many more characters he still can type until the maximum limit is reached.

    In order to allow that functionality, I created a asp.net multiline textbox for text input and an html readonly text field (for displaying number of characters until the maximum).

    Then, I created a Javascript function and called it every time the contents of the multiline textbox changed (onKeyUp and onChange events).

    Below is the code:

    <html>
    <head>

    <script language="JavaScript">

    function CountChars(text,long)
    {
    var maxlength = new Number(long);
    var myLength = text.value.length;
    document.forms[0].Counter.value = maxlength - myLength;
    if (myLength > maxlength) {
    text.value = text.value.substring(0,maxlength);
    }
    }
    </script>

    </head>
    <body>

    <form id="Form1" runat="server">

    <asp:Textbox mode="multiline" id="txtMessage" runat="server" /><br />

    <input type="text" name="Counter" readonly="readonly" />

    </form>

    </body>
    </html>

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Util.SetTextBoxProperties(txtMessage, True)
    End Sub

    Below is the SetTextBoxProperties method, which actually did the work:

    Public Sub SetTextBoxProperties(ByVal txt As TextBox)
        txt.Attributes.Add("onKeyUp", "CountChars(this," & txt.MaxLength & ")")
        txt.Attributes.Add("onChange", "CountChars(this," & txt.MaxLength & ")")

    End Sub
  • Active Control Marker

    Today, while working on my project, I recalled that some of the data entry pages in my project have a lot of textboxes, which may confuse the end-user once he starts entering information. My idea was to make the textbox that has focus visually different from the rest, so I came up with the following code snippet, that does the actual work:

    Public Sub ShowActiveTextbox(ByVal txt As TextBox)

        txt.Attributes.Add("onfocus", "this.className='class1'")
        txt.Attributes.Add("onblur", "this.className='class2'")

    End Sub

    This code would work just fine if I had only one or two Textboxes on my page. However, some of the pages may contain a dozen of those textboxes, so I came up with another useful method:

    Public Sub ShowActiveTextbox(ByVal col As ControlCollection)

        For Each Ctl As Control In col

           If Ctl.GetType().Name = "Textbox" Then

               Call ShowActiveTextbox(Ctl)

            Else

                'Handle TextBoxes contained in container controls, such as panel
                If
    ctl.Controls.Count > 0 Then

                   Call ShowActiveTextBox(ctl.Controls)

                End If

           End If

        Next
     End Sub

     Now we have a method that will handle any number of controls on a specific page and will apply the onFocus and onBlur event handlers to all Textboxes on that page.

More Posts