Showing posts with label SSRS Report. Show all posts
Showing posts with label SSRS Report. Show all posts

December 16, 2016

SSRS: How to open a linked SSRS Report with parameter in popup

Last month working with SSRS related project, requirement was to open another report on click of date field. Basically, when user clicks on date field in main SSRS report, it should open another report in pop up.

Approach: To achieve the requirement, I've used JavaScript function. Here are the steps , how I have done it !
1. Click on Textbox Property of Date field.
2. Click on Action , Select Go to URL.
3. Click on Expression and apply a code as below.

 ="javascript:void(window.open('" & Cstr(Parameters!BaseUrl.Value) & "?ScheduleId=" & Cstr(Fields!BS_ID.Value) & "','blank','toolbar=no, menubar=no,scrollbars=no, resizable=no, location=no, addressbar=no, left=100,top=100,height=350,width=500'))"  
 
blog16.png

blog17.png

Now, I'll explain you how we can pass BaseURL and other fields as a parameter to generate other SSRS Report. I have a Page named "AdminReport.aspx" which has following code.

 <rsweb:ReportViewer ID="rptViewer" runat="server" Height="500px" Style="-ms-overflow-y: scroll" Width="1100px" ShowToolBar="False" ShowParameterPrompts="False" ShowCredentialPrompts="False"></rsweb:ReportViewer>   

In Code behind of AdminReport Page, I've used below code to bind a second report:
 private void ShowReport()  
     {  
       try  
       {  
         string urlReportServer = ConfigurationManager.AppSettings["ReportViewerUrl"]; //It is a URL Of Report Server.
         rptViewer.ProcessingMode = ProcessingMode.Remote; // Processing Mode will be either Remote or Local.
         rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer); //Set the Report Server URL.
         rptViewer.ServerReport.ReportPath = "/AdminReport"; //Set path of Report Page.
         //Create an Array List to combine the Parameters which are to be passed into SSRS Report.  
         ArrayList reportParam = new ArrayList();  
         reportParam = ReportDefaultParam();  
         ReportParameter[] param = new ReportParameter[reportParam.Count];  
         for (int k = 0; k < reportParam.Count; k++)  
         {  
           param[k] = (ReportParameter)reportParam[k];  
         }  
         rptViewer.ServerReport.ReportServerCredentials = new CustomReportServerCredentials();  // Set Credentials.
         rptViewer.ServerReport.SetParameters(param); //Set Report Parameters  
         rptViewer.ServerReport.Refresh();  
       }  
       catch (Exception ex)  
       {  
         CommonUtilities.WriteEventToLogFile(CommonUtilities.GetCurrentTime() + ":AdminViewReport : ShowReport : " + ex.ToString(), "", "", "");  
         // throw ex;  
       }  
     }  
     /// <summary>  
     /// Pass default parameters to the report  
     /// </summary>  
     /// <returns></returns>  
     private ArrayList ReportDefaultParam()  
     {  
       ArrayList arrLstDefaultParam = new ArrayList();  
       string strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;  
       string strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");  
       string baseUrl = strUrl + "ScheduleMaterialReport.aspx";  
       arrLstDefaultParam.Add(CreateReportParameter("BaseUrl", baseUrl));  
       return arrLstDefaultParam;  
     }  
     /// <summary>  
     /// Create report using parameters  
     /// </summary>  
     /// <param name="paramName"></param>  
     /// <param name="pramValue"></param>  
     /// <returns></returns>  
     private ReportParameter CreateReportParameter(string paramName, string pramValue)  
     {  
       ReportParameter aParam = new ReportParameter(paramName, pramValue);  
       return aParam;  
     }  
  • BaseURL - is URL of the second report page, "/SubReport.aspx").
  • BS_ID.value - is the ID (value) of selected record in main Report.
  • ScheduledId - is the field passed as query string parameter to second SSRS report.
We pass this value from main report to other report as query string parameter. E.g.: http://siteurl/SubReport.aspx?ScheduleId=6
 int ScheduleId =Convert.ToInt32(Request.QueryString["ScheduleId"]))  

Now, when user clicks on Date field in Main Report ("AdminReport.aspx" page), other report will open in "SubReport.aspx" page as pop up.

Hope this would be helpful!! If you have any questions you can reach out our SharePoint Consulting team here.

November 23, 2016

Default Print button in SSRS Report is not showing/working in Chrome and Firefox browsers

Scenario
SSRS toolbox provides Print functionality. But it's browser dependent. It works fine only with Internet Explorer browser, not compatible with Firefox and Chrome browsers.

Resolution
To achieve this, we've to use custom Print button and JavaScript code which executes on button click.

HTML code for Print button and Report viewer:
 <asp:Button runat="server" CssClass="btn-addschedule-bot" Style="margin-left: 10px;" ID="btnPrint" CausesValidation="true" ValidationGroup="vgSubmit" OnClientClick="printReportClick();" Text="Print Report" />  
 <div style="border: 1px solid #A7B0E8; margin: 0px 10px; padding: 5px; float: left;">  
 <rsweb:ReportViewer ID="rptViewer" runat="server" Height="500px" Style="-ms-overflow-y: scroll" Width="1100px" ShowToolBar="False" ShowParameterPrompts="False" ShowCredentialPrompts="False"></rsweb:ReportViewer>  
 </div>  

JavaScript Code to print a report in Chrome and Firefox:
 <script type="text/javascript">  
     function printReport(report_ID) {  
       var rv1 = $('#' + report_ID);  
       var iDoc = rv1.parents('html');  
       // Reading the report styles  
       var styles = iDoc.find("head style[id$='ReportControl_styles']").html();  
       if ((styles == undefined) || (styles == '')) {  
         iDoc.find('head script').each(function () {  
           var cnt = $(this).html();  
           var p1 = cnt.indexOf('ReportStyles":"');  
           if (p1 > 0) {  
             p1 += 15;  
             var p2 = cnt.indexOf('"', p1);  
             styles = cnt.substr(p1, p2 - p1);  
           }  
         });  
       }  
       if (styles == '') { alert("Cannot generate styles, Displaying without styles.."); }  
       styles = '<style type="text/css">' + styles + "</style>";  
       //--- Reading the report html  
       var table = rv1.find("div[id$='_oReportDiv']");  
       if (table == undefined) {  
         alert("Report source not found.");  
         return;  
       }  
       //-- Generating a copy of the report in a new window  
       var docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">';  
       var docCnt = styles + table.parent().html();  
       var docHead = '<head><title>Printing ...</title><style>body{margin:5;padding:0;}</style></head>';  
       var winAttr = "location=yes, statusbar=no, directories=no, menubar=no, titlebar=no, toolbar=no, dependent=no, width=720, height=600, resizable=yes, screenX=200, screenY=200, personalbar=no, scrollbars=yes";;  
       var newWin = window.open("", "_blank", winAttr);  
       writeDoc = newWin.document;  
       writeDoc.open();  
       writeDoc.write(docType + '<html>' + docHead + '<body onload="window.print();">' + docCnt + '</body></html>');  
       writeDoc.close();  
       // The print event will fire as soon as the window loads  
       newWin.focus();  
       // uncomment to autoclose the preview window when printing is confirmed or canceled.  
       // newWin.close();  
     };  
     function printReportClick() {  
       printReport('<%=rptViewer.ClientID %>');  
     }  
   </script>  

Print Preview in Chrome browser:



I hope this will help you out to make print functionality working in Chrome and Firefox.

If you have any questions you can reach out our SharePoint Consulting team here.