CR 9 for C#.net Question
-
Hello All, I have an ASP.net application that is currently using the push method and displaying reports off of ADO.net datasets. I am struggling with a design method for the following questions: 1. .NET treats each crystal report you add to your project as a class and I am using a several reports and the ReportDocument class to set datasources for each one and so on. Depending on which report my users select in an aspx page i show them the report they want by creating an object of the type of report they selected. I am wondering if I can create say Object myobj = new Object() and depending on what report they select cast the general myobj object into the specific type of the report class the user selected instead of having to instantiate an instance of every type of report within my code? Thanks for your help:confused: Frank
-
Hello All, I have an ASP.net application that is currently using the push method and displaying reports off of ADO.net datasets. I am struggling with a design method for the following questions: 1. .NET treats each crystal report you add to your project as a class and I am using a several reports and the ReportDocument class to set datasources for each one and so on. Depending on which report my users select in an aspx page i show them the report they want by creating an object of the type of report they selected. I am wondering if I can create say Object myobj = new Object() and depending on what report they select cast the general myobj object into the specific type of the report class the user selected instead of having to instantiate an instance of every type of report within my code? Thanks for your help:confused: Frank
Depending on how you display the list of reports, you can use
Activator.CreateInstance
to create an instance of the report. Cast that to theReportDocument
base class and callSetDataSource
with your ADO.NETDataSet
. For instance, if you use aListBox
you could set the display name to whatever you want, but set the value for each item to the corresponding .rpt name. Since this gets compiled to a class with the same name using a namespace that is your project's root namespace (the project name by default) plus any folders it's in, you could do something like this:string reportName = "MyWebApp.Reports." + listBox1.SelectedValue;
ReportDocument report = Activator.CreateInstance(reportName, "MyWebApp.dll");
report.SetDataSource(dataSet1);Of course, you should add error handling like try-catch blocks and conditionals (like checking for
null
values, etc., since throwing exceptions is expensive).Microsoft MVP, Visual C# My Articles