Pregunta

I am trying to View a report dynamically from code behind. But when the parameters are changed from dynamic textboxes added in the page. in the report refresh() the data is not changed.

I call sqlDS() and reportBuild() in the !IsPostback.

This method is for defining the sqlDatasource:

    protected void sqlDS()
    {
        string conString, prName = "";
        int counter = 0;
        Reporting rep = new Reporting();
        rep = rep.searchReport(repID_HF.Value);

        Reporting repFold = new Reporting();
        repFold = repFold.searchFolder(foldID_HF.Value);

        if (repFold.FolderName.Split('(')[1] == "Web Reports)")
        {
            conString = dbSql.connectionStringAll;
            prName = dbSql.providerName;
        }
        else
        {
            conString = db.connectionStringAll;
            prName = db.providerName;
        }
        SqlDataSource1.ConnectionString = conString;
        SqlDataSource1.ProviderName = prName;

        string sqlString = System.IO.File.ReadAllText(Server.MapPath("~/Reports/SQLs/" + rep.SqlFile));
        sqlString.Replace(System.Environment.NewLine, " ");


        SqlDataSource1.SelectCommand = sqlString;

        SqlDataSource1.CancelSelectOnNullParameter = false;

        Reporting repParam = new Reporting();

        allPs = repParam.getAllParamRep(rep.RepID);


        foreach (Reporting itemParam in allPs)
        {
            if (itemParam.ParamType == "Date")
            {
                SqlDataSource1.SelectParameters.Add(":" + itemParam.ParamName, itemParam.ParamDefaultValue);
                counter++;
            }
            else if (itemParam.ParamType == "Text")
            {
                SqlDataSource1.SelectParameters.Add(":" + itemParam.ParamName, itemParam.ParamDefaultValue);
                counter++;
            }
            else if (itemParam.ParamType == "Menu")
            {
                counter++;
            }
        }
    }

This method is for declaring the report properties:

    protected void reportBuild()
    {
        Reporting rep2 = new Reporting();
        rep2 = rep2.searchReport(repID_HF.Value);

        ReportViewer1.LocalReport.ReportPath = "Reports/RDLC/" + rep2.RdlcFile;
        this.ReportViewer1.LocalReport.ReportEmbeddedResource = rep2.RdlcFile;

        ReportParameter[] paramss = new ReportParameter[SqlDataSource1.SelectParameters.Count];

        for (int i = 0; i < SqlDataSource1.SelectParameters.Count; i++)
        {
            paramss[i] = new ReportParameter(SqlDataSource1.SelectParameters[i].Name.Split(':')[1], SqlDataSource1.SelectParameters[i].DefaultValue);
        }
            ReportDataSource rds = new ReportDataSource(rep2.DatasetName.Split('.')[0], SqlDataSource1);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);

        //paramss[0] = new ReportParameter("TDATE", SqlDataSource1.SelectParameters[0].DefaultValue);
        //paramss[1] = new ReportParameter("CUST_NUM", SqlDataSource1.SelectParameters[1].DefaultValue);
        ReportViewer1.LocalReport.SetParameters(paramss);

        ReportViewer1.LocalReport.Refresh();

    }

In the reportViewer refresh method i try to set the new parameters according to the dynamic textboxes added in the page:

    protected void ReportViewer1_ReportRefresh(object sender, System.ComponentModel.CancelEventArgs e)
    {
        foreach (Control txt in Panel1.Controls)
        {
            if (txt is TextBox)
            {
                txts.Add(txt);
            }
        }

        foreach (TextBox txtbox in txts)
        {

            Reporting repP = new Reporting();
            repP = repP.searchParam(txtbox.Attributes["pID"].ToString());
            if (repP.ParamType == "Date")
            {
                SqlDataSource1.SelectParameters[":" + repP.ParamName].DefaultValue = txtbox.Text;
            }
            else if (repP.ParamType == "Text")
            {
                SqlDataSource1.SelectParameters[":" + repP.ParamName].DefaultValue = txtbox.Text;
            }
        }

        //Reporting r = new Reporting();
        //r = r.searchReport(repID_HF.Value);


        //Reporting rep = new Reporting();
        //rep = rep.searchReport(repID_HF.Value);

        //ReportDataSource rds = new ReportDataSource(rep.DatasetName.Split('.')[0], SqlDataSource1);
        //this.ReportViewer1.Reset();
        //ReportViewer1.LocalReport.DataSources.Clear();
        //ReportViewer1.LocalReport.DataSources.Add(rds);

        ReportParameterInfoCollection x = ReportViewer1.LocalReport.GetParameters();
        //Response.Redirect(Request.RawUrl);
        ReportViewer1.LocalReport.Refresh();

    }

I tried debugging and found every thing is working correctly the SQL parameters changed, the Report Parameters also is changed.

so why the data in the report is not changed? Plz help me

¿Fue útil?

Solución

I got a better and easier way to solve this problem using this link

http://www.igregor.net/post/2007/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

And you can use array of strings to pass attributes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top