Click here to Skip to main content
15,881,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I don't know if the Subject is correct my Query?!...

I have created 21 crystal reports naming them GR01_rpt, GR02_rpt, GR03_rpt.... GR021_rpt. Each of the 21 reports has its own format.

to lessen the long coding, i created a Function,

VB
Public Function A01Control(ByVal txndate As String, ByVal curr_cd As String)


      Dim rpt As New GR01_rpt < -- how can i make this variable string
      'GR01_rpt is a Crystal Report
      'GR01_ds is a DataSet for GR01_rpt

      What I wanted to do is, <----------------------
      Dim rpt As New xRpt
      wherein xRpt can be either GR01_rpt, GR02_rpt GR03_rpt to GR21_rpt, Depending    
      on What Report the User Processes.

      Also, How can i do it in the DataSet <------------------

      Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
      Dim myDS As New GR01_ds()

      da.Fill(myDS, myRPT)
      rpt.SetDataSource(myDS)
      ReportMain.CrystalReportViewer2.ReportSource = rpt

      ReportMain.CrystalReportViewer2.Refresh()

  End Function



Hope that my query is right??
Posted

You can store crystal report files (*.rpt ) in program directory . On file (rpt) properties in solution explorer, set "Build action" - 'None', "Copy to Output Directory" - 'Copy if newer'.

The code to load report is:
VB
dim rptFileName as string="GR01_rpt.rpt"
Dim rpt as new CrystalDecisions.CrystalReports.Engine.ReportDocument 
rpt.Load(rptFileName)

Hope it will help.
 
Share this answer
 
v2
Comments
Alan Tuscano 2-Nov-11 20:42pm    
Alan Tuscano - 10 secs ago
Hi and Good Morning Raimis9 & RaisKazi,

I wanted to paste the screen shot of the error but donno how.. :)

i did your code but got Error:
CrystalReportsException was unhandled by user code.
-- Load Failed.



My rpt Files is inside a folder of my solution, does it make effect?
Alan Tuscano 3-Nov-11 2:04am    
Hi again Raimis9 & RaisKazi,

I have found the error, what i did is add the directory(folder map) of my .rpt...

Thank you so much..

Now, my other problem is also in the initial post.
working with Dataset,

Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
Dim myDS As New GR01_ds() <------ this is for GR01_rpt report,

what i want to do is,
Dim myDS As New Var_Ds()
wherein Var_Ds will represent any DataSet that is for a specific report,
GR01_ds is for GR01_rpt Report,
Gr02_ds is for GR02_rpt Report,
Gr03_ds is for GR03_ rpt Report,
up to GR23_ds

If User selects GR-07 Report,
Var_Ds will be GR07_ds,

my code will then be like,
Dim da As SqlDataAdapter = New SqlDataAdapter(comm)
Dim myDS As New Var_ds()



da.Fill(myDS, myRPT)
rpt.SetDataSource(myDS)
ReportMain.CrystalReportViewer2.ReportSource = rpt

ReportMain.CrystalReportViewer2.Refresh()


Hope that my Explaination can be understood..
Hi Alan,
you can use different sqls for each report's dataadapter instead of using different dataset name, for example:

VB
...
Dim da As SqlDataAdapter = New SqlDataAdapter(comm, sql(rptfileName))
Dim myDs as New DataSet()
da.Fill(myDS, myRPT)
rpt.SetDataSource(myDS) 

ReportMain.CrystalReportViewer2.ReportSource = rpt 
ReportMain.CrystalReportViewer2.Refresh()
...

Private Function sql(ByVal reportFile As String) As String

        Select Case reportFile
            Case "GR01_rpt.rpt"
                Return "SELECT * FROM Table1"
            Case "GR02_rpt.rpt"
                Return "SELECT * FROM Table2"

        End Select

    End Function
 
Share this answer
 
Comments
Alan Tuscano 3-Nov-11 3:02am    
Hi Raimis9,

Thank you Again for helping me with my first query! :)

with my second Question, Actually sir, I already did that... What i wanted to do is to eliminate the use of "Select Case" as in reality, I have 45 Crystal Reports to do.. in which that 41 reports already has a DS, my Boss ask me to make some changes especially with the Long Procedure i did...
huhuhuh... after typing lots of it... he wants me lessen the coding.

and its really my choice as its the one i know.
Alan, I found required code I used to load form by it's name . Hope it will help


Imports System.Reflection
...

dim ds as Dataset
ds=CreateDataset("GR01_ds")
...

 Public Function CreateDataset(ByVal datasetName As String) As DataSet
        Return DirectCast(CreateObjectInstance(datasetName), DataSet)
    End Function
    Public Function CreateObjectInstance(ByVal objectName As String) As Object
        ' Creates and returns an instance of any object in the assembly by its type name.

        Dim obj As Object

        Try
            If objectName.LastIndexOf(".") = -1 Then
                'Appends the root namespace if not specified.
                objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
            End If

            obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

        Catch ex As Exception
            obj = Nothing
        End Try
        Return obj

    End Function
 
Share this answer
 
Comments
Alan Tuscano 3-Nov-11 4:25am    
Hi Sir,

I tried, but in Function CreateObjectInstance

obj returns "nothing"
Raimis9 3-Nov-11 4:37am    
Where the datasets are stored, in the projects root or special folder?
Alan Tuscano 3-Nov-11 4:46am    
inside a folder named Dataset
Raimis9 3-Nov-11 4:54am    
I created test project and it works...
May be exception occurred, use debugger for the function to see.
Alan Tuscano 3-Nov-11 5:13am    
i created a Dataset in the project's root,
did your code,

but inside this function
Public Function CreateObjectInstance(ByVal objectName As String) As Object
' Creates and returns an instance of any object in the assembly by its type name.

Dim obj As Object

Try
If objectName.LastIndexOf(".") = -1 Then
'Appends the root namespace if not specified.
objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
End If

obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
---- > after this where object returns "Nothing"

Catch ex As Exception
obj = Nothing
End Try
Return obj

End Function

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900