ReportViewer databinding (WPF) - wpf

I have a rdlc file and I try to show it using report viewer control in WPF. My DataSource is called 'OfficeDataSet' and DataSet used in report is called 'Address'. I try to initialize report viewer (I put it in WindowsFormsHost) like this:
private void PrepareReport()
{
reportDataSource = new ReportDataSource();
officeDataSet = new OfficeDataSet();
adapter = new AddressTableAdapter();
adapter.Fill(officeDataSet.Address);
reportDataSource.Name = "OfficeDataSet_Address";
reportDataSource.Value = officeDataSet.Address;
viewerInstance.LocalReport.DataSources.Add(reportDataSource);
viewerInstance.LocalReport.ReportEmbeddedResource = "WpfApplication1.Reports.TestReport.rdlc";
viewerInstance.Location = new System.Drawing.Point(0, 0);
viewerInstance.RefreshReport();
}
but I got the message "A data source instance has not been supplied for the data source 'Address'".
Anybody know what can be wrong with this code? (I'm totally new in WPF and Reporting Services).

If i remember well, the datasource into the report file should have the same name (or maybe value) as the reportDataSource. So in your report file the datasource should be called "OfficeDataSet_Address".

Related

WPF designer error due to incorrect running assembly path

I have a control that upon loading parses an XML file with country names and later populates a ComboBox. I access my XML as following:
public DataTable GetData()
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + #"..\..\Resources\XML\countries.xml");
return ds.Tables[0];
}
GetData is called inside control's Loaded handler.
At design time, however, I get an error saying that directory does not exist with the XML file and as you see it is looking in the wrong AppDomain path (Visual Studio, not project):
How can I resolve this behaviour?
The typical way to avoid this is to wrap the code in a check to see if it is running in the designer.
DesignerProperties.GetIsInDesignMode(this)
Will return true in design mode, and false when the app is running "for real" (if called from a DesignerObject). You should only try to read your xml when not in design mdoe.

Moving from SQL Databinding to using the Entity framework

I'm changing my WPF application to use the Entity Framework instead of calling sql db directly.
On this one window I have a listview containing a gridview and I'm databinding it by using the following method which calls a stored procedure to get the data.
Now I already have my model generated from my existing sql database and included the stored proc...
How would I go about changing this method to read the data from the entity model instead of directly from sql?
public static void BindData(DataGrid grid)
{
SqlConnection loginCon = new SqlConnection();
loginCon.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlDataAdapter adapter = new SqlDataAdapter("sp_SELECT_CONSHEAD", loginCon))
{
DataSet data = new DataSet();
adapter.Fill(data);
grid.ItemsSource = data.Tables[0].DefaultView;
}
}
You can use Function Import to get the stored procedure mapped to entity in EntityFramework. Then you can directly call the function in your code with single line of code.
grid.ItemsSource = dbContext.GetSP_Select_Conshead();

Listbox won't display updated database info once program is published

I am a beginner to programming and I am trying to make a program that tracks my jogging data in WPF. I set up a listbox that has databinding with my compact sql database where my info gets logged to. While I am in visual studio it works as it should, but when I publish the program, the listbox will no longer display the proper info from the database. It starts fresh all over again each time I start a new session of the program. I know that the info is in the database because I have charts that will properly show all the latest logged data. The listbox, however, will only show data logged during that current session. When I close down the program and reopen, the data from my previous session is not displayed in the listbox anymore.
My code looks like this:
runDataDataSet1 ds;
runDataDataSet1TableAdapters.runDataTableTableAdapter dt;
weightDataSet wds;
weightDataSetTableAdapters.weightTableTableAdapter wdt;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ds = ((runDataDataSet1)(this.FindResource("runDataDataSet1")));
dt = new runDataDataSet1TableAdapters.runDataTableTableAdapter();
dt.Fill(ds.runDataTable);
CollectionViewSource cvs = ((CollectionViewSource)(this.FindResource("runDataTableViewSource")));
cvs.View.MoveCurrentToFirst();
wds = ((weightDataSet)(this.FindResource("weightDataSet")));
wdt = new weightDataSetTableAdapters.weightTableTableAdapter();
wdt.Fill(wds.weightTable);
CollectionViewSource wcvs = ((CollectionViewSource)(this.FindResource("weightTableViewSource")));
wcvs.View.MoveCurrentToFirst();
To submit the data I have this code for when the submit button is pushed:
double pace = runData.runPace(runData.milesRun, runData.timeRun);
double pace2 = Math.Round(pace, 1);
double milesDuration = Math.Round((runData.mileDuration(runData.milesRun, runData.timeRun)), 1);
paceTextBox.Text = ((milesDuration) + " min mile");
//UPDATE DATABASE
string myConnectionString = #"Data Source=C:\Users\zfam\My Projects\programming\visual studio\XTrakker\XTrakker\runData.sdf";
string mySelectQuery = "SELECT [int], [runDate], [runDist], [runTime], [runPace] FROM [runDataTable] order by [int] desc";
DataRow dr = ds.Tables["runDataTable"].NewRow();
SqlCeDataAdapter adapter = new SqlCeDataAdapter(mySelectQuery, myConnectionString);
dr["runDate"] = date;
dr["runDist"] = distData;
dr["runTime"] = timeData;
dr["runPace"] = milesDuration;
ds.Tables["runDataTable"].Rows.Add(dr);
SqlCeCommandBuilder projectBuilder = new SqlCeCommandBuilder(adapter);
DataSet newSet = ds.GetChanges(DataRowState.Added);
adapter.Update(newSet, "runDataTable");
adapter.Dispose();
Again, while I am in visual studio and I run the program, this all works as it should. Once I publish, the listbox will only show the data from the current session, even the the actual database file is being updated. Perhaps I am doing this all wrong? Any thoughts?
Hard to say, but I am a bit confused about this line:
string myConnectionString = #"Data Source=C:\Users\zfam\My Projects\programming\visual studio\XTrakker\XTrakker\runData.sdf";
Look like you hardcode your datasource provider. Are you sure you use the same datasource in visual studio, and when you publish your app?
Maybe the datasource is re-created at each execution, which could explain why you have always new data.

