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();
}
请帮帮我。谢谢你的时间。 :)
答案 0 :(得分:0)
好吧,我建议仔细查看堆栈跟踪。但是,我的猜测是System.Web.HttpContext.Current
为空。许多人没有意识到这可能是null,所以当你尝试引用它的Server
属性时,你会得到一个空的引用异常。