error while assigning a query to populate a reportView

0

Can I get a template to populate a Rpt without DATASET?

What I use is not working. follow the model I use.

private void Carregar()
{

DateTime dt1; dt1 = dtpickerInicial.Value;
            DateTime dt2; dt2 = dtpickerFinal.Value;

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet dsum = new DataSet();
        DataTable oTable = new DataTable();
        string strReportPath = "";

        try
        {
            strReportPath = @"rptRelatorioCaixa.rdlc";
            reportViewer1.LocalReport.ReportPath = strReportPath;
            SqlConnection conn = new SqlConnection(Properties.Settings.Default.BD_OneDrive );
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM CAIXA"; // Where dataSaida between @dtini and @dtfim ";

            cmd.CommandType = CommandType.Text;
            conn.Open();

            SqlDataReader oDataReader = cmd.ExecuteReader();
            oTable.Load(oDataReader);
            ReportDataSource myReportDataSource = new ReportDataSource(oTable);// da erro pois me cobra um dataSet
            reportViewer1.LocalReport.EnableExternalImages = true;
            //ReportParameter p = new ReportParameter("Titulo", "Resultado parcial do mes");
            //this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p });

            reportViewer1.Clear();
            if (reportViewer1.LocalReport.DataSources.Count > 0)
                reportViewer1.LocalReport.DataSources[0] = myReportDataSource;
            else
                reportViewer1.LocalReport.DataSources.Add(myReportDataSource);

            reportViewer1.RefreshReport();
    
asked by anonymous 16.11.2015 / 13:30

1 answer

0

I use the following method: in it I pass a dictionary, where the key is the name of the dataset (configured dataset in rdlc, not a Dataset of System.Data), and in the value of the dictionary is an IEnumerable, which can be any thing that implements IEnumerable as List. I use dictionary for more complex reports that need more than one data source.

        private void InicializarRelatorio(string nomeRelatorio, Dictionary<string, IEnumerable> dados)
        {
            //percorre o dicionario para adicionar as fontes de dados no relatorio
            foreach (var dado in dados)
            {
                ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = dado.Key; //nome do DataSet no .rdlc
                reportDataSource.Value = dado.Value; // objeto(lista) de dados
                reportViewer.LocalReport.DataSources.Add(reportDataSource);
            }

            //carrega o relatorio que deve estar na pasta do executavel. o arquivo rdlc deve estar CopyToLocal
            reportViewer.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + @"Relatorios\" + nomeRelatorio;
            reportViewer.RefreshReport();
        }
    
19.11.2015 / 17:00