Setting Connection for Crystal Report Programatically in VB.Net

Abstract :

This code shows how to Set Connection for Crystal Report Programaticaly

Include the namespaces

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Here is the code :

'Create an Instance of the report
Dim myReport As ReportDocument = New ReportDocument
Dim chkstate As Boolean
chkstate = setConnection("My_ConnectionString", myReport)
If chkstate = False Then
MsgBox("Empty Connection String", MsgBoxStyle.Critical)
End If
Public Function setConnectionCrystal(ByVal ConnectionString As String, ByVal myRep As ReportDocument)
Dim settings As ConnectionStringSettings
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()
settings = ConfigurationManager.ConnectionStrings(ConnectionString)
If settings Is Nothing Then
Return (False)
End If
Dim TestArray() As String = Split(settings.ConnectionString, ";")
Dim TA() As String
TA = Split(TestArray(0), "=")
myConnectionInfo.ServerName = TA(1)
TA = Split(TestArray(1), "=")
myConnectionInfo.DatabaseName = TA(1)
TA = Split(TestArray(2), "=")
myConnectionInfo.UserID = TA(1)
TA = Split(TestArray(3), "=")
myConnectionInfo.Password = TA(1)
Dim myTables As Tables = myRep.Database.Tables
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
myTableLogonInfo.ConnectionInfo = myConnectionInfo
Return (True)
End Function

Thanks & Regards,


Guest Author: kell02 Oct 2013

where did you get the connectionStringSettings type?

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: