Pass data to ssrs report (.rdl) from code behind in ASP.NET C#, ProcessingMode=Remote

后端 未结 2 585
遇见更好的自我
遇见更好的自我 2021-01-07 08:30

I can see that there are many questions already being asked regarding the same thing like:

Binding a datasource to a rdl in report server programmatically - SSRS

相关标签:
2条回答
  • 2021-01-07 09:17

    Most likely, you may wind up needing to run them in the context of the web application, and pull the data from the datasource into the report by hand.

    I did this project a few years ago, and wouldn't mind getting it back up and running, and finally completed to support MVC/Web Forms/Win Forms/WPF/etc. as well as get it more robust.

    This may get you on the right track.

    0 讨论(0)
  • 2021-01-07 09:24

    Finally we implemented it like the following . I thought may be it is of some use to others. So am posting my solution:
    This can be done with the implementation of XML
    First convert your data into XML, like following:

    private void ConvertToXml(ref XmlDocument xm)
    {
     const string header = @"<ENVELOPE>";
     var strenvelopes = "";
     GridItemCollection selectedRows;
     selectedRows = dgAddressList.SelectedItems;
    
    if (selectedRows.Count > 0)
    {
     foreach (GridItem item in dgAddressList.SelectedItems)
     {                 
      strenvelopes += @"<ADDRESS>" + 
      "<NAME>" + "<![CDATA[" + ((object[])(item.DataItem))[2].ToString() + "]]>" + "</NAME>" +
      "<CONTACT>" + "<![CDATA[" + ((object[])(item.DataItem))[1].ToString() + "]]>" + "</CONTACT>" +
      "<STREET>" + "<![CDATA[" + ((object[])(item.DataItem))[3].ToString() + "]]>" + "</STREET>" +
      "<SUBURBSTATE>" + "<![CDATA[" + ((object[])(item.DataItem))[4].ToString() + "]]>" + "</SUBURBSTATE>" +
      "</ADDRESS>";                  
      }
    }
    
       const string footer = @"</ENVELOPE>";  
    
       var envelopes = header + strenvelopes + footer;
       xm.LoadXml(envelopes);
    }
    

    You need to replace ((object[])(item.DataItem))[].ToString() with your own values
    Then generate your report like following :

    private void GenerateReport()
    {
           var xm = new XmlDocument();
           ConvertToXml(ref xm);
           var xml = xm.InnerXml.ToString();
           rptViewer = ucrvEnvelope.FindControl("rptViewer") as ReportViewer;
           if (rptViewer != null)
           {
               rptViewer.ProcessingMode = ProcessingMode.Remote;              
               rptViewer.ServerReport.ReportServerUrl = new Uri("http://localhost/MyReports");
               rptViewer.ServerReport.ReportPath = "/Reporting/rptEnvelope";
               rptViewer.PromptAreaCollapsed = true;
           }
            ReportParameter myParam = new ReportParameter("list", xml);
            rptViewer.ServerReport.SetParameters(new ReportParameter[] { myParam });
            rptViewer.ShowParameterPrompts = false;
            rptViewer.ShowBackButton = true;
            rptViewer.ServerReport.Refresh();
      }
    

    You should add a Parameter "list" to your report. because we will be passing whole XML thing to this "list" through the following Dataset,

    Now the Dataset that will handle this XML:
    Normally, what we do is you write some stored procedure or write some query inside the dataset, since ours is the XML, it needs to be handled XML way,
    With the Query Type: Text write the following query to handle the XML data

    DECLARE @docHandle int DECLARE @xmlDocument varchar(max); DECLARE @listXML nvarchar(max)
    SET @listXML = @list
    SET @xmlDocument = @listXML EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument
        SELECT * FROM OPENXML (@docHandle, N'/ENVELOPE/ADDRESS') WITH
        (
          NAME nvarchar(max) 'NAME',
          CONTACT nvarchar(max) 'CONTACT',
          STREET nvarchar(max) 'STREET',
          SUBURBSTATE nvarchar(max) 'SUBURBSTATE'
        )
    

    The Fields NAME, CONTACT etc. should be the same with what you created the XML document in the first place, at the top ConvertToXML

    So the bottom line is, you are not passing data to report through strored procedure from DB but you are actaully grabbing data from aspx page - Code behind and converting the same to XML and passing it to your report-server report via this Dataset
    So whats in the code behind is passed to your report.. voila !

    0 讨论(0)
提交回复
热议问题