How to solve a datasource error in Microsoft report viewer? - c#

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

Related

filter ReportViewer data?

I'm trying to make a button to load and filter my ReportViewer by name but the output is data source instance not supplied. Here is my code:
private void btnReport_Click(object sender, EventArgs e)
{
String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ReportDataSource rds = new ReportDataSource("practise", ds.Tables[0]);
reportViewer2.ProcessingMode = ProcessingMode.Local;
reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
if (ds.Tables[0].Rows.Count > 0)
{
reportViewer2.LocalReport.DataSources.Clear();
reportViewer2.LocalReport.DataSources.Add(rds);
reportViewer2.RefreshReport();
}
This could be simple. Please check that your dataset name in the report is not "DataSet1" by default. Also, make sure that the path for report is correct. Here is a working example:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DemoDB"].ConnectionString))
{
String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
SqlDataAdapter adp = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);
reportViewer2.ProcessingMode = ProcessingMode.Local;
reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
if (ds.Tables[0].Rows.Count > 0)
{
reportViewer2.LocalReport.DataSources.Clear();
reportViewer2.LocalReport.DataSources.Add(rds);
reportViewer2.RefreshReport();
}
}
I hope that would help someone.

Save edits from DataGridView to Datatable

Hi i want to save edits in DataGriView to datatable , i tried that code but an error shows 'System.ArgumentOutOfRangeException: 'The index was out of range. It must not be negative and must be smaller than the size of the collection.
Parameter name: index '' help
private void button11_Click(object sender, EventArgs e)
{
con.Open();
String query = "SELECT * FROM Taom";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
dataGridView1.DataSource = dt1;
SDA.Fill(dt);
dataGridView1.Rows[2].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value)));
dataGridView1.Rows[3].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value)));
ds1.Tables.Add(dt);
con.Close();}
I changed my code to that code no errors showed after i run values on datagridview change but no changes in datatable !!!
string query = "SELECT * FROM [dbo].[Taom]";
SqlConnection conn = new SqlConnection(#"Data Source=STE-P0024818PW;Initial Catalog=test;Integrated Security=True");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Contents"] = "98"; //Before it was 10
dt.Rows[1]["Contents"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
Fill the right value inside your table and after pass the table rightly filled in your datasource, don't change this directly in the gridview like:
I try myself and it works:
private void Run()
{
string query = "SELECT * FROM dbo.[Anrufthema]";
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Anrufthema"] = "98"; //Before it was 10
dt.Rows[1]["Anrufthema"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
}
My Result, it works !

Search Box not working

When I type in my search box my Data grid view doesn't show the data that i typed in even though its in the database. This is my codes for the search box
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MySqlDataAdapter adapt = new MySqlDataAdapter();
con = new MySqlConnection(MyConnectionString);
adapt = new MySqlDataAdapter("SELECT * from tblregister where FirstName like '" + textBox1.Text + "%'", con);
DataTable ds = new DataTable();
DataSet dt = new DataSet();
adapt.Fill(dt);
dataGridView1.DataSource =ds;

Only column header is showing up on report viewer. No data is showing up

This is with reference to my previous question "Report not showing up on report viewer"
I am developing a c# application where I need to generate a report. I am a using a dataset which is filled with the data coming from a stored procedure which takes one parameter from the C# code. I am creating a parameter in report1.rdlc and populating it with the data from a textbox. When I run the application I can see only column headers on the report while as it does’t show any data on the report viewer.
My code below
public void GenerateBranchwiseReport()
{
conn.Open();
SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
while (BranchReportread.Read())
{
BranchManagerName.Add(BranchReportread.GetValue(0).ToString());
}
conn.Close();
foreach (string managername in BranchManagerName)
{
conn.Open();
SqlCommand GetReportDatacmd = new SqlCommand();
GetReportDatacmd.CommandText = "USP_BranchwiseReport";
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(new SqlParameter("#BranchManagerName", managername));
GetReportDatacmd.Connection = conn;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = true;
string reportname = #"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = #"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("ManagerName", managername);
this.reportViewer1.LocalReport.SetParameters(param);
ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//SendEmail();
}
public void GenerateBranchwiseReport()
{
string ManagerNames="";
conn.Open();
SqlCommand BranchReportcmd = new SqlCommand("select [am/bsi name] from masterlookup where [asc type]='BRANCH' group by [am/bsi name]", conn);
SqlDataReader BranchReportread = BranchReportcmd.ExecuteReader();
while (BranchReportread.Read())
{
if (ManagerNames.length==0)
ManagerNames="'"+BranchReportread.GetValue(0).ToString()+"'";
else
ManagerNames =ManagerNames + ",'" + BranchReportread.GetValue(0).ToString()+ "'";
}
conn.Close();
//So now, You have Multiple Managers in ManagerNames string You can send it to SQL, which must used in Where clause with IN (#Managers) in you Stored Procedure.
conn.Open();
SqlCommand GetReportDatacmd = new SqlCommand();
GetReportDatacmd.CommandText = "USP_BranchwiseReport";
GetReportDatacmd.CommandType = CommandType.StoredProcedure;
GetReportDatacmd.Parameters.Add(new SqlParameter("#BranchManagerName", ManagerNames));
GetReportDatacmd.Connection = conn;
GetReportDatacmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(GetReportDatacmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
reportViewer1.Reset();
this.reportViewer1.Visible = true;
string reportname = #"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
this.reportViewer1.LocalReport.ReportPath = #"d:\users\administrator\documents\visual studio 2010\Projects\ReportwithParameter\ReportwithParameter\Report1.rdlc";
ReportParameter[] param = new ReportParameter[1];
param[0] = new ReportParameter("ManagerName", managername);
this.reportViewer1.LocalReport.SetParameters(param);
ReportDataSource ReportBranch = new ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]);
this.reportViewer1.LocalReport.ReportEmbeddedResource = reportname;
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DatasetWithParameter.USP_BranchwiseReport", ds.Tables[0]));
//this.reportViewer1.LocalReport.DataSources.Add(ReportBranch);
this.reportViewer1.LocalReport.Refresh();
//SendEmail();

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.

Categories