简体   繁体   中英

ReportViewer doesn't show data

The idea is to enter new fields then show them in report http://www.mediafire.com/view/6sla63hpecx2cc9/workflow.PNG mediafire.com/view/1juiekfr5gbk48d/theproplem.GIF

i filter the table in the DataSet and it suppose to show data, it works perfect with the DataGridView, now i hope to find a way to fill the report with the same data set that fill the datagrid

public partial class bill : Form
{
    SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\I.S\C#\C#(Projects)\Nothin\billingSystem\billingSystem\Store.mdf;Integrated Security=True;User Instance=True");

    public bill(string Cusn,string su,string am,string to,string Di, string Cnum)
    {
        InitializeComponent();
        label1.Text = Cusn;
        label2.Text = su;
        label3.Text = am;
        label4.Text = to;
        label5.Text = Di;
        label6.Text = DateTime.Now.ToString("d/M/yyyy");
        label7.Text = Cnum;
    }

    private void bill_Load(object sender, EventArgs e)
    {
        LoadReport();
    }

    private void LoadReport()
    {

        int R = Convert.ToInt32(label7.Text);

        SqlDataAdapter ADAP = new SqlDataAdapter("Select * from Newbill where Con = '" + R + "'", cn);
        DataSet DS = new DataSet();
        ADAP.Fill(DS, "Store");
        dataGridView1.DataSource = DS.Tables["Store"];



        // TODO: This line of code loads data into the 'DataSet10.NewBill' table. You can move, or remove it, as needed.
        this.NewBillTableAdapter.Fill(this.DataSet10.NewBill, R);
        this.reportViewer1.RefreshReport();

        ReportParameter[] allPar = new ReportParameter[5]; // create parameters array
        ReportParameter parSu = new ReportParameter("Summation", label2.Text);
        ReportParameter parDiscount = new ReportParameter("Discount", label5.Text);
        ReportParameter parDisA = new ReportParameter("DiscountAmount", label3.Text);
        ReportParameter parTotal = new ReportParameter("Total", label4.Text);
        ReportParameter parCus = new ReportParameter("CustomerName", label1.Text);

        allPar[0] = parSu; //assign parameters to parameter array
        allPar[1] = parDiscount;
        allPar[2] = parTotal;
        allPar[3] = parDisA;
        allPar[4] = parCus;

        this.reportViewer1.LocalReport.SetParameters(allPar);
        this.reportViewer1.RefreshReport();
        // TODO: This line of code loads data into the 'DataSet1.NewBill' table. You can move, or remove it, as needed.
        //this.NewBillTableAdapter.Fill(this.DataSet1.NewBill, R, O);
        //this.reportViewer1.RefreshReport();
    }
}

IVE SEEN THIS : http://www.verious.com/qa/how-can-i-load-datatable-as-report-data-source/

actually i need steps to view data from DB with Query to filter the data in the report, All i found on google is just the like this: http://www.codeproject.com/Articles/31862/Dynamic-Binding-Of-RDLC-To-ReportViewer but its not working when a new fields has inserted.

how about this, i try to refresh the dataset to include the changes that happens in the table (in my case insert new rows "new data") But it gives me :

A data source instance has not been supplied for the data source 'DataSet 10_NewBill'.


private void LoadReport()
        {
            int R = Convert.ToInt32(label7.Text);

            this.reportViewer1.LocalReport.DataSources.Clear();
            DataTable dt = new DataTable();
            dt = this.NewBillTableAdapter.GetData(R);

            ReportDataSource rprtDTSource = new ReportDataSource(dt.TableName, dt);

            this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
            this.reportViewer1.RefreshReport(); 

            // TODO: This line of code loads data into the 'DataSet10.NewBill' table. You can move, or remove it, as needed.
            this.NewBillTableAdapter.Fill(this.DataSet10.NewBill,R );

            this.reportViewer1.RefreshReport();

            ReportParameter[] allPar = new ReportParameter[5]; // create parameters array
            ReportParameter parSu = new ReportParameter("Summation", label2.Text);
            ReportParameter parDiscount = new ReportParameter("Discount", label5.Text);
            ReportParameter parDisA = new ReportParameter("DiscountAmount", label3.Text);
            ReportParameter parTotal = new ReportParameter("Total", label4.Text);
            ReportParameter parCus = new ReportParameter("CustomerName", label1.Text);

            allPar[0] = parSu; //assign parameters to parameter array
            allPar[1] = parDiscount;
            allPar[2] = parTotal;
            allPar[3] = parDisA;
            allPar[4] = parCus;


            this.reportViewer1.LocalReport.SetParameters(allPar);
            this.reportViewer1.RefreshReport();
        }

