Populate a reportviewer by codebehind

依然范特西╮ 提交于 2020-01-15 12:26:09

问题


Im using reportviewer with strongly dataset but now
I need to populate my reportviewer via codebehind. But I'm getting this error:
A data source instance has not been supplied for the data source 'ds_blabla'.
Here's my code to feed the dataset:

public DataSet PreencheDS(Relatorios rel)
  {
     DataSet retorno = new DataSet();
     try
       {
         string sql = @"SELECT proj.descricao AS projeto, func.descricao AS funcionalidade, clb.clube AS cliente, ch.descricao
         FROM MyTable ch 
         INNER JOIN table1 proj ON MyTable.projeto = table1.id 
         INNER JOIN table2 func ON MyTable.funcionalidade = table2.id 
         INNER JOIN table3 clb ON MyTable.clube = table3 .id
         WHERE (MyTable.responsavel = @responsavel) AND (MyTable.clube = @clube) AND (MyTable.dt_cadastro >= @dt_inicial) AND (MyTable.dt_cadastro <= @dt_final)";                
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add(new MySqlParameter("@responsavel", MySqlDbType.VarChar)).Value = rel.Responsavel_ID;
                cmd.Parameters.Add(new MySqlParameter("@clube", MySqlDbType.VarChar)).Value = rel.Clube_ID;
                cmd.Parameters.Add(new MySqlParameter("@dt_inicial", MySqlDbType.DateTime)).Value = rel.Dt_Inicial;
                cmd.Parameters.Add(new MySqlParameter("@dt_final", MySqlDbType.DateTime)).Value = rel.Dt_Final;
                retorno = _dal.Consultar(cmd);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return retorno;
        }  

And here's the page that contains my ReportViewer

protected void Page_Load(object sender, EventArgs e)
    {           
        if (!IsPostBack)
           {
              rel.Responsavel_ID = Convert.ToInt32(Session["usrValue"].ToString());
              rel.Clube_ID = Convert.ToInt32(Session["clube_id"].ToString());
              rel.Dt_Inicial = Session["dt_inicial"].ToString() + " 00:00:00";
              rel.Dt_Final = Session["dt_final"].ToString() + " 23:59:59";

              DataSet ds = _rel.PreencheDS(rel);
              ReportDataSource source = new ReportDataSource("MyTable", ds.Tables[0]);              
              this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
              this.ReportViewer1.LocalReport.DataSources.Clear();
              this.ReportViewer1.LocalReport.DataSources.Add(source);
              this.ReportViewer1.LocalReport.Refresh();
            }
        }  

I want to do it by codebehind instead of using strongly dataset because I need to pass parameters to my reportviewer. But these parameters they may vary... So I want to do it by codebehind.

UPDATE
I did like this. Now there's no message anymore, but nothing appears...


回答1:


In the following line, instead of MyTable, it should read ds_blabla:

ReportDataSource source = new ReportDataSource("MyTable", ds.Tables[0]);              


来源:https://stackoverflow.com/questions/15971400/populate-a-reportviewer-by-codebehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!