Thursday, July 13, 2006

Dynamic Crystal Reports

This is something I experienced a long time ago but had to post it here for people that need it.

Working with the datareport in VB 6.0 was fun. Most of my coleagues would connect their reports to the dataenvironment before they could display records in the datareport. I had a way of doing it without a dataenvironment and I would be able to display a subset of records in a given table. That I enjoyed alot.

While moving to VB.Net, I discovered that the datareport was no more there. We have crystal report in place of that. I tried applying my tricks but it did not work. Then I discovered this:

I'll create a command (say cmdReport) with select statement (Select ... From myTable where ...)and a reportviewer (Say RView) at design time. I create a report file (myReportFile) that's linked to a dataset table (myDataset.myReportTable).

and finally do the following in my coding environment.

Public DRp As New myReportFile()
Public DA As OleDbDataAdapter

Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
Try
cmdReport.Parameters(0).Value = dtFrom.Value.Date
DA = New OleDbDataAdapter(cmdReport)
myDataset.myReportTable.Clear()
Conn.Open()
DA.Fill(myDataset.myReportTable)
Conn.Close()
If myDataset.myReportTable.Rows.Count > 0 Then
DRp.SetDataSource(myDataset.myReportTable)
DRp.Refresh()
RView.ReportSource = DRp
Else
RView.ReportSource = Nothing
MsgBox("No Record found!", MsgBoxStyle.Information, "Searching...")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Conn.Close
End Try
End Sub

Walah! It bring back the joy I felt with VB 6 and datareport.

0 Comments:

Post a Comment

<< Home