使用Report.DataSourceConnections时:“对象引用未设置为对象的实例”

时间:2012-04-19 20:50:12

标签: c# crystal-reports

请参阅以下代码。我认为这是非常自我解释的。我正在运行VS2010和Crystal Reports for VS2010。疯狂的事情是我有一个类似的项目,类似的代码,不会在该行上产生异常...所以我一直试图找出两个项目之间的差异,但到目前为止已经空了。

感谢您就如何解决此问题提出任何建议或想法!

using System;
using System.Data;
using System.Data.SqlClient;
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 CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

public partial class Reports : System.Web.UI.Page
{
  ReportDocument rptSP;


protected void Page_Load(object sender, EventArgs e)
{
   rptSP = new ReportDocument();
   string rptPath = Server.MapPath(Request.QueryString["rptname"].ToString());
   rptSP.Load(rptPath, 0);
   rptSP.DataSourceConnections["MyServer\\MyInst", "MyDB"].SetConnection("MyServer\\MyInst", "MyDB", "MyUsername", "MyPass");
//The above line causes an excepton while debugging on my local development machine

如果在我的aspx页面上有问题,我有以下内容:

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

然后在我的web.config中我有:

            <add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
            <add assembly="CrystalDecisions.ReportSource, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
            <add assembly="CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
            <add assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
            <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>

以及我的BIN目录中的一些其他Crystal DLL。

1 个答案:

答案 0 :(得分:1)

尝试:

rptSP.DataSourceConnections[@"MyServer\MyInst", "MyDB"].SetConnection(@"MyServer\MyInst", "MyDB", "MyUsername", "MyPass");

注意用单个反斜杠替换双反斜杠,并使用随附的@符号。我更喜欢这种方式来加倍反斜杠,因为我明确地控制了字符串格式。