How to save a copy of the Report being rendered in ReportViewer Control

I had worked with a few customers in the past who had the following requirement.

  1. Using web ReportViewer control.
  2. Rendering a Server Report
  3. Providing an option to create a Copy of the existing report with some specified set of Parameters

First of all, we need to capture the selected parameters in a ReportParameterInfoCollection object. Here it is:

  protected void ReportViewer1_Load(object sender, EventArgs e)
    {        
        pm = ((Microsoft.Reporting.WebForms.ReportViewer)(sender)).ServerReport.GetParameters();
    }

Since, as per the requirement we need to make a copy of the Report, there are number of approaches to do this. Please refer: https://msdn.microsoft.com/en-us/library/aa237807(SQL.80).aspx 

But the main intent here is to copy the report with a desired name. For this, we use a Textbox to allow the user to enter the desired name.

In the Save button click we need to do the following:

  • Call the method, ReportingService2005.GetReportDefinition, and save the report with the above specified parameters. This report can be treated like a temporary file.
  • Upload the Report with the name we desired for, for this we use the method: ReportingService2005.CreateReport Method
 protected void Save_Click(object sender, EventArgs e)
    {        
        rs = new ReportingService2005();
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        string reportName = ReportViewer1.ServerReport.ReportPath;
        byte[] reportDefinition = null;
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        try
        {
     //Following code helps us Save a report as an RDL file
             reportDefinition = rs.GetReportDefinition(reportName);
              MemoryStream stream = new MemoryStream(reportDefinition);

             doc.Load(stream);
               doc.Save("C:\" + TextBox1.Text + "rdl"); 
  //Following code helps us upload the RDL file with the desired name. 
  Byte[] definition = null; 
  Warning[] warnings = null; 
  FileStream stream1 = File.OpenRead("C:\" + TextBox1.Text + "rdl"); 
  definition= new Byte[stream1.Length]; 
  stream1.Read(definition, 0, (int)stream1.Length); 
  stream1.Close(); 
  warnings = rs.CreateReport(TextBox1.Text, "/Path", true, definition, null);
                if (warnings != null)
               {
                   foreach (Warning warning in warnings)
                   {   
                            Console.WriteLine(warning.Message);
                 }
               }   
                SetReportParameters();
        }
        catch (SoapException ex)
        {
            //Enter exception details
        }
    }

Now, we need to set the parameters of the uploaded report to the one we had captured from the ReportViewer control.

 private void SetReportParameters()
    {
        rs = new ReportingService2005();
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        List<string> str = null;
        ReportParameter[] parameters = rs.GetReportParameters(@"/CascadingParamRepro/" + TextBox1.Text, null, false, null, null);
        if (parameters != null)
        {
            foreach (ReportParameter parameter in parameters)
            {
                foreach (Microsoft.Reporting.WebForms.ReportParameterInfo d in pm)
                {
                    str = new List<string>();
                    for (int i = 0; i < d.Values.Count; i++)
                    {
                        str.Add(d.Values[i].ToString());
                    }
                    if (d.Name == parameter.Name)
                    {
                        parameter.DefaultValues = str.ToArray();
                    }
                }
            }
            rs.SetReportParameters("/ReportPath/" + TextBox1.Text, parameters);
        }
   }

The above code snippet helps us Set the parameters to the desired values.

Hope this helps!!