2008-05-10

Create Report

Create report by vb.net and crystal report

1. Create new visual basic project.

2. Add button and CrystalReportViewer to form1.



















3. Add new DataSet item.

4. Add DataTable in dataset design page and add data table columns.
- Title, FirstName, LastName, Address



























5. Add Crystal Report item by select report document as a blank report




































6. On Field Explorer right click at Database Fields and select Database Expert.

7. Select Project Data -> ADO.NET DataSets and add customer table to ReportDataset.




















8. Design Report (Add report header name txtHeader and report footer name txtFooter).






9. Sample code to show report.

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
' Get data to dataset
Dim CustomerData As DataSet = CreateDataSet()

' Create report instance and set data to report data source
Dim rpt As New CustomerReport
rpt.SetDataSource(CustomerData.Tables(0))

' Access crystal report object
' Sample to access textbox object

Dim txtHeader As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtHeader")
txtHeader.Text = "This is Report Header"

Dim txtFooter As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtFooter")
txtFooter.Text = "This is Report Footer"

' Show Report
Me.CrystalReportViewer1.ReportSource = rpt

End Sub

Private Function CreateDataSet() As DataSet
Dim ds As New DataSet("ReportDataset")

' Create Table
Dim dt As New DataTable("Customer")
dt.Columns.Add("Title", Type.GetType("System.String"))
dt.Columns.Add("FirstName", Type.GetType("System.String"))
dt.Columns.Add("LastName", Type.GetType("System.String"))
dt.Columns.Add("Address", Type.GetType("System.String"))

' Add DataTable to DataSet
ds.Tables.Add(dt)

' Create new row
Dim dr As DataRow = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Tom"
dr("LastName") = "Brown"
dr("Address") = "Tom Address"
ds.Tables(0).Rows.Add(dr)

dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "John"
dr("LastName") = "Grary"
dr("Address") = "John Address"
ds.Tables(0).Rows.Add(dr)

dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Alex"
dr("LastName") = "Smith"
dr("Address") = "Alex Address"
ds.Tables(0).Rows.Add(dr)

Return ds
End Function


10. Show Report.




2008-05-05

BackgroundWorker

BackgroundWorker Class

Executes an operation on a separate thread.

Sample Code

1.Create event DoWork.

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Sample how to disable button send before send email method after that enabled button.
DisableSendButton()
' to do SendEmail method
EnabledSendButton()

End Sub

2. Show how to invoke method when use BackgroundWorker control.

Private Sub DisableSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(DisableSendButton))
Else
btnSend.Enabled = False
btnCancelSend.Visible = True
End If
End Sub

Private Sub EnabledSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(EnabledSendButton))
Else
btnSend.Enabled = True
btnCancelSend.Visible = False
End If
End Sub

3.Start BackgroundWorker

BackgroundWorker1.RunWorkerAsync()