Reputation: 247
Background:
I've been tasked with converting an Access database application to ASP.Net C# MVC. This is my first MVC application.
There are 10 reports that need converting. We're using RDLC files and reportviewer. I'm using Visual Studio 2010 with all the most recent patches. We're hooking up to a SQL Server database that's been populated by the existing Access database, so the table structure is pretty much set in stone, or else we'll have to try to convert 10 years of data.
I've completed all but two of the reports. The final reports require more processing, and don't just display data from the database. The easiest way to do this would be to create a C# object and do all the processing server-side, then use RDLC to display the results.
Issue
The problem I'm having is that Visual Studio won't recognize the objects I've created as potential datasources. Every time I try to "add dataset" it brings up the "datasource configuration wizard" and only offers the SQL Server database as the possible data connection. I know there's a screen that exists that allows you to select an object as your dataset, but I never see that screen.
Here are the objects (processing functions removed for clarity):
public class TurnAroundVal
{
// Registration Package information
public string dataType { get; set; }
// Calculated totals; values only set through constructor or calculation function
public int packageCount { get; private set; }
public int dayCount { get; set; }
public double avgTurnAround { get; private set; }
public int upperRange { get; private set; }
public int lowerRange { get; private set; }
}
public class TurnAroundVals
{
// Public Variables
public IEnumerable<TurnAroundVal> TurnArounds { get; private set; }
public DatePass dates { get; set; }
public int pkgTotal { get; private set; }
public double dayTotal { get; private set; }
public double avgAllTurnArounds { get; private set; }
}
I also would be willing to use the IEnumerable of "TurnAroundVal" as a datasource, and just pass the dates, int, and doubles in as parameters. Either would work.
Question
Is there a setting in Visual Studio 2010 that I'm missing to allow the RDLC Designer to see the objects I created? Am I wrong in even thinking this will work with the classes I defined?
Answer
All of the suggestions given were helpful, but ultimately what got it to work for me was to create a temporary non-web project in the same solution and create the RDLC there. When I went to add a dataset, it shows "Objects" as an option. You have to add a reference to your web project so that your objects are visible, but then you can pick the one you want and use that in the RDLC. After that, you can just drag-drop the RDLC file into your web project, delete the temp project, and you're good to go. Just remember that you have to add the datasources manually in code, and they have to be named the same as you specified in the RDLC.
Upvotes: 20
Views: 32606
Reputation: 136
I was able to add an object as a datasource in VS 2017 without the report Wizard. [Winforms app using c#] [PS I am a NOOB, so there is going to be mistakes etc, but i hope this helps with giving a direction.] steps I used:
part A 1. create an object that represents the data e.g.: object / model class in my case it is an appointment object
class FutureVisitsModel
{
public DateTime StartDate {get; set;}
public string Client_Id { get; set; }
public string Treatment_Desc { get; set; }
public string Service_Code { get; set; }
public FutureVisitsModel()
{
DateTime startdate = StartDate;
String clinetid = Client_Id;
String treatmentdesc = Treatment_Desc;
String serviceCode = Service_Code;
}
}
Part B: create report 2. Added report to solution: right clicked solution, select add new item and chose report enter image description here 3. opened the blank report 4. from toolbox, drag n drop a table to the report 5. a dialogue opens to choose a data source 6. select the object from your solution [ ypu will be looking for the class you created in part A
Part C: create a report viewer form 7. on solution explorer create a new form "formRptViewer" 8. open the form and add report viewer control 9. If you dont have the control in your tool box, you will need to install the report viewer package from nugget or install via package manger console. webforms version : Install-Package Microsoft.ReportingServices.ReportViewerControl.WebForms winforms version: Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms
9.1 set the control of the report viewer to public in solution explorer click the dropdown next to your formRptViewer to see the files that make up the form edit this file: formRptViewer.Designer.cs change the controll pf the reprtviewer to public public Microsoft.Reporting.WinForms.ReportViewer ReportViewer1;
part D: create a datatable and send it to report 10. create a datatable DataTable dataTableFutureVisits = new DataTable(); [you will need to fill the datatable with y our own data] 11. add using statement using Microsoft.Reporting.WinForms; 12. set the data source and create a new instance of the report viewer
ReportDataSource rs = new ReportDataSource();
rs.Name = "DataSet1";
rs.Value = dataTableFutureVisits;
FormRptViewer frm = new FormRptViewer();
frm.ReportViewer1.LocalReport.DataSources.Clear();
frm.ReportViewer1.LocalReport.DataSources.Add(rs);
frm.ReportViewer1.LocalReport.ReportEmbeddedResource = "ChiroRecallList.RptFutureVisits.rdlc";
// name the report with date
frm.ReportViewer1.LocalReport.DisplayName = "Your File Name Goes Here" + "_" + DateTime.Now.ToString("yyyyMMdd HH:mm");
frm.ShowDialog();
Part E: updating the report: adding columns to the datatable and show them in the report [ also forks for removing a column]
delete the data source that is named after your model screen shot
clean and rebuild solution screenshot
Upvotes: 0
Reputation: 30403
I've just come across this same issue when trying to create RDLC reports in an ASP.NET MVC project in Visual Studio 2017, so I'm adding this as a separate answer to make it clear this is still an issue at January 2018.
My solution consisted of a C# library project and an MVC client project.
Adding reports (by choosing at add a new item, then choosing a Report Wizard item) in the C# library project brings up the Report Wizard with the Data Source Configuration Wizard (where I can choose whether to use a Database, Service or an Object as the datasource) modally on top of it:
Whereas, choosing to add a new report wizard item in the MVC project just brought up the Report Wizard without the Data Source Configuration Wizard:
I tried adding business object classes into my MVC project, recompiling it and then adding a Report to the MVC Project again, but I still got the second screenshot, so it seems like you just don't get the Data Source Configuration Wizard when adding to MVC projects for some reason.
Upvotes: 0
Reputation: 21
Visual Studio does not recognize your class when you use only public members. When you use getter and setter instead, you can see your class when you choose your datasource.
There are some other wizards in Visual Studio that do not work with public members.
Cheers, Markus
Upvotes: 2
Reputation: 21
Set your objects under the same namespace but on a different project which you then reference on your UI project.
Also try implementing INotifyPropertyChanged
.
Upvotes: 2
Reputation: 398
You may need to put the class file in the App_Data or App_Code folder, but I am not certain.
This might also help.
http://msdn.microsoft.com/en-us/library/ms251692%28v=vs.100%29.aspx
Upvotes: 3