none
How to setparameter() based on the parameter's value (got by getParameter())

    Question

  • Hi all,

     

    I have a report with some parameters, one of them is a xml format string whose value dependes on the values of the rest parameters. The value of the rest parameters are queried from database tabels or views.

     

    Since I have to handle some possible special characters, I currently want to construct the value of the xml fromat parameter in my c# web page. I use serverReport.getparameter() to get the parameters from the report, then use these values to construct the xml format parameter, then write this parameter back to report through ServerReport.SetParameter().

     

    e.g.,

    report aprameters:  p1, p2, p3, XMLParam

     

    process I use:

    1. user select values(v1,v2,v3) for parameters(p1,p2,p3) on the report parameter zone

    2. web app get these values(v1,v2,v3) through serverreport.getparameter()

    3. manipulate the xml string (xmlString)  based on these values(v1,v2,v3)

    4. set the xml string(xmlString) back to report parameter XMLParam through ServerReport.SetParameter()

     

    But I can't get what I plan to get. After I use Server.Setparameter(), all the parameters  are reset to their default value but the value user chose.

     

    If I put Server.SetParameter() under (!ISPostBack){  }, the report parameter can get the value that user chooses each time, but  setparameter() will not have chance to be executed; if I put ServerReport.SetParameter() outside the (!IsPostBack){ }, all parameters will be reset to their default values no matter whether the user chooses new values for them.

     

    I believe someone know how to handle this issue, and I hope I can get help.

     

    Thanks,

     

    Jone

     

    mardi 1 mai 2007 21:38

Toutes les réponses

  • No body can help me?
    vendredi 4 mai 2007 13:35
  • I think the problem with the parameters being reset each time has to do with setting the server url or report path on every postback rather than the set parameters call.  If you are setting either ReportViewer.ServerReport.ReportServerUrl or ReportViewer.ServerReport.ReportPath on each post back, after viewstate is loaded, it might cause this behavior.  You might consider placing those property sets inside your if statement, or better yet, move to the Page_Init.
    mardi 8 mai 2007 20:02
    Modérateur
  • Hi Brian,

     

    Thanks for your reply.

     

    I tried to put the ReportViewer.ServerReport.ReportServerUrl or ReportViewer.ServerReport.ReportPath in the Page_init, but it still doesn't work. I copied my code here, and you may know what's my problem.

     

    protected void Page_Init(object sender, EventArgs e)

    {

    ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/reportserver");

    ReportViewer1.ServerReport.ReportPath = "/Reports/Report0001";

    }

     

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    string cnnstr = "Data Source=RINY\\PA2005;Initial Catalog=HOB";

    ReportParameter cnnStr = new ReportParameter("cnnStr", cnnstr);

    ReportViewer1.ServerReport.SetParameters(new ReportParameter[] { cnnStr});

     

    }

    else

    {

    ReportParameterInfoCollection rptUserChoice = ReportViewer1.ServerReport.GetParameters(); //new ReportParameterInfoCollection();

    string inputData = getParamsForMyRpt(rptUserChoice);

    ReportParameter inputdt = new ReportParameter("inputData", inputData);

    ReportViewer1.ServerReport.SetParameters(new ReportParameter[] { inputdt });

    }

     

    }

     

    I also Tried to put

    protected void Page_Init(object sender, EventArgs e)

    {

    if (!IsPostBack)

    {

    ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://localhost/reportserver");

    ReportViewer1.ServerReport.ReportPath = "/Reports/Report0001";

    }

     

     

    I still can't set back the inputData based on the user's choice. After I select a value from a parameter's dropdown list then "View The report", the parameter was reset to the default value, and both GetParameters() and Setparameters() use the default the parameter value not what I selected.

     

    Thanks,

     

    Jone

    jeudi 10 mai 2007 19:21
  • I think the problem is that you set the report parameter on every postback (the else clause in your Page_Load method).  When you set the parameter programmatically, the viewer will ignore input from the built-in parameters UI and instead repopulate it based on the new values from the server.  Can you instead call SetParameters only when you need to (i.e. only when the user changes the value)?
    mercredi 16 mai 2007 01:24
    Modérateur
  • May be somebody my post will help.
    I found only one way how to get current values from parameter area in report - using reflection.

    After receive report parameters, I'm set new values for one or several fields and send back.



    ReportViewer _rsViewer;

    Dictionary<string, ReportParameter> parameters = new Dictionary<string, ReportParameter>();

    parameters.Clear();

    object parametersArea =

    _rsViewer.GetType().GetField("m_parametersArea",

    BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_rsViewer);

    IDictionary paramPrompts = (IDictionary)

    parametersArea.GetType().GetField("m_paramPrompts",

    BindingFlags.Instance | BindingFlags.NonPublic).GetValue(parametersArea);



    foreach (object value in paramPrompts.Keys)

    {

    ReportParameterInfo pi = (ReportParameterInfo)

    value.GetType().GetProperty("ReportParameter").GetValue(value, null);

    string[] currentValue = (string[])

    value.GetType().GetProperty("CurrentValue").GetValue(value, null);

    ReportParameter p = new ReportParameter(pi.Name);

    p.Values.AddRange(currentValue);

    p.Visible = pi.Visible;

    parameters.Add(p.Name, p);

    }

    • Proposé comme réponse Evgeny Zyuzin lundi 23 février 2009 16:34
    lundi 23 février 2009 16:33
  • Just a quick thanks for Zyuzin! Your code sample solved a problem that had me pulling my hair out.

    -J-
    mardi 24 février 2009 07:19
  • Thanks, Zyuzin!  What a huge help.  I had to change the following line:

    parameters.Add(p.Name, p);

    to

    ReportViewer.ServerReport.SetParameters(new ReportParameter[] { p });

    mardi 5 janvier 2010 23:08