如何将crystalreportviewer与数据库连接?

时间:2014-04-23 14:20:27

标签: sql-server vb.net crystal-reports

我有一个sql表名Patient_Detail,我想用水晶报告报告,但是ERROR是Object reference not set to an instance of an object.,在rd.Load("C:\Users.....

Dim dap As SqlDataAdapter
Dim dat As DataTable
Dim bds As BindingSource
Dim cmd As SqlCommand
Dim ds As DataSet
Dim rd As ReportDocument
Private Sub Report_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    cn.Open()
    cmd = New SqlCommand
    dap = New SqlAdapter
    ds = New DataSet
    rs = New ReportDocument
    cmd.Connection = cn
    cmd.CommandText = "SELECT * FROM Patient_Detail"
    dap.SelectCommand = cmd
    dap.Fill(ds, "Patient_Detail")

    rd.Load("C:\Users\Hou Chandara\documents\visual studio 2013\Projects\WindowsApplication1\WindowsApplication1\MyReport.rpt")

    rd.SetDataSource(ds)
    CrystalReportViewer1.ReportSource = rd
    CrystalReportViewer1.RefreshReport()
    cn.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

您需要先设置rd。 rd = new ReportDocument()

或在声明中

Dim rd As New ReportDocument