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

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 http://www.experts-exchange.com/Database/Reporting_/Crystal_Reports/Q_22689905.html and the post by Hilfy here http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=589

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.
LVL 1
LandhoAsked:
Who is Participating?
 
LandhoConnect With a Mentor Author Commented:
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; 
            table.ApplyLogOnInfo(tableLogonInfo); 
        }
    }
    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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Report View</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <CR:CrystalReportViewer ID="Viewer" runat="server" AutoDataBind="True"
            ReportSourceID="CrystalReportSource1"  />
        <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
        </CR:CrystalReportSource>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
topdog770Commented:
When the refresh occurs, re-set the logon information.
0
 
CodeCruiserCommented:
Can you show us the code you are using in page load?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.