How can I reuse one ReportViewer control for many reports with EF data sources?

I would like to stick with one single form and ReportViewer control, and at runtime assign various reports and data sources. The diverse and complex solutions to this revealed by a quick google check prompted me to rather ask here. How can I achieve this?
You'll need a form featuring a ReportViewer control, RDLC reports, and data sources; there are possibly several ways of implementing this, but you can have a "report manager" class exposing methods that each display a specific report (ex. ShowOrdersReport(), ShowTimeSheetReport(), etc.) - or you could define a base ReportBase class with a Show() method implementation that prompts for parameters when needed... whatever works, it will essentially boil down to this:
var reportForm = new ReportViewerForm(); // instantiate the form
// load the report definition into the viewer:
reportForm.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.Report.rdlc";
// loading the data works great with a DataSet:
var data = _reportingDataSet.Tables[0];
// create the ReportDataSource with the report data:
var reportDataSource = new ReportDataSource("rdsReportDataSource", data);
reportForm.ShowReport(new[] { reportDataSource });
Here you'll want to inject the _reportingDataSet dependency; if you're using parameterized stored procedures, you'll want to prompt for the report parameters before you populate the DataSet.
The ShowReport() method adds the data sources to LocalReport.DataSources, and then calls RefreshReport() which displays the report you've specified.
public void ShowReport(IEnumerable<ReportDataSource> dataSources)
{
foreach (var dataSource in dataSources)
reportViewer1.LocalReport.DataSources.Add(dataSource);
reportViewer1.RefreshReport();
}
If you are using Crystal report then use this on click of load report button
CrystalReportViewer.ReportSource = ReportName
If you are using MS ReportViewer control then it needs two important steps to show the reports
Assign the report file path to the ReportViewer
Set the data source
So for example a ReportViewer control named reportViewer1 needs to display a SomeReport.rdlc file then following code is required (let's say on click of a button)
this.reportViewer1.LocalReport.ReportPath = #"Add absolute path of rdlc file"//e.g. #"C:\SomeReport.rdlc" ;
this.reportViewer1.RefreshReport();
This is just a simple example and for simplicity i have used static report if you need to display data from database just assign datasource property before call to RefreshReport
e.g.
this.reportViewer1.LocalReport.DataSources.Add(MyreportDataSource);
where MyreportDataSource is object of type ReportDataSource, you can easily convert any ADO.net DataTable to ReportDataSource object.
I hope this much info will do for you, in case you want to see more details you may refer a very good article at this location
http://www.c-sharpcorner.com/UploadFile/robo60/StandaloneRDLCReports11142007183516PM/StandaloneRDLCReports.aspx

How to modify a DataTable's Query Generated in the designer with code

I have a RDLC file with a dataset Created in the designer and the report loads ok no errors.
My problem is the query in the DataTable is alway's the same.
I would like to change that query in the designer but haveb't been able.
I also have tried to create the dataset in code and then add it yo the RDLC file but I was not successful to add the Dataset to the RDLC.
Thanks
Try this if you want to load a custom DataTable inside your report:
//clean old dataset
yourReport.LocalReport.DataSources.Clear();
DataTable yourDataTable = null;
yourDataTable = loadDataTableFromYourDataSource();
ReportDataSource datasource = new ReportDataSource("YourDataSourceNameFoundInsideTheRdlc", yourDataTable);
yourReport.LocalReport.DataSources.Add(datasource);
//refresh the report
yourReport.LocalReport.Refresh();
hope it helps

Resources