locked
How to perform runtime binding on a null reference' RRS feed

  • Question

  • I am using an excel sheet as an input for my code, i.e I am taking data row by row from each column in excel and assigning it to property of a class

    case 1 : It gets assigned easily , if there is value in a column .

    Case 2 : it doesnt works and throws an error for the columns which are empty

    The error is: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot perform runtime binding on a null reference'

    Here is what I have done:

    className.propertyName= (string)(xlRange.Cells[rowABCD, 2] as Range).Value2.ToString();

    also I have tried:

                                

    className.propertyName = className.propertyName == null ? className.propertyName= (string)(xlRange.Cells[rowABCD, 7] as Range).Value2.ToString() : className.propertyName = "  Empty";

    Thursday, July 2, 2020 4:12 AM

Answers

  • Hi Shirin14,
    The ToString() method will cause null reference exception when the value is null. And using Convert.ToString(), it will return empty string for the null value.
    So you can try to use Convert.ToString() instead of ToString() method.

    className.propertyName = className.propertyName == null ? Convert.ToString(className.propertyName= (string)(xlRange.Cells[rowABCD, 7] as Range).Value2): className.propertyName = " Empty";

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shirin14 Thursday, July 2, 2020 12:05 PM
    Thursday, July 2, 2020 7:03 AM

All replies

  • Hi Shirin14,
    The ToString() method will cause null reference exception when the value is null. And using Convert.ToString(), it will return empty string for the null value.
    So you can try to use Convert.ToString() instead of ToString() method.

    className.propertyName = className.propertyName == null ? Convert.ToString(className.propertyName= (string)(xlRange.Cells[rowABCD, 7] as Range).Value2): className.propertyName = " Empty";

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Shirin14 Thursday, July 2, 2020 12:05 PM
    Thursday, July 2, 2020 7:03 AM
  • Hi Daniel,

    Thankyou very much!

    Its working now, However in case of other datatypes apart from String it doesn't work

    Example If is a double value or DateTime value is there...

    1) I have done this way for double values:                         

    className.propertyName= className.propertyName == 0.0 ? Convert.ToDouble(className.propertyName = (double)(xlRange.Cells[rowABCD, 7] as Range).Value2) : className.propertyName = 0.0d;

    error is: Cannot convert null to 'double' because it is a non-nullable value type'

    Therefore I declared Property like this : public double? propertyName

    By declaring as above it doesn't gives error , However it everytime it returns an empty value even if there is some value.

    2) For Date Time:                           

    className.propertyName = className.propertyName == DateTime.MinValue ? Convert.ToDateTime(className.propertyName = (DateTime)(xlRange.Cells[rowABCD, 10] as Range).Value2) : className.propertyName = DateTime.MinValue;

    error is : Cannot convert type 'double' to 'System.DateTime''

    Can you tell what is the wrong thing .

    Thanks and regards.

    Thursday, July 2, 2020 12:30 PM
  • Hi Shirin14, 
    For question1:
    You can convert string to double value using double.TryParse(string value) method. This will handle null values internally and set it to 0 if input value is null or cannot convert to double.
    For question2:
    You can try to use DateTime.FromOADate(Double) method to convert type double to System.DateTime.
    More details you can refer to this thread.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 3, 2020 8:59 AM
  • Hi Daniel,

    Unfortunately , its not working

    for 1) It is giving error as  No overload for method 'TryParse' takes 1 arguments

    for 2) it is asking to first convert into double.

    Can you please help me.

    Tuesday, July 7, 2020 3:17 PM