C#/ReportViewer: How to use parameters without specifying parameter names

Viewed 45

I am using a C# solution that uses the ReportViewer control to access our SSRS server reports.

I have been asked if it is possible to code the solution in a way that the parameter names are not specified. I am not sure how to ask this question to be honest.

Currently that solution is used by a handful of reports, so it is simple enough to hard code the parameter names, these parameters are query string parameters.

The solution gets the server URL from the web.config file:

// Add the Reporting Server URL
rvSiteMapping.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"].ToString());

The solution gets the report path from a query string parameters named = ReportPath and assigned to a report viewer report path property named ReportPath:

//Add the Report Path
rvSiteMapping.ServerReport.ReportPath = Request.QueryString["ReportPath"];

These two should remain like this.

What I need to change is the follwoing:

First report takes one parameter named MeetingID, Second report takes one parameter named RecordID, Third report takes an array with 3 parameters (each optional)

I specify the name of each parameter for each report. I am asking if there is a way to achieve the same but without having to specify parameter names, so the solution can be used for any report. Our reports take from 1 to some up to 5 parameters.

This is my current code, please don't be harsh on me, I am learning how to work this solution just now.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
            rvSiteMapping.Height = Unit.Pixel(Convert.ToInt32(700));
            rvSiteMapping.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            rvSiteMapping.ShowCredentialPrompts = false;

            if (ConfigurationManager.AppSettings["UseCredentials"].ToString().ToUpper() == "TRUE")
            {
                rvSiteMapping.ServerReport.ReportServerCredentials = new ReportCredential(
                    ConfigurationManager.AppSettings["UserName"].ToString(),
                    ConfigurationManager.AppSettings["UserPassword"].ToString(),
                    ConfigurationManager.AppSettings["UserNameDomain"].ToString()
                    );
            }

            // Add the Reporting Server URL
            rvSiteMapping.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"].ToString());
            
            //Add the Report Path
            rvSiteMapping.ServerReport.ReportPath = Request.QueryString["ReportPath"];

                        
            if (Request.QueryString["MeetingID"] != null)
            {
                Microsoft.Reporting.WebForms.ReportParameter[] parameters = new Microsoft.Reporting.WebForms.ReportParameter[1];
                parameters[0] = new Microsoft.Reporting.WebForms.ReportParameter("MeetingID", Request.QueryString["MeetingID"]);
                rvSiteMapping.ServerReport.SetParameters(parameters);
            }                    
            else if (Request.QueryString["RecordID"] != null)
            {
                Microsoft.Reporting.WebForms.ReportParameter[] parameters = new Microsoft.Reporting.WebForms.ReportParameter[1];
                parameters[0] = new Microsoft.Reporting.WebForms.ReportParameter("RecordID", Request.QueryString["RecordID"]);
                rvSiteMapping.ServerReport.SetParameters(parameters);

            }
            else if (Request.QueryString.Count > 2)
            {
                Microsoft.Reporting.WebForms.ReportParameter[] parameters = new Microsoft.Reporting.WebForms.ReportParameter[3];
                parameters[0] = new Microsoft.Reporting.WebForms.ReportParameter("StreetNumber", Request.QueryString["StreetNumber"]);
                parameters[1] = new Microsoft.Reporting.WebForms.ReportParameter("StreetName", Request.QueryString["StreetName"]);
                parameters[2] = new Microsoft.Reporting.WebForms.ReportParameter("District", Request.QueryString["District"]);
                rvSiteMapping.ServerReport.SetParameters(parameters);
            }
            rvSiteMapping.ServerReport.Refresh();
        }
        catch (Exception ex)
        {
            Console.WriteLine("General exception. {0}", ex.Message);
        }
    }
}

Thank you in advance for any help, or ideas you might be able and willing to share.

Erasmo

0 Answers
Related