CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    6

    [RESOLVED] How to programmatically set data source for Crystal Reports basic for VS 2008?

    I have created and tested my reports on the development pc and it works fine. For these reports, I have used the database expert and set my desired database and tables etc. I use OLEDB and Access .mdb file as my database.

    Now that I want to deploy my application, I am getting a login failure in the client machine. I want the reports to connect to the database I am packing in the setup. I want something like the Application.StartUpPath so that the reports will extract data from the database no matter where the user installs the application.

    Please help!

  2. #2
    Join Date
    Jul 2005
    Posts
    1,083

    Re: How to programmatically set data source for Crystal Reports basic for VS 2008?

    Study this general information about VB.NET & Crystal Reports , it could be helpful
    -------------------------------------------------------------------------------------------------------

    'To pass logon information to a Crystal Report at runtime

    Code:
    Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.Shared
    
    Dim crtableLogoninfos As New TableLogOnInfos()
    Dim crtableLogoninfo As New TableLogOnInfo()
    Dim crConnectionInfo As New ConnectionInfo()
    Dim CrTables As Tables
    Dim CrTable As Table
    Dim TableCounter
    'If you are using a Strongly Typed report (Imported in your project) named CrystalReport1.rpt use the following:
    Code:
    Dim crReportDocument As New CrystalReport1()
    'If you are using a Non-Typed report, and loading a report outside of the project, use the following:
    Code:
    Dim crReportDocument As New ReportDocument()
    
    crReportDocument.Load("c:\myReports\myReport.rpt")
    'Set the ConnectionInfo properties for logging on to the Database

    'If you are using ODBC, this should be the DSN name NOT the physical server name.
    'If you are NOT using ODBC, this should be the physical server name
    Code:
    With crConnectionInfo
        .ServerName = "DSN or Server Name"
    'If you are connecting to Oracle there is no DatabaseName. Use an empty string. 
    'For example, .DatabaseName = ""
        .DatabaseName = "DatabaseName"
        .UserID = "Your User ID"
        .Password = "Your Password"
    End With
    'This code works for both user tables and stored procedures.
    'Set the CrTables to the Tables collection of the report
    Code:
    CrTables = crReportDocument.Database.Tables
    
    'Loop through each table in the report and apply the LogonInfo information
    For Each CrTable in CrTables
        CrTableLogonInfo = CrTable.LogonInfo
        CrTableLogonInfo.ConnectionInfo = crConnectionInfo
        CrTable.ApplyLogOnInfo(crtableLogoninfo)
    
        'If your DatabaseName is changing at runtime, specify the table location.
        'For example, when you are reporting off of a Northwind database on SQL server you should have the following line of code:
        crTable.Location = "Northwind.dbo." & crTable.Location.Substring(crTable.Location.LastIndexOf(".") + )
    Next
    
    
    'Set the viewer to the report object to be previewed.
    CrystalReportViewer1.ReportSource = crReportDocument
    ----------------------------------------------
    Also it may help that you visit --->
    https://boc.sdn.sap.com/taxonomy/term/3
    ----------------------------------------------


    If you are changing database at runtime, it is important that you specify the table location after you apply logon information (this is a case sensitive property).
    You can either specify the table name only or the fully qualified table name such as
    crTable.location = "databaseName.dbo.tablename"

    Refer to knowledge base article c2010275 if you wish to change database logon information in the main and subreport.
    If you are reporting off an Access database, then specify either the 'ServerName' or 'DatabaseName' to the 'ConnectionInfo' Object depending on how you are connecting to Access.
    For example, if you are connecting to Access through ODBC, then set the 'DatabaseName' for the 'ConnectionInfo' object as follows:
    Code:
    With crConnectionInfo
        .DatabaseName = "C:\mydatabase\mydata.mdb"
    End With
    If you are connecting to Access through OLE DB, then set set the 'ServerName':
    Code:
    With crConnectionInfo
        .ServerName = "C:\mydatabase\mydata.mdb"
    End With
    It is not possible to report of a secured Access database using a native connection.
    See knowledge base article C2010460 for more information.

    If you are using more than one database with different usernames and passwords, use a loop to pass in the different values.
    Last edited by jggtz; December 12th, 2009 at 12:04 PM.

  3. #3
    Join Date
    Dec 2009
    Posts
    6

    Re: How to programmatically set data source for Crystal Reports basic for VS 2008?

    Thanks a lot for the quick reply ... I will try it out now and post my results... wish me luck ... n thanks again for the detailed description!

  4. #4
    Join Date
    Dec 2009
    Posts
    6

    Re: How to programmatically set data source for Crystal Reports basic for

    I am using the following code:

    Private Sub frmRptInv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim cr As New ReportDocument
    Dim crCon As New ConnectionInfo
    Dim crtableLogoninfos As New TableLogOnInfos
    Dim crtableLogoninfo As New TableLogOnInfo
    Dim CrTables As Tables
    Dim CrTable As Table


    cr.Load(Application.StartupPath & "\Reports\crInv.rpt")

    With crCon
    .ServerName = Application.StartupPath & "\Database\sales_stocks.mdb"
    .UserID = "Admin"
    .Password = "MyPass"
    End With

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

    Dim pfDefs As ParameterFieldDefinitions
    Dim pfDef As ParameterFieldDefinition

    Dim pVal As New ParameterValues
    Dim pDisVal As New ParameterDiscreteValue

    ' Customer Code
    pDisVal.Value = rlong
    pfDefs = cr.DataDefinition.ParameterFields
    pfDef = pfDefs.Item("InvId")
    pVal = pfDef.CurrentValues

    pVal.Clear()
    pVal.Add(pDisVal)

    pfDef.ApplyCurrentValues(pVal)

    ' Load Report to cr viewer
    crv.ReportSource = cr
    crv.Refresh()
    End Sub

    It was working perfectly till I added the password to the .mdb file. Now, I'm getting the LogOn Failed Error. You said something about a secured access database and not being able to report off it. Please explain that! Where are the Knowledge Base Articles you are talking about?

  5. #5
    Join Date
    Dec 2009
    Posts
    6

    Re: [RESOLVED] How to programmatically set data source for Crystal Reports basic for

    I dont know why but just modifying

    crCon.Password = "MyPass"

    with

    crCon.Password = Chr(10) & "MyPass"

    makes the code perfect again!

  6. #6
    Join Date
    Oct 2008
    Posts
    42

    Re: [RESOLVED] How to programmatically set data source for Crystal Reports basic for

    i am uisng the same code but i get error va\lues cannot be Null . i debgubed fr each balue specified, but i get all teh values

Tags for this Thread

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

Featured