0

WindowsForm C#上的水晶报告2008

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

如何修复它?

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();
        }

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

4

1 回答 1

0

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

于 2012-11-20T03:00:31.120 回答