开发者

How to dynamically assign report and connectioninfo to crystal report

I am completely lost on this one--

I have a .Rpt file that runs fine by itself. This report file takes 4 parameters and connects to an oracle database. I have all the database code set up correctly. So i think.

I cannot for the life of me connect this report file to the connection and have the report viewed on my ASP.NET webpage.

When i run this code, no matter what, i get the text invalid pointer where the crystalreportviewer control goes. I am running Crystal Reports 2008 SP2 in an ASP.NET 3.5 project. I am open to people suggesting links to thorough methods of viewing reports (including specifying parameters, connectioninfo, and disposing reportdocument) seeing how I am completely new to this.

I need this process to be dynamic because in the future i will be dynamically setting report files, parameters, database info, etc...

Thank you all in advance.

My Code behind:

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)
        {

        }
    }
}

My Page:

<%@ 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>  &nbsp;  Enter the Employee Position : <asp:TextBox ID="txtAvailPos" runat="server" Width="125" > </asp:TextBox> 
 <p style="margin-left: 80px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                 Detail Level : <asp:DropDownList ID="ddlDetailLvl" runat="server"  >
                                                                                                <asp:ListItem Text="SELF" Value="SELF" Selec开发者_JAVA百科ted="True"></asp:ListItem>
                                                                                                <asp:ListItem Text="TEAM" Value="TEAM" ></asp:ListItem>
                                                                                             </asp:DropDownList> </p>  
 <p style="margin-left: 120px">&nbsp;&nbsp;&nbsp;                        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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         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>


It turns out that the report we were using was an older Crystal Reports (8 i think) report that was using a .qry file. After we converted the query to a command object (in crystal) everything worked!


I just skimmed over it and I don't know if this is your entire problem, but one thing I see is this:

//p2
paramField2.CurrentValues.Add(paramDiscreteValue2);
paramFields.Add(paramField2);
paramField2.CurrentValues.Add(paramDiscreteValue2);
paramFields.Add(paramField2);

Correct this and give it another try. Let me know if you still have issues and I'll dig further into it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