RDLC issue with mutiple datasets Winform project in c# - c#

I'm building a report in report viewer. With a single dataset, it works great but I need to include in this same report, mutiple datasets. Can you tell me what i'm doing wrong please (with some code if possible). I found a whole bunch of info on this issue but everything is not in the same programming language. I'm using C#. In the .RDLC, I have one datasource created and 2 datasets (DataSet and DataSet1). Here is my current code:
private void LoadReport()
{
try
{
MySqlConnection con = new MySqlConnection(conSettings.ToString());
MySqlCommand cmd = new MySqlCommand("packing_slips", con);
MySqlCommand cmd1 = new MySqlCommand("client_info", con);
con.Open();
cmd.Parameters.Add("#project", MySqlDbType.VarChar, 20).Value = project_id_box.Text;
cmd.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add("#project", MySqlDbType.VarChar, 20).Value = project_id_box.Text;
cmd1.CommandType = CommandType.StoredProcedure;
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
MySqlDataAdapter adp1 = new MySqlDataAdapter(cmd1);
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
adp.Fill(ds);
adp1.Fill(ds1);
reportViewer1.Reset();
this.reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Value = ds.Tables[0];
reportDataSource.Name = "DataSet";
ReportDataSource reportDataSource1 = new ReportDataSource();
reportDataSource1.Value = ds1.Tables[0];
reportDataSource1.Name = "DataSet1";
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportPath = "project_report.rdlc";
this.packing_slipsTableAdapter.Fill(this.shopmanagerDataSet.packing_slips);
this.projectsTableAdapter.Fill(this.shopmanagerDataSet.projects);
this.reportViewer1.RefreshReport();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I'm not getting any compile errors when compiling but the report is blank. I'm retreiving the data from a MySQL db with stored procedures. When debugging i'm seeing that ds and ds1 are getting populated correctly. Thanks.

I found the answer. When adding mutiple datasets to a RDLC, you need to specify from what dataset the information is coming from in the textbox properties. EG:
=First(Fields!project_number.Value, "DataSet")
That fixed it for me. The code above is good for anyone else having this issue.

Related

Reportviewer with MySQL stored procedure. Report not showing data (winform)

I built a form that contains a report viewer and also created a stored procedure in MySQL workbench 8.0. The procedure was tested and works ok. Here is my code:
private void LoadReport()
{
try
{
MySqlParameter quote = new MySqlParameter();
quote = new MySqlParameter("quote", SqlDbType.VarChar);
quote.Value = quote_id_box.Text;
MySqlConnection con = new MySqlConnection(conSettings.ToString());
MySqlCommand cmd = new MySqlCommand();
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "project_report";
cmd.Parameters.Add(quote);
MySqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
dt.Load(dr);
cmd.ExecuteNonQuery();
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("pr_DataSet", dt);
this.reportViewer1.LocalReport.DataSources.Add(rds);
this.reportViewer1.RefreshReport();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The dataTable is getting a response from MySQL and it has the right data in it but the problem is that the report won't show the information. It gives me
"the report source has not been defined"
Any ideas on what i'm doing wrong? This is my first report ever using report viewer. Any help would be greatly appreciated. Thanks

Creating Dataset Dynamically and passing to ReportViewer

all i did is just create a reportViewer in the form, then i have this code:
SqlConnection cn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\billingSystem\Store.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
private void Form1_Load()
{
runRptViewer();
cn.Open();
}
private void rptGetDataset()
{
DataSet ds = new DataSet();
ds.DataSetName = "dsNewDataSet";
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn);
ds.GetXmlSchema();
da.Fill(ds);
ds.WriteXmlSchema(#"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd");
ds.WriteXml(#"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml");
}
private DataTable getData()
{
DataSet dss = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NewBill", cn);
da.Fill(dss);
DataTable dt = dss.Tables["NewBill"];
return dt;
}
private void runRptViewer()
{
this.reportViewer2.Reset();
//this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
this.reportViewer2.LocalReport.ReportPath =(#"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc");
ReportDataSource rds = new ReportDataSource("dsNewDataSet_NewBill", getData());
this.reportViewer2.LocalReport.DataSources.Clear();
this.reportViewer2.LocalReport.DataSources.Add(rds);
//this.reportViewer2.DataBind();
this.reportViewer2.LocalReport.Refresh();
}
}
i have two reportViewer the reportViewer1 work but in case the directory of the db has change it will not work, so thats why i try in another reportViewer, to make it work even if the directory of the db changed, i can just change the Connection string.
The problem is the report don't show anything, i think the problem in the code:
//this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
this is a windows form so there is no server, ive change it to:
this.reportViewer2.LocalReport.ReportPath =(#"G:\I.S\Testoooooooo\Testoooooooo\Report1.rdlc");
and this one dont work:
//this.reportViewer2.DataBind();
i cant understand this two lines, does it mean to create a Dataset1.xsd and Dataset1.xml, or just edit them.
ds.WriteXmlSchema(#"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xsd");
ds.WriteXml(#"G:\I.S\Testoooooooo\Testoooooooo\Dataset1.xml");
if possible i need a steps from creating every thing to codding that will be great.
How did you create your dataset? Is it SQL or from Objects in memory?
If you havent created a dataset, you do need to create one before the report can work properly. This might help: http://shrutikapoor-ubc.blogspot.com/2013/05/using-business-objects-in-report-viewer.html
To use the report viewer in your C# WinForm project add the following code to a button or inthe form_load:
string strConnectionString = "Data Source=(local);Initial Catalog=Projects_DB;Integrated Security=True";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_GetProject " + "'0660CAD6-6F1A-4D19-A1FD-17BF3655AC98'");
cmd.CommandType = CommandType.Text;
cmd.Connection = new SqlConnection (strConnectionString);
da.SelectCommand = cmd;
da.Fill(ds,"DataSet1");
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds.Tables[0]));
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();
Assuming that you have a stored procedure that accepts #ProjectID parameter. You have to set the cmd.CommandType = CommandType.Text if you pass these parameters along with the sql command. However, if you don't want to pass parameters just change the commandType to cmd.CommandType = CommandType.StoredProcedure.
Below is the stored procedure used in this example:
CREATE PROC [dbo].[sp_GetProject]
#ProjectID nvarchar(50)=NULL
AS
BEGIN
SELECT ProjectID, ProjectName FROM Projects
WHERE
(#ProjectID IS NULL) OR (ProjectID = #ProjectID)
END
Microsoft has a pretty good walkthrough here...
http://blogs.msdn.com/b/sqlforum/archive/2011/04/28/sql-reporting-services-ssrs-bind-dynamic-dataset-to-your-local-report-with-reportviewer.aspx

reportviewer rdl file

I am using the ReportViewer control to design a report but when I run the project I get this error:
A data source instance has not been supplied for the data source 'DataSet1'.
Here is my code:
SqlConnection myConnection = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sqla = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
myConnection.ConnectionString = SqlDataSource1.ConnectionString;
cmd.Connection = myConnection;
cmd.CommandText ="select * from users";
cmd.CommandType = CommandType.Text;
sqla.SelectCommand = cmd;
sqla.Fill(dt);
sqla.Fill(ds);
ReportViewer1.Reset();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "reports/allusers.rdl";
ReportDataSource rds = new ReportDataSource("ds_users",dt);
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.ZoomMode = ZoomMode.Percent;
ReportViewer1.LocalReport.Refresh();
What am I missing?
I added a "DataSet1" when I created the rdl file so I needed to pass the data this Dataset to render the report. I changed the ReportDataSource lines to the following:
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";
rds.Value = dt;
And that solved the error message.

How to solve a datasource error in Microsoft report viewer?

i am having Report page in my windows project .In that i included microsoftReportViewer . In that page two combobox is there .items in 1st combobox are:
FirmDetails
ChittyHolding
LoanDetails.
Corresponding to selection this items in combobox1, items in combobox will change .I need report According to this values in 2 combbox.
My code is like
if (cbReprt.Text == "FirmDetails")
{
if (cbGeneral.Text == "AllFirmDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds = new allfirmdetails();
string str = "Select * from View_2";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable dt = new DataTable();
da.Fill(dt);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\Report7.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt));
reportViewer1.RefreshReport();
}
else
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds1 = new allfirmdetails();
string str1 = "Select * from View_2 where FirmName='" + cbGeneral.Text + "'";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(ds1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\firmwise.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt1));
reportViewer1.RefreshReport();
}
}
else if (cbReprt.Text == "ChittyDetails")
{
if (cbGeneral.Text == "AllChittyDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
ChittyDetails ds2 = new ChittyDetails();
string str2 = "Select * from View_1";
SqlCommand cmd2 = new SqlCommand(str2, con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(ds2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\allchitty.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ChittyDetails_View_1", dt2));
reportViewer1.RefreshReport();
}
First I select chittyholding details. Then I got the report. Then I select chitty details report, I got the error like
**An error occured during local report processing .A data sourcr instance has not been supplied for the data source "ChittyHolding_View_7"**. If I close and run the project again ,then that firmdetails selection will work.But another will not work.It means Only for one selection I am getting the report. Why? Please solve this error
Try to call reportViewer1.Reset(); instead of reportViewer1.Refresh(); at the beginning of every condition

DataGridView binding not working

I am working on a winforms project and i have this following code in the Form_Load method. But it doesnt work. Can anyone help me?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sella.Properties.Settings.Database1ConnectionString1"].ConnectionString);
// A SqlCommand object is used to execute the SQL commands.
SqlCommand scmd = new SqlCommand("Select * From CustCalls", conn);
// A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
SqlDataAdapter sda = new SqlDataAdapter(scmd);
// Create and Fill a new DataSet.
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds;
Try sourcing directly to the table in the dataset:
dataGridView1.DataSource = ds.Tables[0];
SqlDataAdapter sda = new SqlDataAdapter(scmd, conn);
dataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;

Categories