我完全失去了這一個 -如何動態分配報告和連接信息到水晶報告
我有一個自行運行良好的.RPT文件。此報告文件需要4個參數並連接到Oracle數據庫。我已經正確設置了所有數據庫代碼。所以我認爲。
我不能爲我的生活連接這個報告文件的連接,並在我的ASP.NET網頁上查看報告。
當我運行這段代碼時,無論如何,我得到了crystalreportviewer控件所在的文本無效指針。我在ASP.NET 3.5項目中運行Crystal Reports 2008 SP2。我向那些建議鏈接到查看報告的全面方法(包括指定參數,連接信息和處理報告文檔)的人員開放,看看我是如何全新的。
我需要這個過程是動態的,因爲今後我會動態地設置報表文件,參數,數據庫信息,等等
謝謝大家提前。
我的代碼背後:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
namespace AMR
{
public partial class RptParameter2 : System.Web.UI.Page
{
ReportDocument crReportDocument;
ConnectionInfo connectionInfo;
public enum ReportState { NotSet, FromStart, FromSession,FromPostBack };
public void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack && !Page.IsCallback)
{
LoadReport(ReportState.FromStart);
}
else
{
LoadReport(ReportState.FromPostBack);
}
}
public void LoadReport(ReportState rptState)
{
if (rptState != ReportState.FromPostBack)
{
if (crReportDocument != null)
{
crReportDocument.Close();
crReportDocument.Dispose();
crReportDocument = null;
}
crReportDocument = new ReportDocument();
if (rptState == ReportState.FromSession)
crReportDocument = (ReportDocument)Session["cr"];
connectionInfo = new ConnectionInfo();
ParameterField paramField1 = new ParameterField();
ParameterField paramField2 = new ParameterField();
ParameterField paramField3 = new ParameterField();
ParameterField paramField4 = new ParameterField();
ParameterFields paramFields = new ParameterFields();
ParameterDiscreteValue paramDiscreteValue1 = new ParameterDiscreteValue();
ParameterDiscreteValue paramDiscreteValue2 = new ParameterDiscreteValue();
ParameterDiscreteValue paramDiscreteValue3 = new ParameterDiscreteValue();
ParameterDiscreteValue paramDiscreteValue4 = new ParameterDiscreteValue();
//p1
paramField1.Name = "Siebel Position";
paramDiscreteValue1.Value = txtAvailPos.Text;
paramField1.CurrentValues.Add(paramDiscreteValue1);
paramFields.Add(paramField1);
//p2
paramField2.Name = "DETAIL_LEVEL";
paramDiscreteValue2.Value = ddlDetailLvl.SelectedValue;
paramField2.CurrentValues.Add(paramDiscreteValue2);
paramFields.Add(paramField2);
//p3
paramField3.Name = "RPT_MONTH";
paramDiscreteValue3.Value = ddlMonth.SelectedValue;
paramField3.CurrentValues.Add(paramDiscreteValue3);
paramFields.Add(paramField3);
//p4
paramField4.Name = "RPT_YEAR";
paramDiscreteValue4.Value = txtYear.Text;
paramField4.CurrentValues.Add(paramDiscreteValue4);
paramFields.Add(paramField4);
//loading
CrystalReportViewer1.ReportSource = crReportDocument;
crReportDocument.Load(Server.MapPath("~/myreport.rpt"));
connectionInfo.ServerName = "myserver";
connectionInfo.DatabaseName = System.Configuration.ConfigurationSettings.AppSettings["Crystal_Database"];
connectionInfo.UserID = System.Configuration.ConfigurationSettings.AppSettings["Crystal_Username"];
connectionInfo.Password = System.Configuration.ConfigurationSettings.AppSettings["Crystal_Password"];
SetDBLogonForReport(connectionInfo);
CrystalReportViewer1.ParameterFieldInfo = paramFields;
CrystalReportViewer1.EnableDatabaseLogonPrompt = false;
CrystalReportViewer1.EnableParameterPrompt = false;
CrystalReportViewer1.Visible = true;
Session["cr"] = crReportDocument;
}
else//reportstate from postback
{
CrystalReportViewer1.ReportSource = (ReportDocument)Session["cr"];
}
}
private void SetDBLogonForReport(ConnectionInfo connectionInfo)
{
TableLogOnInfos tableLogOnInfos = CrystalReportViewer1.LogOnInfo;
foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
{
tableLogOnInfo.ConnectionInfo = connectionInfo;
}
}
public void btnyclick(object sender, EventArgs e)
{
LoadReport(ReportState.FromSession);
}
public void btnClose_Click(object sender, EventArgs e)
{
}
}
}
我的頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RptParameter2.aspx.cs" Inherits="AMR.RptParameter2" %>
<%@ 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>Crystal Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="floatbox">
<p> Enter the Employee Position : <asp:TextBox ID="txtAvailPos" runat="server" Width="125" > </asp:TextBox>
<p style="margin-left: 80px"> Detail Level : <asp:DropDownList ID="ddlDetailLvl" runat="server" >
<asp:ListItem Text="SELF" Value="SELF" Selected="True"></asp:ListItem>
<asp:ListItem Text="TEAM" Value="TEAM" ></asp:ListItem>
</asp:DropDownList> </p>
<p style="margin-left: 120px"> Month : <asp:DropDownList ID="ddlMonth" runat="server" >
<asp:ListItem Text="01" Value="01" ></asp:ListItem>
<asp:ListItem Text="02" Value="02" ></asp:ListItem>
<asp:ListItem Text="03" Value="03" ></asp:ListItem>
<asp:ListItem Text="03" Value="04" ></asp:ListItem>
<asp:ListItem Text="05" Value="05" ></asp:ListItem>
<asp:ListItem Text="06" Value="06" ></asp:ListItem>
<asp:ListItem Text="07" Value="07" ></asp:ListItem>
<asp:ListItem Text="08" Value="08" ></asp:ListItem>
<asp:ListItem Text="09" Value="09" ></asp:ListItem>
<asp:ListItem Text="10" Value="10" ></asp:ListItem>
<asp:ListItem Text="11" Value="11" ></asp:ListItem>
<asp:ListItem Text="12" Value="12" ></asp:ListItem>
</asp:DropDownList> </p>
<p style="margin-left: 120px"> Year : <asp:TextBox ID="txtYear" runat="server" Text="2009" Enabled="false" Width="35" > </asp:TextBox></p>
<p style="margin-left: 160px"> <asp:Button runat="server" ID="btny" Text="Show Report" OnClick="btnyclick" /> </p>
<asp:Button runat="server" ID="btnclose" Text="Close" OnClick="btnClose_Click" /><br />
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="true" />
</div>
</div>
</form>
</body>
</html>
+1爲你找出你的問題。我正要重新考慮一下,但最近家裏的緊急情況讓我很忙。很高興你想出來了。 – Dusty 2009-10-30 19:35:32