Tuesday, August 5, 2008

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

Saturday, March 22, 2008

Character 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.SetTextBoxPropertiesFull(txtMessage, True)
End Sub

and then, in the SetTextBoxPropertiesFull method I had the following two lines of code that did the work:

txt.Attributes.Add("onKeyUp", "CountChars(this," & txt.MaxLength & ")")
txt.Attributes.Add("onChange", "CountChars(this," & txt.MaxLength & ")")



Another handy code snippet is as follows:

txt.Attributes.Add("onfocus", "this.className='myStyle1'")
txt.Attributes.Add("onblur", "this.className='myStyle2'")


where myStyle1 and myStyle2 are names of css classes defined in the stylesheet.