Crystal Report WinForm如何修复错误对象引用未设置为对象的实例?

时间:2012-11-20 02:37:58

标签: c# winforms crystal-reports compiler-errors crystal-reports-2008

2008年WindowsForm C#上的水晶报告

错误:对象引用未设置为对象的实例。

如何解决?

string appConn = ConfigurationManager.ConnectionStrings["connDB"].ConnectionString;

private void button5_Click(object sender, EventArgs e)
        {
            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();
            DataTable dt;
            string strSQL;

            strSQL = "SELECT * FROM filesTA WHERE ChkDate = '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "'";

            objConn.ConnectionString = appConn;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL;
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;

            dtAdapter.Fill(ds, "myDataTable");
            dt = ds.Tables[0];

            dtAdapter = null;
            objConn.Close();
            objConn = null;

            ReportDocument rpt = new ReportDocument();
                   rpt.Load(System.Web.HttpContext.Current.Server.MapPath("Report\\CrystalReport1.rpt")); <<< Error
            rpt.SetDataSource(dt);
            this.crystalReportViewer1.ReportSource = rpt;
            this.crystalReportViewer1.Refresh();
        }

请帮帮我。谢谢你的时间。 :)

1 个答案:

答案 0 :(得分:0)

好吧,我建议仔细查看堆栈跟踪。但是,我的猜测是System.Web.HttpContext.Current为空。许多人没有意识到这可能是null,所以当你尝试引用它的Server属性时,你会得到一个空的引用异常。