Pages

Men

rh

4/14/2014

How to call SSRS Report from .Net using Asp.net with C#

Name Spaces:

using System.Configuration;
using System.Net;
using Microsoft.Reporting.WebForms;
using System.Security.Principal;



Code in Asp.Net:-

ReportViewer1.ServerReport.ReportServerUrl = new Uri(System.Configuration.ConfigurationManager.AppSettings["ReportServerURL"].ToString());
                    ReportViewer1.ServerReport.ReportPath = "/" + System.Configuration.ConfigurationManager.AppSettings["ReportFolderName"].ToString() + "/Report1";
                    ReportParameter[] myparam = new ReportParameter[2];
                    myparam[0] = new ReportParameter("UserId", Session["UserID"].ToString().Trim());
                    myparam[1] = new ReportParameter("Role", Session["RoleId"].ToString().Trim());
                    ReportViewer1.ServerReport.SetParameters(myparam);
                    ReportViewer1.ServerReport.Refresh();





  protected void Page_Init(object sender, EventArgs e)
        {
            ReportViewer1.ServerReport.ReportServerCredentials =
                new MyReportServerCredentials();
        }


        [Serializable]
        public sealed class MyReportServerCredentials :
            IReportServerCredentials
        {
            public WindowsIdentity ImpersonationUser
            {
                get
                {
                    // Use the default Windows user.  Credentials will be
                    // provided by the NetworkCredentials property.
                    return null;
                }
            }


            public ICredentials NetworkCredentials
            {
                get
                {
                    // Read the user information from the Web.config file. 
                    // By reading the information on demand instead of
                    // storing it, the credentials will not be stored in
                    // session, reducing the vulnerable surface area to the
                    // Web.config file, which can be secured with an ACL.

                    // User name
                    string userName =
                        ConfigurationManager.AppSettings
                            ["MyReportViewerUser"];

                    if (string.IsNullOrEmpty(userName))
                        throw new Exception(
                            "Missing user name from web.config file");

                    // Password
                    string password =
                        ConfigurationManager.AppSettings
                            ["MyReportViewerPassword"];

                    if (string.IsNullOrEmpty(password))
                        throw new Exception(
                            "Missing password from web.config file");

                    // Domain
                    string domain =
                        ConfigurationManager.AppSettings
                            ["MyReportViewerDomain"];

                    if (string.IsNullOrEmpty(domain))
                        throw new Exception(
                            "Missing domain from web.config file");

                    return new NetworkCredential(userName, password, domain);
                }
            }


            public bool GetFormsCredentials(out Cookie authCookie,
                        out string userName, out string password,
                        out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;

                // Not using form credentials
                return false;
            }
        }


-------------

In Web.config file, we  have to add below Code:-

   <add key="ReportServerURL" value="http://Your Report Server Name" />
    <add key="ReportFolderName" value="Report Folder Name in the report server" />
    <add key="MyReportViewerUser" value="UserId" />
    <add key="MyReportViewerPassword" value="Password" />
    <add key="MyReportViewerDomain" value="Your DomainName" />



No comments :

Post a Comment