Answered by:
Error in report code in C#

Question
-
User639567535 posted
i try to get report when user click on update.. scanerio is there is edit button in gridview and also there is several records so when user click on this edit button then box appear furthermore there is several fields then user select report type from drop down now when user select report type and click on update button then report is display but when i debug error is occurred i try this code
protected void gvNR_NRListDevx_AfterPerformCallback(object sender, ASPxGridViewAfterPerformCallbackEventArgs e) { if (e.CallbackName == "STARTEDIT") { int index = ((ASPxGridView)sender).EditingRowVisibleIndex; //view state value with in page... in this code we get form id from gridview ViewState["FFID"] = ((ASPxGridView)sender).GetRowValues(((ASPxGridView)sender).FocusedRowIndex, "FormID"); //get vechile id from gridview ViewState["VID"] = ((ASPxGridView)sender).GetRowValues(((ASPxGridView)sender).FocusedRowIndex, "VID"); //get form datetime from gridview ViewState["DateTime"] = Convert.ToDateTime(((ASPxGridView)sender).GetRowValues(((ASPxGridView)sender).FocusedRowIndex, "DateTime")); } else if (e.CallbackName == "CANCELEDIT") { } else if(e.CallbackName == "UPDATEEDIT") { DateTime DateT = (DateTime)ViewState["DateTime"]; string VehId = (string)ViewState["VID"]; _sys = new clsDe_ComplainMaster(); DataTable dtUpdated = _sys.SelectUpdatedNRtype(ViewState["FFID"].ToString(), VehId, DateT); if (dtUpdated.Rows[0]["NRType"].ToString() == "0002") { printgpsreport(DateT, VehId, "REPORT1.rpt"); } else if (dtUpdated.Rows[0]["NRType"].ToString() == "0011") { printgpsreport(DateT, VehId, "REPORT2.rpt"); } public DataTable SelectUpdatedNRtype(string FormID , string VEhID, DateTime DateT) { string strSql = "select NRType from NR_NRType_LogHistory where FormID = '" + FormID + "' ANd VehicleID = '" + VEhID + "' AND NRDate = '" + DateT + "'"; return this.GetQueryData(strSql); } public DataSet getreport() { DataSet ds; try { SqlParameter[] Params = { new SqlParameter("@datetime",SqlDbType.DateTime), new SqlParameter("@VehID",SqlDbType.VarChar) }; if ( datetime != null) { Params[0].Value = datetime; } else { Params[0].Value = DBNull.Value; } if (VehID != null) { Params[1].Value = VehID; } else { Params[1].Value = DBNull.Value; } ds = GetDataSet("sp_gps_report", Params); return ds; } catch (Exception ex) { throw new Exception(ex.Message); } }
ERROR IS OCCURED ON THIS LINE
DateTime DateT = (DateTime)ViewState["DateTime"];
An exception of type 'System.NullReferenceException' occurred in ERP.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
any help please
Thursday, April 28, 2016 5:21 AM
Answers
-
User-434868552 posted
Please study my reply, above; note the edit.
Regarding your current reply to Lokesh B R, you need to ensure that you do not lose scope:
Boolean goodDateTime = false; // for DateTime.TryParse DateTime DateT; // out parameter for DateTime.TryParse if (String.IsNullOrWhiteSpace(ViewState["DateTime"])) { // handle invalid date/time error } else { // use DateTime.TryParse to convert ViewState["DateTime"] to System.DateTime } if(goodDateTime) { // then you can safely use the value of DateT }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 28, 2016 6:21 AM
All replies
-
User1577371250 posted
Hi,
It states that your ViewState is NULL.
if(ViewState["DateTime"] != null)
{
DateTime DateT = (DateTime)ViewState["DateTime"];
}Thursday, April 28, 2016 5:46 AM -
User-434868552 posted
else if(e.CallbackName == "UPDATEEDIT") { * DateTime DateT = (DateTime)ViewState["DateTime"];
at the very least, you should set a break point on the above statement.
An exception of type 'System.NullReferenceException' occurred in ERP.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.
ViewState["DateTime"] is null ... your goal is to determine WHY ViewState["DateTime"] is null.
whenever ViewState["DateTime"] is null, you will experience a 'System.NullReferenceException'.
Bakhtawar As..., you also need to make your code more robust with a try/catch:
else if(e.CallbackName == "UPDATEEDIT") { try { DateTime DateT = (DateTime)ViewState["DateTime"]; } catch (System.NullReferenceException nullDateTime) { // code to appropriately handle NullReferenceException type of exception } catch (Exception unknown) { // code to appropriately handle any other type of exception }
EDIT:
you have a secondary problem:
DateTime example = (DateTime)"2016-04-28"; // CS0030 Cannot convert type 'string' to 'System.DateTime'
i'm guessing that when ViewState["DateTime"] is not null, it's likely to be a System.String type.
for that reason, your probably want your code to be similar to this:
if (String.IsNullOrWhiteSpace(ViewState["DateTime"])) { // handle invalid date/time error } else { // use DateTime.TryParse to convert ViewState["DateTime"] to System.DateTime }
END EDIT.
Thursday, April 28, 2016 5:55 AM -
User639567535 posted
Hi,
It states that your ViewState is NULL.
if(ViewState["DateTime"] != null) { DateTime DateT = (DateTime)ViewState["DateTime"]; }
thank you for your reply
when i try this it shows me error on this line
if (dtUpdated.Rows[0]["NRType"].ToString() == "0002")
{
printgpsreport(DateT, VehId, "report1.rpt");
}The name 'DateT' does not exist in the current context
Thursday, April 28, 2016 5:58 AM -
User-434868552 posted
Please study my reply, above; note the edit.
Regarding your current reply to Lokesh B R, you need to ensure that you do not lose scope:
Boolean goodDateTime = false; // for DateTime.TryParse DateTime DateT; // out parameter for DateTime.TryParse if (String.IsNullOrWhiteSpace(ViewState["DateTime"])) { // handle invalid date/time error } else { // use DateTime.TryParse to convert ViewState["DateTime"] to System.DateTime } if(goodDateTime) { // then you can safely use the value of DateT }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 28, 2016 6:21 AM -
User303363814 posted
There is no entry in the ViewState called "DateTime". Where is the code that creates it?
Thursday, April 28, 2016 9:06 AM