0
  if(startDate == "")
  {
      DateTime? startDateParser = null;
      DateTime.TryParse(startDate, out startDateParser);
  }

if the string startDate is empty i would like it to output a null however it complains

cannot convert from 'out System.DateTime?' to 'out System.DateTime'

parsing the null into a sql statement which the parameter accepts nulls however gets parsed in as 01/01/0001

command.Parameters.AddWithValue("@StartDate", (r.StartDate == null ? DBNull.Value : (object)r.StartDate));

any help will be greatly appreciated

J.Alex
  • 45
  • 1
  • 8
  • I'm assuming your question isn't really how to convert from a nullable type to a non-nullable type, but is actually how to pass a null parameter? If so then this might help: https://stackoverflow.com/questions/7497786/passing-null-as-sqlparameter-datetime-value – Richard Hansell Aug 16 '18 at 13:43

3 Answers3

2

Use this code to resolve "cannot convert from 'out System.DateTime?' to 'out System.DateTime'" problem.

DateTime tmpDate;
DateTime? dateValue = DateTime.TryParse(startDate, out temp) ? temp : (DateTime?)null;

Can use the date value you parse for the result variable. You can use either null or a minimum value.

object resultVal = dateValue.HasValue ? dateValue.Value : DateTime.MinValue; // or DBNull.Value

command.Parameters.AddWithValue("@StartDate",resultVal  );
0

A System.DateTime cannot be null. If you require a parameter of this type to allow nulls, you can do as your error suggests and use System.DateTime? which allows null values.

SO has many questions in relation to this topic, such as the following: datetime-null-value

Taken from the accepted answer:

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue, because it is a value type rather than a reference type.

You can also use a nullable DateTime, like this:

DateTime? MyNullableDate;

Or the longer form:

Nullable<DateTime> MyNullableDate;

And, finally, there's a built in way to reference the default of any type. This returns null for reference types, but for our DateTime example it will return the same as DateTime.MinValue:

default(DateTime)
Community
  • 1
  • 1
Barry Piccinni
  • 1,685
  • 12
  • 23
  • im making a use interface where all but one thing can be parsed up as empty and it should still succeed the same as if they all had values, satrtDate comes up to the c# as "" and when it then goes to the sql it goes to 01/01/0001 as it has to be a datetime , if there is a way for the sql to detect 01/01/0001 and nd convert it there in the sql that will be fine or in the c# beforehand. maybe im looking for the wrong thing – J.Alex Aug 16 '18 at 13:37
  • It sounds like something you should definitely be fixing in the C# rather than the SQL to me. If you pass an empty string like "" into SQL it will interpret it as just that - an empty string which it thinks it can parse to a date. If you make it a nullable type and pass in Null it will interpret this as null and not try to parse it to a date. – Barry Piccinni Aug 16 '18 at 13:43
  • yea that does sound better , its not parsing in a null or "" , what im guessing its doing is parsing the minimum value for a datetime and doing that although all the values which handle the date are all nullable – J.Alex Aug 16 '18 at 13:49
0

Remember, with out you're giving the method your variable to manipulate. A DateTime struct is an 8 byte value type. Variables for holding such values are 8 bytes and are expected to directly contain the value.

A DateTime? is a nullable DateTime - which is also a struct. But a Nullable<T> always contains an instance of the underlying and an extra bool. Which means that it will be at least 9 bytes in size (in practice, almost certainly 12 in most common implementations). A variable for such a type is (say) 12 bytes and contains the underlying value and the bool (and the bool may be "first" in the layout)

Now do you see why, if you're supplying the variable for some other method to manipulate, it has to be exactly the right sort of variable in order for the code inside that method to work with it correctly.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • i see that im just wondering how i would make them acceptable to one another so it can be manipulated effectively – J.Alex Aug 16 '18 at 13:47