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

后端 未结 2 586
遇见更好的自我
遇见更好的自我 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: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 = @"";
     var strenvelopes = "";
     GridItemCollection selectedRows;
     selectedRows = dgAddressList.SelectedItems;
    
    if (selectedRows.Count > 0)
    {
     foreach (GridItem item in dgAddressList.SelectedItems)
     {                 
      strenvelopes += @"
    " + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
    "; } } const string footer = @"
    "; 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 !

提交回复
热议问题