0

次のコードのいずれかを使用して、実行時にデータベースをCrystal Reportに接続しますが、ポストバックでデータベース接続が失われます(ツリービューをクリックするか、次のページに移動しようとすると)、これは取得したエラーです:
System.Runtime.InteropServices。 COMException:データベースのログオンに失敗しました。

            ReportDocument cryRpt = new ReportDocument();
            cryRpt.Load(path);

(((1)))

            for (int i = 0; i < cryRpt.DataSourceConnections.Count; i++)
                cryRpt.DataSourceConnections[i].SetConnection(ServerName, DatabaseName, UserID, Password);
            for (int i = 0; i < cryRpt.Subreports.Count; i++)
                for (int j = 0; j < cryRpt.Subreports[i].DataSourceConnections.Count; j++)
                    cryRpt.OpenSubreport(cryRpt.Subreports[i].Name).DataSourceConnections[j].SetConnection(ServerName, DatabaseName, UserID, Password);

                    cryRpt.OpenSubreport(cryRpt.Subreports[i].Name).DataSourceConnections[j].SetConnection(ServerName, DatabaseName, UserID, Password);

(((2)))

            crConnectionInfo.ServerName = ServerName;
            crConnectionInfo.DatabaseName = DatabaseName;
            crConnectionInfo.UserID = UserID;
            crConnectionInfo.Password = Password;
            crDatabase = cryRpt.Database;
            crTables = crDatabase.Tables;

            for (int i = 0; i < crTables.Count; i++)
            {
                crTable = crTables[i];
                crTableLogOnInfo = crTable.LogOnInfo;
                crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
                crTable.ApplyLogOnInfo(crTableLogOnInfo);
                //crTable.Location = crConnectionInfo.DatabaseName + ".dbo." + crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1) 
            }
4

1 に答える 1

1

ポストバックすると、CrystalReportViewer は有効な接続を持つレポートを失います。

セッションで cryRpt を保存する行を、既存のレポート読み込みコードに追加します。以下に従って、この結合されたコードを、最初のページの読み込み時にのみ実行されるルーチンに配置します。

if (!IsPostBack)
{
    // do all your normal code loading you did in your OP
    // add this line to store the ReportDocument
    Session["myReport"] = cryRpt;
    crReportViewer.ReportSource = cryRpt;
    crReportViewer.Show();
}

次に、このコードを else に配置して (ポスト バックするとき)、ReportDocument をセッションから取得し、CrystalReportViewer コントロールに再割り当てします。

else
{
    // this is when you postback
    // (i.e. paging, drilling into tree view, exporting, printing)        
    crReportViewer.ReportSource = (ReportDocument)Session["myReport"];
    crReportViewer.Show();
}
于 2012-04-24T03:57:15.543 に答える