Now the only thing left is to connect this managed report to the data source at runtime and also to the Report viewer.
1.
Open
Form1
code window.
Add a Procedure
ShowReport and PrepareReport Function.
VB.NET
Private
Sub
ShowReport()
Dim
rpt
As
New
CrystalReport1()
'The report you created
Dim
dsReport
As
New
Dataset1()
'The dataset that you created for this report
'We need a report object that is fully populated to display, so declare one
Dim
objReport
As
Object
objReport = prepareReport(rpt, dsReport, "SELECT * FROM Customers")
rpvReport.ReportSource = objReport
End
Sub
'To get a report from Table
Public
Overloads
Function
prepareReport(
ByVal
rpt
As
Object
,
ByVal
dsReport
As
Object
,
ByVal
tblName
As
String
)
As
Object
Dim
myConnection
As
SqlConnection
Dim
MyCommand
As
New
SqlClient.SqlCommand()
Dim
MyDA
As
New
SqlDataAdapter()
Try
myConnection =
New
SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=northwind;")
MyCommand.Connection = myConnection
MyCommand.CommandText = tblName
MyCommand.CommandType = CommandType.Text
MyDA.SelectCommand = MyCommand
MyDA.Fill(dsReport, "Customers")
rpt.SetDataSource (dsReport)
Return
rpt
Catch
ReportErr
As
Exception
MessageBox.Show("Prepare Report Error:" & vbCrLf
& ReportErr.Message, "Report Sample", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
MyDA.Dispose()
MyCommand.Dispose()
myConnection.Dispose()
End
Try
End
Function