30

I am using this code to truncate datetime from my database into its year and time components. The variables YearOfRelease and Runtime contain datetime of the format "dd/MM/yyyy hh:mm:ss" It was working fine previously but its now giving the error:

String reference not set to an instance of a String. Parameter name: s

It could only be something wrong in the DateTime.ParseExact function, could anyone please let me know why 'null' is suddenly causing this problem when previously it was working perfectly?

    DateTime dt2 = new DateTime();


    dt = DateTime.ParseExact(YearOfRelease, "dd/MM/yyyy hh:mm:ss", null);

    Year = dt.Year.ToString();


    dt2 = DateTime.ParseExact(RunTime, "dd/MM/yyyy hh:mm:ss", null);
    string hour = dt2.Hour.ToString();
    string min = dt2.Minute.ToString();

    Time = hour + ":" + min;

1 Answer 1

26

The first parameter of DateTime.ParseExact is a string parameter named 's'.

Therefore, it looks like YearOfRelease or RunTime is null in your program. Make sure those are set before you call DateTime.ParseExact.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean in the database? Nope, they are not null, I just checked
Thanks, I rechecked, there was something wrong in my c# code, the variables weren't getting the correct values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.