AND THIS

    int R = Convert.ToInt32(label7.Text);
    SqlDataAdapter ADAP = new SqlDataAdapter("Select * from Newbill where Con = '" + R + "'", cn);
    DataSet DS = new DataSet();
    ADAP.Fill(DS, "Store");
    DataTable dt = new DataTable();
    dt.TableName = "Store";
    cn.Open();
    ADAP.Fill(dt);
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    ReportDataSource source = new ReportDataSource("Store", dt);
    reportViewer1.LocalReport.DataSources.Clear();
    reportViewer1.LocalReport.DataSources.Add(source);
    reportViewer1.DataBind();
    reportViewer1.LocalReport.Refresh();
    cn.Close();

it gives me :Error 'Microsoft.Reporting.WinForms.ReportViewer' does not contain a definition for 'DataBind' and no extension method 'DataBind' accepting a first argument of type 'Microsoft.Reporting.WinForms.ReportViewer' could be found

so what reference i need in using?

First of all create your rpt object then set datasource.
Follow below steps.

CrystalReport1 objRpt = new CrystalReport1();  //create your rpt object
objRpt.SetDataSource(Dataset.Tables["tablename"]);   //set datasource to your rpt.
cryRptViewer.ReportSource = objRpt;  //give rpt object to your crystal report viewer.

Well I will try to answer your question for both WinForms and ASP.NET as you didn't specify which you were using. The problem is that you are calling RefreshReport on an empty report... which returns exactly that; an empty report.

Win Forms Setting Datasource of Report

//First Create a `DataSource` for your Report:

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);   //This is what you are missing
this.reportViewer1.RefreshReport();

ASP.Net Setting Datasource of Report

//First Create a `DataSource` for your Report - Same as winforms

ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Name = "ReportData"; 
reportDataSource.Value = DS.Tables["Store"];

//Then bind the ReportViewer to that DataSource
this.reportViewer1.LocalReport.SetParameters(allPar);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.DataBind(); //This is the difference in ASP.Net   
this.reportViewer1.RefreshReport();

From your example, you can setup SqlConnection s, ReportParameter s and DataTable s all day long, but unless you bind your ReportViewer to the populated DataSet you will get nothing.

This is what I use for me when binding a local RDLC to the report viewer.

// create SqlConnection
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        myCommand.Connection = myConnection;
        SqlDataAdapter da = new SqlDataAdapter(myCommand);

        //get the data
        DataSet data = new DataSet();
        da.Fill(data);

        if (data != null && data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0)
        {
            ReportingServicesReportViewer.Visible = true;
            ltrStatus.Text = string.Empty;

            //provide local report information to viewer
            ReportingServicesReportViewer.LocalReport.ReportPath = Server.MapPath(Report.RDLCPath);

            //bind the report attributes and data to the reportviewer
            ReportDataSource rds = new ReportDataSource("DataSet1", data.Tables[0]);
            ReportingServicesReportViewer.LocalReport.DataSources.Clear();
            ReportingServicesReportViewer.LocalReport.DataSources.Add(rds);
            ReportingServicesReportViewer.LocalReport.Refresh();
        }
        else
        {
            ReportingServicesReportViewer.Visible = false;
            ltrStatus.Text = "No data to display.";
        }

I had problems when not giving the data table a name. Another thing you can check is the event viewer for maybe exceptions that the report viewer is having, but not displaying?

The problem is that the name of your DataTable (in code) does not match the name of your dataset (in the report). Your error gives the name that you should use. Try this:

private void LoadReport()
    {
        int R = Convert.ToInt32(label7.Text);

        this.reportViewer1.LocalReport.DataSources.Clear();
        DataTable dt = new DataTable();
        dt = this.NewBillTableAdapter.GetData(R);
        //the DataTable name MUST match the name of the corresponding dataset 
        //   (as it is named in the report)
        dt.TableName = "10_NewBill";

        ReportDataSource rprtDTSource = new ReportDataSource(dt.TableName, dt);

        this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
        this.reportViewer1.RefreshReport(); 

        //etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM