1
votes

I have some problems when I run my app to display a ReportViewer. This is my C# code: enter image description here

private void Report_Load(object sender, EventArgs e)
            {
                ShowReport();
            }
            private DataTable GetData()
            {

                DateTime dtStart = DateTime.Parse(txtDataS.Text);
                DateTime dtEnd = DateTime.Parse(txtDataE.Text);
                DataTable _dt = new DataTable();
                using (_con = new SqlConnection(_strConexao))
                {
                _cmd = new SqlCommand("SELECT_CADS", _con);
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Parameters.Add("@SDATE", SqlDbType.DateTime).Value = dtStart;
                _cmd.Parameters.Add("@EDATE", SqlDbType.DateTime).Value = dtEnd;
                _adp = new SqlDataAdapter(_cmd);
                _adp.Fill(_dt);

            }
            return _dt;
        }

        private void ShowReport()
        {
            _reportViewer.Reset();
            DataTable _dt = GetData();
            ReportDataSource rds = new ReportDataSource("DataSetRel",_dt);
            _reportViewer.LocalReport.DataSources.Add(rds);
            _reportViewer.LocalReport.ReportEmbeddedResource = "Project.Rel.rdlc";
            _reportViewer.ProcessingMode = ProcessingMode.Local;

            ReportParameter dt1 = new ReportParameter("dtStart", txtDataS.Text);
            ReportParameter dt2 = new ReportParameter("dtEnd", txtDataE.Text);

            _reportViewer.LocalReport.SetParameters(new ReportParameter[] {dt1,dt2});
            _reportViewer.LocalReport.Refresh(); 
        }

When I run this app and I input Initial Date and final date and click in show button nothing happened Note in my rdlc report i have two parameters dtStart and dtEnd and i use it as a expression in textbox. Whats is the problem? Why I can't pass this parameters to rdlc report?

1
When you use parametric SQL query you don't need use parameters in report. - Reza Aghaei
@RezaAghaei, in this case how can I do for display a inputed from date and to date in my report viewer? - Wesley Heron
@RezaAghaei not. In my report i need get fromDate and ToDate to display in textbox on rdlc report also i need to query in database. - Wesley Heron
@RezaAghaei it must be inside showReport() method? - Wesley Heron
@RezaAghaei, in rdlc i do not must declare parameters? - Wesley Heron

1 Answers

1
votes

Create a form and put a ReportViewr on it, then in form load event write such code:

var reportBindingSource = new System.Windows.Forms.BindingSource();

var reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
//Name of dataset in your rdlc report
reportDataSource.Name = "DataSet1";
reportDataSource.Value = reportBindingSource;

this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "StackSamplesCS.Data.Report1.rdlc";

//Set parameters
//These are repot parameters, so use the names that you gave them in report.
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("StartDate", this.DatePicker1.SelectedDate.ToString()));
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("EndDate", this.DatePicker1.SelectedDate.ToString()));

//put your connection string
//example: @"data source=(localdb)\v11.0;initial catalog=YourDatabase;integrated security=True;"
//example @".\sqlexpress;;initial catalog=YourDatabase;integrated security=True;"
var connection = W"Your Connection String" ;

//your command
//example: "SELECT * FROM YourTable WHERE StartDate>@StartDate AND EndDate<@EndDate"
var command = "Your Command";

var tableAdapter = new System.Data.SqlClient.SqlDataAdapter(command, connection);

//Set Sql Parameters
tableAdapter.SelectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@StartDate", this.DatePicker1.SelectedDate));
tableAdapter.SelectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EndDate", DatePicker2.SelectedDate));
var dataTable= new DataTable();
//Get data
tableAdapter.Fill(dataTable);

reportBindingSource.DataSource = dataTable;

this.reportViewer1.RefreshReport();
  • Please Carefully read comments
  • Look in your Report and see what is the name of DataSet that it use
  • Look in Your Report and see what is the name of parameters in report and use them when passing report parameters.
  • Use parameters in your code not in Report