Senin, 02 November 2009

Mengubah Setting Connection Crystal Report pada VB.Net

Peringatan : Menggunakan .Net Framework 2.0 & belum pernah dites... :p
Sumber : http://social.msdn.microsoft.com/Forums/en-US/vscrystalreports/thread/7345fc6c-9749-42a3-b866-509c57f3cc35

Program VB .Net yang menggunakan Crystal Report (CR) yang dibuat di komputer lokal akan menemukan kendala saat program tersebut didistribusikan (diinstall dikomputer yang menggunakan database yang berbeda) karena ConnectionString yang dipergunakan pasti berbeda. Berikut source code untuk mengatasi masalah tersebut.

Mengganti CR ConnectionString pada VB
Note : Struktur tabel harus sama!!!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myReport As New worldsales_northwind() '<-- lihat source code di bawah CrystalReportViewer1.ReportSource = myReport myReport.Load() Dim myDBConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo() With myDBConnectionInfo .ServerName = “localhost” .DatabaseName = “Northwind” .UserID = “sa” .Password = “” End With End Sub

Mengganti source melalui VB tanpa tergantung pada CR

Dim crtableLogoninfos As New TableLogOnInfos()
Dim crtableLogoninfo As New TableLogOnInfo()
Dim crConnectionInfo As New ConnectionInfo()
Dim CrTables As Tables
Dim CrTable As Table

'Declare ReportDocument object and load your existing report
'Make sure that you give the correct path for the document else it will give exception
Dim crReportDocument As New ReportDocument()
crReportDocument.Load("C:\MyApp\Crystal\Crystal\WorldSalesReport.rpt")
CrystalReportViewer1.ReportSource = crReportDocument

CrTables = crReportDocument.Database.Tables

Dim crLoc As String
crLoc = txtDb.ToString & ".dbo"

For Each CrTable In CrTables

crtableLogoninfo = CrTable.LogOnInfo
'Read MachineName\InstanceName,Database details from User interface
'and load them into crConnectionInfo object
crConnectionInfo.ServerName = txtMac.Text
crConnectionInfo.DatabaseName = txtDb.Text
crConnectionInfo.IntegratedSecurity = True
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
CrTable.Location.Substring(CrTable.Location.LastIndexOf(".") + 1)

Next

crReportDocument.ReportOptions.EnableSaveDataWithReport = False
'Refresh the ReportViewer Object
CrystalReportViewer1.RefreshReport()
'Bind the ReportDocument to ReportViewer Object
CrystalReportViewer1.ReportSource = crReportDocument

Dari Sumber lain : http://vb.net-informations.com/crystal-report/vb.net_crystal_report_load_dynamically.htm

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
Dim crtableLogoninfos As New TableLogOnInfos
Dim crtableLogoninfo As New TableLogOnInfo
Dim crConnectionInfo As New ConnectionInfo
Dim CrTables As Tables
Dim CrTable As Table

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

With crConnectionInfo
.ServerName = "YOUR SERVER NAME"
.DatabaseName = "YOUR DATABASE NAME"
.UserID = "YOUR DATABASE USERNAME"
.Password = "YOUR DATABASE PASSWORD"
End With

CrTables = cryRpt.Database.Tables
For Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo
CrTable.ApplyLogOnInfo(crtableLogoninfo)
Next

CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

Tidak ada komentar: