Crystal Reports 2008 .NET Viewer prompts for db password on page refresh

Posted on 2009-04-16
Last Modified: 2012-05-06
As an alternative to the Crystal Reports ActiveX viewer on IIS, we ended up using a solution which was a mixture of this EE post and the post by Hilfy here

Basically, we store the cr datasource username, password, and server in web.config - and on page load the report name is in the query string of the URL.  Works PERFECTLY and we are very happy with it.

However, when we attempt to refresh the page it prompts for the database password again.
I've been scouring for several hours to figure out why this happens... any idea?  I tried moving to Page_Init instead of Page_Load, same issue.  I was wondering if there was something specific I was missing.
Question by:Landho
    LVL 12

    Expert Comment

    When the refresh occurs, re-set the logon information.
    LVL 83

    Expert Comment

    Can you show us the code you are using in page load?
    LVL 1

    Accepted Solution

    code is below... some of the config is stored in web.config, could easily be changed so you pass it on page load or grab it from someplace else.
    using System;
    using System.Data;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Web;
    using CrystalDecisions.Shared;
    using CrystalDecisions.ReportSource;
    using System.Diagnostics;
    public partial class _Default : System.Web.UI.Page 
        protected string reportPath = ConfigurationManager.AppSettings["Rptpath"].ToString();
        protected string dbName = ConfigurationManager.AppSettings["dbName"].ToString();
        protected string dbUser = ConfigurationManager.AppSettings["dbUser"].ToString();
        protected string dbPass = ConfigurationManager.AppSettings["dbPass"].ToString();
        protected string serverName = ConfigurationManager.AppSettings["serverName"].ToString();
        protected void Page_Load(object sender, EventArgs e)
            CrystalDecisions.CrystalReports.Engine.ReportDocument crReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            string reportName = Request.QueryString["ReportName"];
            string qServer = serverName;
            string rptPath = reportPath;
            crReport.Load(rptPath + reportName);
            ParameterDiscreteValue discreteVal = new ParameterDiscreteValue(); 
            ParameterRangeValue rangeVal = new ParameterRangeValue(); 
            ParameterValues curvalues = new ParameterValues(); 
            foreach (ParameterFieldDefinition parafld in crReport.DataDefinition.ParameterFields) 
                if (parafld.DiscreteOrRangeKind.ToString() == "DiscreteValue") 
                    discreteVal.Value = Request.QueryString[parafld.ParameterFieldName]; 
                if (discreteVal.Value != null) 
                { curvalues.Add(discreteVal); parafld.ApplyCurrentValues(curvalues); 
            CrystalDecisions.Shared.ConnectionInfo connectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
            connectionInfo.ServerName = serverName;
            connectionInfo.UserID = dbUser;
            connectionInfo.Password = dbPass;
            // set report connection for main report    
            SetDBLogonForReport(connectionInfo, crReport, qServer);    
            // set report connection for any subreports    
            SetDBLogonForSubreports(connectionInfo, crReport, qServer);    
            // set group tree function off             
            //CrystalReportViewer.DisplayGroupTree = false;    
            // view report
            Viewer.ReportSource = crReport;
        private void SetDBLogonForReport(CrystalDecisions.Shared.ConnectionInfo connectionInfo, ReportDocument reportDocument, string qServer)
            Tables tables = reportDocument.Database.Tables; 
            foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables) 
            { TableLogOnInfo tableLogonInfo = table.LogOnInfo; 
                tableLogonInfo.ConnectionInfo = connectionInfo; 
        private void SetDBLogonForSubreports(CrystalDecisions.Shared.ConnectionInfo connectionInfo, ReportDocument reportDocument, string qServer)
            Sections sections = reportDocument.ReportDefinition.Sections; 
            foreach (Section section in sections) 
            { ReportObjects reportObjects = section.ReportObjects; 
                foreach (ReportObject reportObject in reportObjects) 
                    if (reportObject.Kind == ReportObjectKind.SubreportObject) 
                        SubreportObject subreportObject = (SubreportObject)reportObject; 
                        ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName); 
                        SetDBLogonForReport(connectionInfo, subReportDocument, qServer); 
    and the aspx page
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register assembly="CrystalDecisions.Web, Version=12.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <title>Report View</title>
        <form id="form1" runat="server">
            <CR:CrystalReportViewer ID="Viewer" runat="server" AutoDataBind="True"
                ReportSourceID="CrystalReportSource1"  />
            <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">

    Open in new window


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now