Dim dtAdapter As New SqlDataAdapter
Dim ds As New DataSet
Conn = New SqlConnection
com = New SqlCommand
Conn.ConnectionString = DBConnString.strConn
If txtSearch.Text.Trim() = "" Then Exit Sub
Dim sqlSearch As String = "SELECT * FROM ComInfo WHERE DepID=@DepID ORDER BY AssetID"
MsgBox(sqlSearch)
With com
.Parameters.Clear()
.Parameters.Add("@DepID", SqlDbType.NVarChar).Value = txtSearch.Text.Trim()
.Connection = Conn
.CommandText = sqlSearch
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = com
dtAdapter.Fill(ds, "dtComInfo")
dt = ds.Tables(0)
dtAdapter = Nothing
Conn.Close()
Conn = Nothing
Dim rpt As New ReportDocument()
Dim directory As String = Application.StartupPath
rpt.Load(directory & "\reportComInfo.rpt")
rpt.SetDataSource(dt)
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
ตอนใช้งาน
Code (VB.NET)
Dim rpt As New ReportDocument()
Dim directory As String = Application.StartupPath
rpt.Load(directory & "\reportComInfo.rpt")
rpt.SetDataSource(GetTableOLEDB("SELECT * FROM ComInfo WHERE DepID like %'" & txtSearch.Text.Trim() &"'% ORDER BY AssetID",ConnString....))
Me.CrystalReportViewer1.ReportSource = rpt
Me.CrystalReportViewer1.Refresh()