Good Afternoon All,
I've spent the best part of the last 2 weeks searching for help on the issue I'm having with my C# WinForm application.
I have a form which has a DataGridView displaying details pulled from a database. When the user double-clicks a row in this grid, the code grabs the value from the CustomerID column, puts it into a static public string, then opens another form which has a reportviewer.
What I'm trying to do then is to get the reportviewer to run a report, using the CustomerID as a parameter.
So far all I've managed to do is get it to state that the report definition has not been specified.
My SQL server is an old server which does not have SSRS, and I can't install it due to change request issues, so I'm having to use a Local Report, not a Server Report.
The way I created the report was to go to my Solution Explorer in Visual Studio, and go to Add - New Item - Report, which I then gave a dataset and a parameter. I put the report in a subfolder called Reports, which sits within the solution next to all my forms.
The code for my reportviewer form is as below. Don't laugh too much, I'm 100% self taught.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace FormsApp
{
public partial class ReportGenerator : Form
{
public ReportGenerator()
{
InitializeComponent();
this.reportViewer1.RefreshReport();
}
private void ReportGenerator_Load(object sender, EventArgs e)
{
if (CustomerList.CustomerID!= "")
{
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = "\\Reports\\CustomerDetails.rdlc";
this.reportViewer1.ShowParameterPrompts = true;
ReportParameter CustID = new ReportParameter("CustomerID",CustomerList.CustomerID);
this.reportViewer1.LocalReport.SetParameters(CustID);
}
else
{}
}
}
}
Am I using the LocalReport incorrectly? I'm really stumped and this is the last bit I need to get working then my application is complete.
Any help would be greatly appreciated.
Many thanks
UPDATE
My code now shows as below following suggestions from various sources.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Data.SqlClient;
namespace FormsApp
{
public partial class ReportGenerator : Form
{
public ReportGenerator()
{
InitializeComponent();
this.reportViewer1.RefreshReport();
}
private DataTable getCustomerData()
{
SqlConnection con = new SqlConnection(Home.ConString);
DataSet ds = new DataSet();
ds.DataSetName = "CustomerListRetrieve";
string sql = "SELECT * FROM Customers";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
DataTable dt = ds.Tables[0];
return dt;
}
private void ReportGenerator_Load(object sender, EventArgs e)
{
if (CustomerList.CustomerID != "")
{
this.reportViewer1.Reset();
this.reportViewer1.LocalReport.ReportEmbeddedResource = "FormsApp.Reports.CustomerDetails.rdlc";
ReportDataSource rds = new ReportDataSource("CustomerListRetrieve", getCustomerData());
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(rds);
ReportParameter CustID= new ReportParameter("CustomerID", CustomerList.CustomerID);
this.reportViewer1.LocalReport.SetParameters(CustID);
this.reportViewer1.LocalReport.Refresh();
ViewButtonClicked();
}
else
{}
}
private void ViewButtonClicked(object sender, ReportParametersEventArgs e)
{
var SubmittedParameters = e.Parameters.Clone();
e.Parameters.Clear();
SubmittedParameters.Where(x => x.Name == "Parametername").FirstOrDefault().Values[0] = "YourValue";
SubmittedParameters.ToList().ForEach(x => e.Parameters.Add(x));
}
private void OnReportChosenFromList()
{
reportViewer1.SubmittingParameterValues -= ViewButtonClicked;
reportViewer1.SubmittingParameterValues += ViewButtonClicked;
}
}
}
The datasource code I added following the response from codingbiz. After adding this, I no longer got the report definition error, just a blank window when the reportviewer
loaded.
The bottom two methods - ViewButtonClicked and OnReportChosenFromList were added following the suggestion from Bernard Walters. After adding this I get 2 errors stating:
- Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
Microsoft.Reporting.WinForms.ReportParameterCollection
does not contain a definition for 'Clone' and no extension method 'Clone'...
Really stumped now. Probably implemented the suggestions wrong.