Saturday, March 10, 2012

Creating a Report with Crystal Reports VB Net






This tutorial will help you to create a report with crystal report, we create ADO OLE DB as a connection string, to create a simple report with crystal report using  ADO OLE DB Connection string, follow the instructions, first we use sqlserver as a database and crystal report as our report. add your crystal report with right click on your current project properties > add  > new items then click and select reporting choose cystal report then ok.

Create crystal report a
and will show crystal reports gallery, choose "as a blank report" then ok, to select the connections string properties, right click the database fields in the field explorer located in your project left design and choose database expert.

Create crystal report b

microsoft provides many more available connections string, to hook up your sql server database with oledb(ado), create new connection > ole db (ado)  > make new connection > microsoft oledb provide for sql server.


create crystal report c

then click next to continue will show you to manage the connection string, fill the server name with your server connection, check the integrated securty if you connect using localhost, select the database that you want to select in the database field then click finish, ok now we have a connection string with provided connection by sql server, and we going to choose our table for crystal report design, choose the server name "192.168.1.1/sqlexpress"  > database > dbo > select your table by clicking the arrow, then click ok.


create crystal report d

now we have our current table,to put the field from the table in your crystal report design,choose the table, select field by using "drag and drop" you will get more tools design with right click in your report or from the toolbox on bottom left.
Cystral report design

ok now we take a look "windows form" in our project if you dont have it one right click on the current project, create with "add new" > "add windows form", create the same as the picture below by adding two datetimepicker,a button and a crystalreportviewer double click your button insert the code below.

Code:
        Dim Conn As New SqlClient.SqlConnection
        Dim Da As New SqlClient.SqlDataAdapter
        Dim ds As New DataSet
        Dim strConnection As String
        Dim SQL As New SqlClient.SqlCommand
        strConnection = "Data Source=192.168.1.1\SQLEXPRESS;Initial Catalog=Contoh; 
        User=admin; Pwd=1"
        Conn = New SqlClient.SqlConnection(strConnection)
        Conn.Open()
        SQL.Connection = Conn
        SQL.CommandText = "Select * From grafik Where tanggal between'" & 
        Format(DateTimePicker1.Value, "MM/dd/yyyy") & "' and '" &   
        Format(DateTimePicker2.Value, "MM/dd/yyyy") & "'order by Nomor asc "
        Da.SelectCommand = SQL
        Da.Fill(ds, "grafik")
        Dim a As New report 'your crystal report
        Dim b As New Reportform
        a.SetDataSource(ds.Tables!grafik) 'binding your cristal report to the datasource
        b.GroupBox1.Visible = False
        b.CrystalReportViewer1.Visible = True
        b.CrystalReportViewer1.ReportSource = a
  b.Refresh()
        b.WindowState = FormWindowState.Maximized
        b.Show()


form design

from the code I ask of the code to select between two date from the data in the table, the database relates with this article Insert,Save,Update dan Delete pada VB net

crystal report viewer
hope this article will help you.

sourcecode


1 comment: