none
Object reference not set to an instance of an object. RRS feed

  • Question

  • Hello

    I am adding rows to table and setting value row/cell value, but for datetime field it is giving me error

    Object reference not set to an instance of an object.

    Initially I am adding a column to table as

    table.Columns.Add("Upload Date", typeof(DateTime));

    Then I assigning value to row as

    row["UploadDate"] = result["Execution Date"].ToString();


    I also tried with

    row["UploadDate"] = result["Execution Date"];

    As I have I also null or empty value in the "Execution Date" field . Same kind of behaviour experiencing in one of single line text field.

    Please let me know how do I manage this?

    Thursday, May 27, 2010 3:20 AM

Answers

  • you should check for null then.

    if (result["Execution Date"]) != null)

    {

      row["UploadDate"] = result["Execution Date"];

    }

    • Marked as answer by Avian123 Thursday, May 27, 2010 5:35 AM
    Thursday, May 27, 2010 4:29 AM

All replies

  • you should check for null then.

    if (result["Execution Date"]) != null)

    {

      row["UploadDate"] = result["Execution Date"];

    }

    • Marked as answer by Avian123 Thursday, May 27, 2010 5:35 AM
    Thursday, May 27, 2010 4:29 AM
  • what the result here is

    the error may be because of null value in result["Execution Date"]; so before update check whether it's null or not and is null then update System.DBNull in that particular row

     


    Manish Sati

     

     

    Thursday, May 27, 2010 4:30 AM
  • Yes this because of null value. Check in your code for null values and if it is not null then only update the value.
    Thanks & Regards, Neerubee
    Thursday, May 27, 2010 4:45 AM
  • Thanks f00z, it seems to be working
    Thursday, May 27, 2010 5:35 AM
  • But i think if (result["Execution Date"]) != null)  will itself throw an exception, so you need to catch the exception and code accordingly.
    Thursday, May 27, 2010 5:38 AM