Imports System.Data.OleDb'For sqlserver'Imports System.Data.SqlClient'CrystalDecisions.CrystalReports.Engine is a dll which is used for the creation of reports.Imports CrystalDecisions.CrystalReports.EnginePublic Class Reportcode Dim con As OleDbConnection 'dim con As SqlConnection (For sqlconnection) 'DataTable is used for the storage of data which we get from the database Dim dt As DataTable 'Dataadapter is used to fill the data from the database into datatable Dim da As OleDbDataAdapter 'Reportdocument is bind with the reportviewer to show the report. Dim obj As New ReportDocument 'StringBuilder is class used to build multiple strings and create the sentence. Dim str As New System.Text.StringBuilder Private Sub Reportcode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load str.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") 'AppDomain.CurrentDomain.BaseDirectory will give you path of debug folder in your current solution Str.Append(AppDomain.CurrentDomain.BaseDirectory) str.Append("test.mdb") Str.Append(";Persist Security Info=False") con = New OleDbConnection 'ConnectionString is property used for the connecting with the database. con.ConnectionString = str.ToString 'For sqlconnection 'con.ConnectionString = "workstation id=HOME;packet size=4096;integrated security=SSPI;initial catalog=IMS" & _ ' ";persist security info=False" 'Distinct keywords gives you data which is not duplicated in table. da = New OleDbDataAdapter("select distinct(deptno) from emp", con) dt = New DataTable da.Fill(dt) 'To bind datatable with combobox cmbdept.DataSource = dt 'DisplayMember is used to show the data of particular column cmbdept.DisplayMember = "Deptno" da = Nothing dt = Nothing End Sub Private Sub cmdreport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdreport.Click Dim cmd As New OleDbCommand Dim str1 As String Dim str2 As String dt = New DataTable cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = "Select * from emp where deptno=" & Val(cmbdept.Text) da = New OleDbDataAdapter(cmd) da.Fill(dt) Dim str As String str = AppDomain.CurrentDomain.BaseDirectory str1 = str.Remove(str.IndexOf("bin"), 10) str2 = str1 & "RptCode.rpt" 'MsgBox(str2) 'Str1 contains the path of report. obj.Load(str1 & "RptCode.rpt") 'Binding the table with the reportdocument object obj.Database.Tables(0).SetDataSource(dt) 'Binding the document with the reportviewer control CrystalReportViewer1.ReportSource() = obj End SubEnd Class
<img src="c:\report.jpg">