Sunday, June 12, 2011

Crystal report using sql query search with textbox

 the Project solution --> Add --> New Item --> .rpt File
*It will open the following window. Select the Report wizard

 Specifying the data --> Create new connection --> OLEDB(ADO) --> specify the data provider by following screen
*Now, after creating the .rpt file we need to write the code to fill the data source. pastse this code to the command button


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Try
            connection_open()
            strSQL = "select * from tbl_stud where id = '" & TextBox4.Text & "'"
            ds = New DataSet
            myDA = New OleDbDataAdapter(strSQL, cnSQL)
            myDA.Fill(ds, "MyData")




            'Get the Report Location

            Dim strReportPath As String = "C:\Documents and Settings\joeYap\Desktop\exam\WindowsApplication1\WindowsApplication1\CrystalReport1.rpt"

            'Check file exists

            If Not IO.File.Exists(strReportPath) Then

                Throw (New Exception("Unable to locate report file:" & vbCrLf & strReportPath))

            End If



            'Assign the datasource and set the properties for Report viewer

            Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument



            rptDocument.Load(strReportPath)

            rptDocument.SetDataSource(ds.Tables("MyData"))


            CrystalReportForm.CrystalReportViewer1.ShowRefreshButton = False

            CrystalReportForm.CrystalReportViewer1.ShowCloseButton = False

            CrystalReportForm.CrystalReportViewer1.ShowGroupTreeButton = False

            CrystalReportForm.CrystalReportViewer1.ReportSource = rptDocument
            CrystalReportForm.Show()
        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        Finally
            connection_close()
        End Try

    End Sub

No comments:

Post a Comment