crystal report 2008 on WindowsForm C#
Error: Object reference not set to an instance of an object.
How to Fix it?
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();
}
Help me please. Thanks you for you time. 🙂
Well, I would suggest looking at the stack trace closer. However, my guess would be that
System.Web.HttpContext.Currentis null. Many people do not realize that this can be null, so when you try to reference it’sServerproperty you get a null reference exception.