Dilip  Nagle

Dilip Nagle

  • 1.5k
  • 107
  • 4k

Using a specific .rpt Crystal Report Object at Run time

Nov 19 2018 3:24 AM
The Problem:
 
I have created 5 Crystal Reports crInvoice_xxx.rpt files for 5 different Sales Invoice formats. For Each specific Client, I store the specific name of *.rpt file as say crInvoice_abc.rpt (for say Client ABC) in a database of that client.
 
When user runs my web application (and not the web site as my .rpts are objects in dll) developed in asp.net with vb.net, I access database, pickup name of crInvoice_xxx.rpt file, in above case crInvoice_abc.rpt file and create an object of crystal Report at runime as follows:
 
Dim ocrListReportObject As Object
Dim ocrConnectionInfo As ConnectionInfo
Dim ocrDatabase As Database
Dim ocrTables As CrystalDecisions.CrystalReports.Engine.Tables
Dim ocrTable As CrystalDecisions.CrystalReports.Engine.Table
Dim ocrTableLogOnInfo As TableLogOnInfo
dim strCurRptObjectNm = NSGetReportName() //it returns crInvoice_abc for Client ABC
ocrListReportObject = NSObjectFactory.NSCreateAnObject(strCurRptObjectNm)
ocrConnectionInfo = New ConnectionInfo
ocrConnectionInfo.ServerName = Session("strSessionServerNM") 'Computer Server Name
ocrConnectionInfo.DatabaseName = Session("strSessionDatabaseNM") 'Name of database
ocrConnectionInfo.UserID = Session("strSessionLoginNm") 'Login UserID to use
ocrConnectionInfo.Password = Session("strSessionLoginPassword") 'Login Password
ocrDatabase = ocrListReportObject.Database
 
... and so on. It works well in VB.net
 
But, when I try to do it in C#, it gives an error.
 
Code of NSObjectFactory
Public Class NSObjectFactory
'---------------------------------------------------------*
'Method : To Dynamically Create an Instance of an Object *
' : Useful for dynamically using Crystal Report name*
'Status : 06-Dec-2014 *
'---------------------------------------------------------*
Shared Function NSCreateAnObject(ByVal strInObjectName As String) As Object
'---------------*
'Local variables*
'---------------*
Dim assMyAssembly = [Assembly].GetExecutingAssembly()
Dim typMyType As Type = assMyAssembly.GetType(strInObjectName.Trim)
Dim oMyObject As Object = Nothing
'---------------*
'Create Instance*
'---------------*
Try
oMyObject = Activator.CreateInstance(typMyType)
Catch oEx As TargetInvocationException
oMyObject = Nothing
End Try
Return oMyObject
End Function
 
My Question:
 
How to write such a code in C#.
 
Note: Everything upto Object creation is working in C# code. But, setting Properties such as that of database gives an error.
 
Regards,
Dilip Nagle

Answers (1)