Results 1 to 6 of 6

Thread: [RESOLVED] How to connect MSSQL and CR 8.5 programmatically?

  1. #1

    Thread Starter
    Member jeruelx5d's Avatar
    Join Date
    Jun 2006
    Posts
    39

    Resolved [RESOLVED] How to connect MSSQL and CR 8.5 programmatically?

    Guys!

    I'm using MSSQL2000, VB6 and Crystal Report 8.5

    My application is working fine. I'm able to produce report from MSSQL down to CR 8.5. I'm using "Crystal Report Contro"l to access .rpt within my VB6 application.

    The problem started when we are using several MSSQL server. since all my .rpt is configured only to one server, i would have to encounter some report errors e.g. "cannot connect specified server".

    Please help me to connect Crystal Report to SQL Server programmatically using VB6. I think i could help me a lot to resolve this issue.

    Thanks a lot.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: How to connect MSSQL and CR 8.5 programmatically?

    Note: The Crystal Report Control is outdated and is only included for backward compatibility. Think about switching to the Crystal Report Viewer Control.

    To change the datasource at runtime use the Connect or LogOnInfo properties or the LogonServer method.

  3. #3
    Member
    Join Date
    Feb 2006
    Posts
    51

    Re: How to connect MSSQL and CR 8.5 programmatically?

    As above use the Report Viewer Control.
    After setting the server details at runtime I use the code below (.NET but you should get the idea) to connect.

    VB Code:
    1. Dim crtableLogoninfos As New TableLogOnInfos
    2.         Dim crtableLogoninfo As New TableLogOnInfo
    3.         Dim crConnectioninfo As New ConnectionInfo
    4.         Dim crTables As Tables
    5.         Dim crTable As Table
    6.         Dim Environ As String
    7.         Dim crReportDocument As New ReportDocument
    8.         Dim subReportDocument As New ReportDocument
    9.         Dim crSection As Section
    10.         Dim crSections As Sections
    11.         Dim crReportobject As ReportObject
    12.         Dim crReportobjects As ReportObjects
    13.         Dim crSubReportobject As SubreportObject
    14.         Dim crDatabase As Database
    15.        
    16. With crConnectioninfo
    17.             .ServerName = strServerOrDSNName
    18.             .DatabaseName = strDBNameOrPath
    19.             .UserID = strUserID
    20.             .Password = strPassword
    21.         End With
    22.  
    23.         Try
    24.             'Loop through each table in the report and set the connection info
    25.             crTables = crReportDocument.Database.Tables
    26.             For Each crTable In crTables
    27.                 crtableLogoninfo = crTable.LogOnInfo
    28.                 crtableLogoninfo.ConnectionInfo = crConnectioninfo
    29.                 crTable.ApplyLogOnInfo(crtableLogoninfo)
    30.             Next
    31.         Catch ex As Exception
    32.             MsgBox("Unable to Log On To report Tables", MsgBoxStyle.Critical)
    33.             MsgBox("Loading of " & strReportName & " Cancelled.", MsgBoxStyle.Information)
    34.  
    35.             'MsgBox(ex.ToString)
    36.  
    37.             GoTo StopHere
    38.         End Try
    39.  
    40.         Try
    41.             ' Loop through each section and find report objects
    42.             crSections = crReportDocument.ReportDefinition.Sections
    43.             For Each crSection In crSections
    44.                 crReportobjects = crSection.ReportObjects
    45.                 For Each crReportobject In crReportobjects
    46.                     If crReportobject.Kind = ReportObjectKind.SubreportObject Then
    47.                         ' If a subreport is found cast as subreportobject
    48.                         crSubReportobject = CType(crReportobject, SubreportObject)
    49.                         ' Open the sub report
    50.                         subReportDocument = crSubReportobject.OpenSubreport(crSubReportobject.SubreportName)
    51.                         crDatabase = subReportDocument.Database
    52.                         crTables = crDatabase.Tables
    53.                         ' Loop through each table in the sub report and set the connection info
    54.                         For Each crTable In crTables
    55.                             crtableLogoninfo = crTable.LogOnInfo
    56.                             crtableLogoninfo.ConnectionInfo = crConnectioninfo
    57.                             crTable.ApplyLogOnInfo(crtableLogoninfo)
    58.                         Next
    59.                     End If
    60.                 Next
    61.             Next
    62.         Catch ex As Exception
    63.             MsgBox("Unable to Log On To Sub Report Tables")
    64.             ' MsgBox(ex.ToString)
    65.  
    66.         End Try

  4. #4

    Thread Starter
    Member jeruelx5d's Avatar
    Join Date
    Jun 2006
    Posts
    39

    Re: How to connect MSSQL and CR 8.5 programmatically?

    Thanks a lot guys!


    I'm now working with CR Viewer. Thanks a lot for your advice and sample code. It really helps me a lot!


    More power!


    God Bless!


    jeruelx5d

  5. #5
    Member
    Join Date
    Feb 2006
    Posts
    51

    Re: How to connect MSSQL and CR 8.5 programmatically?

    Have found that there is an issue connecting in this manner. Each time a user opened a new report a new process was opened on the server which remained until the user closed the application. This could mean that a user would have upwards of 20 processes running at anyone time as the app is used intensively.

    The solution I found was to put the following

    VB Code:
    1. crReportDocument.Dispose()

    in the close event of the form which had the viewer embedded.

  6. #6
    Addicted Member jeanette_db's Avatar
    Join Date
    Oct 2005
    Location
    DC, Phil
    Posts
    215

    Re: How to connect MSSQL and CR 8.5 programmatically?

    aja! =)
    I learnt to plant my own garden instead of
    waiting for someone to bring me flowers
    visit my blog...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width