ASP.Net Runtime Data Crystal Change Report

Got a script to run Crystal report through ASP.Net, export and send via email.

If I use the current database without logging in, everything works, but if I change the data source (the same database structure, but a different server) at runtime, then the problem is lower.

Crystal 2008 Runtime

ERROR: System.Runtime.InteropServices.COMException (0x80042018): table% 1 does not exist in the document. in CrystalDecisions.ReportAppServer.Controllers.DatabaseControllerClass.VerifyTableConnectivity (Object Table) in CrystalDecisions.CrystalReports.Engine.Table.TestConnectivity () at ScriptCodeClass.ApplyLogon (ReportDocument cr, ConnectionInfo ci) in ScriptConDocument ScriptLodeCodeDocument ScriptLocDoclassDocument Server , String id, String pass) in ScriptCodeClass.FunCreatePDFView (String lsHeader, String lsReportType, String msDatabaseUserId, String msDatabasePassword)

this code can change authentication, but not the data source / server, wondering if a link is needed or for import.

Imports System.Collections
Imports System.Data
Imports T1.Tb.Data
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports T1.Tb
Imports T1.TB.Public
Imports CrystalDecisions.CrystalReports.Engine.ReportDocument
Imports CrystalDecisions.ReportSource
Imports System.Configuration
Imports System.Data.SqlClient


Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared


References CrystalDecisions.CrystalReports.Engine
References CrystalDecisions.Shared
References System.Web.Services
References System.Data
References T1.Tb.dll
References T1.TB.Public
References T1.P1.dll
References T1.P1.Public
References T1.Tb.Fun


public shared function Logon(cr as ReportDocument, server as string, db as string, id as string, pass as string)  as Boolean
      'Use this to change the database logon info for a crystal report
      dim ci as ConnectionInfo = new ConnectionInfo()
      dim subObj as SubreportObject


      ci.ServerName = server
      ci.DatabaseName = db
      ci.UserID = id
      ci.Password = pass


      if ApplyLogon(cr, ci) then
        for each obj as ReportObject in cr.ReportDefinition.ReportObjects
           If (obj.Kind = ReportObjectKind.SubreportObject) Then
        //  if typeof obj.Kind.GetType() is CrystalDecisions.Shared.ReportObjectKind then
            subObj = ctype(obj, SubreportObject)
            if  not ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci) then
               return(false)
            end if
          end if
        next
        Logon = True
      end if
    end function


    private shared function ApplyLogon(cr as ReportDocument, ci as ConnectionInfo ) as Boolean


      dim li as TableLogOnInfo
      dim success as Boolean


      for each tbl as Table in cr.Database.Tables
        li = tbl.LogOnInfo
        li.ConnectionInfo = ci
        tbl.ApplyLogOnInfo(li)
        'check if logon was successful
        'if TestConnectivity returns false, check logon credentials
        if tbl.TestConnectivity() then
          'drop fully qualified table location
          if tbl.Location.IndexOf(".") > 0 then
            tbl.Location = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1)
          else
            tbl.Location = tbl.Location     'THIS IS LINE LEFT OUT IN ALL SAMPLES I SAW
          end if
        else
          success = false
          exit for
        end if
        success = True
      next
    end function
+6
source share
2 answers

, , .

: 1) . 2) 3) /

+1

: () web.config :

  <add key="ServerName" value=""/> Name Or IP Address
  <add key="DataBaseName" value=""/> Database Name
  <add key="DatabaseUser" value=""/>User Name
  <add key="DatabasePassword" value=""/>Password

:

    Dim SERVER_NAME As String = ConfigurationManager.AppSettings("ServerName").ToString()
    Dim DATABASE_NAME As String = ConfigurationManager.AppSettings("DataBaseName").ToString()
    Dim DatabaseUser As String = ConfigurationManager.AppSettings("DatabaseUser").ToString()
    Dim DatabasePassword As String = ConfigurationManager.AppSettings("DatabasePassword").ToString()

DB

   CrystalReportViewer.SetDatabaseLogon(DatabaseUser, DatabasePassword, SERVER_NAME, DATABASE_NAME)

:

CrystalReportViewer.SetDataSource

:

    CrystalReportViewer.SetDatabaseLogon("sa", "123", "Your_Server", "YourDB")
+1

Source: https://habr.com/ru/post/1015780/


All Articles