473,499 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DateTime Null

Hello,

I have the following:

DateTime c = database.Posts.Max(b =b.CreatedAt).Value;

b.CreatedAt is of type "DateTime?"

The problem is when database.Posts is empty. I get an error:
Nullable object must have a value.

I understand that I get a null value but I am not able to apply ??
because it does not allow me.

How can I solve this?

Thanks,
Miguel

Oct 19 '08 #1
4 7180
On Sun, 19 Oct 2008 14:03:00 -0700, shapper <md*****@gmail.comwrote:
Hello,

I have the following:

DateTime c = database.Posts.Max(b =b.CreatedAt).Value;

b.CreatedAt is of type "DateTime?"

The problem is when database.Posts is empty. I get an error:
Nullable object must have a value.

I understand that I get a null value but I am not able to apply ??
because it does not allow me.

How can I solve this?
Can you be more explicit about what you mean by "it does not allow me"?
You didn't post any code that tries to use the null coalescing operator
(??). There's not any particular reason why "it" (the compiler, I presume
you mean) should not "allow" you to use it.

If you can post the code that you think should work but doesn't, we can
help change that to code that does work.

Pete
Oct 19 '08 #2
On Oct 19, 10:33*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 19 Oct 2008 14:03:00 -0700, shapper <mdmo...@gmail.comwrote:
Hello,
I have the following:
DateTime c = database.Posts.Max(b =b.CreatedAt).Value;
b.CreatedAt is of type "DateTime?"
The problem is when database.Posts is empty. I get an error:
Nullable object must have a value.
I understand that I get a null value but I am not able to apply ??
because it does not allow me.
How can I solve this?

Can you be more explicit about what you mean by "it does not allow me"? *
You didn't post any code that tries to use the null coalescing operator *
(??). *There's not any particular reason why "it" (the compiler, I presume *
you mean) should not "allow" you to use it.

If you can post the code that you think should work but doesn't, we can *
help change that to code that does work.

Pete
Sure ... Sorry. I tried:

DateTime c = database.Boxes.Max(b =b.CreatedAt ?? DateTime.UtcNow);

Which seemed logic to me but I get:
The null value cannot be assigned to a member with type
System.DateTime which is a non-nullable value type.

I also tried:
DateTime c = database.Boxes.Max(b =b.CreatedAt).Value ??
DateTime.UtcNow;

But this one does not even compile and I get:
Operator '??' cannot be applied to operands of type 'System.DateTime'
and 'System.DateTime

Thanks,
Miguel
Oct 19 '08 #3
On Sun, 19 Oct 2008 14:43:30 -0700, shapper <md*****@gmail.comwrote:
Sure ... Sorry. I tried:

DateTime c = database.Boxes.Max(b =b.CreatedAt ?? DateTime.UtcNow);

Which seemed logic to me but I get:
The null value cannot be assigned to a member with type
System.DateTime which is a non-nullable value type.
Ah. Yes, it would do that when the database is empty. But, if you move
the coalescing operator out of the lambda expression, I think it should be
okay:

DateTime c = database.Boxes.Max(b =b.CreatedAt) ?? DateTime.UtcNow;

Note that that's subtly different from what you did try:
I also tried:
DateTime c = database.Boxes.Max(b =b.CreatedAt).Value ??
DateTime.UtcNow;
You can only use nullable types with the coalescing operator. The
Nullable<DateTime>.Value property is a non-nullable value type and so is
an illegal operand for the operator.

Without a true concise-but-complete code sample, it's difficult to know
for sure what will or won't work. But I'm pretty sure the above should
solve your problem.

Pete
Oct 19 '08 #4
On Oct 20, 12:20*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 19 Oct 2008 14:43:30 -0700, shapper <mdmo...@gmail.comwrote:
Sure ... Sorry. I tried:
DateTime c = database.Boxes.Max(b =b.CreatedAt ?? DateTime.UtcNow);
Which seemed logic to me but I get:
The null value cannot be assigned to a member with type
System.DateTime which is a non-nullable value type.

Ah. *Yes, it would do that when the database is empty. *But, if you move *
the coalescing operator out of the lambda expression, I think it should be *
okay:

* * *DateTime c = database.Boxes.Max(b =b.CreatedAt) ?? DateTime.UtcNow;

Note that that's subtly different from what you did try:
I also tried:
DateTime c = database.Boxes.Max(b =b.CreatedAt).Value ??
DateTime.UtcNow;

You can only use nullable types with the coalescing operator. *The *
Nullable<DateTime>.Value property is a non-nullable value type and so is *
an illegal operand for the operator.

Without a true concise-but-complete code sample, it's difficult to know *
for sure what will or won't work. *But I'm pretty sure the above should*
solve your problem.

Pete
Thank You Pete. I didn't know that about value ... I though it would
still be a nullable DateTime.
Oct 19 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
548
by: news.microsoft.com | last post by:
Hello, I have DateTime property. How can I set this value to null?
2
24572
by: Pet Matrix. | last post by:
Hi, I am having the schema and one element in that schema is , <xs:element name="EndT" type="TstampType" minOccurs="1" maxOccurs="1"/> How do I represent the NULL value for the datatype dateTime...
8
19218
by: Manish Jain | last post by:
Platform : ASP.Net/C# void SomeFunction(DateTime date) { string text = date.ToString(); //This crashes if date is null } I am using a construct similar to above. I want to check if the date...
1
2407
by: Dean L. Howen | last post by:
Dear friends, Could you help me to solve the problem: How can we set/get NULL values of datetime in DatetimePicker ? I design a form using DateTimePicker to input/output date value, but the date...
2
15854
by: Rey | last post by:
Howdy all. My problem deals w/inserting nulls into database (SQL Svr 2K) for the datetime fields activityDate and followUpDate where nulls are allowed. >From the web form, the user can type...
8
4316
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
3
1898
by: Daves | last post by:
a get { ... } for public property SelectedValue returns DateTime type to be used as a parameter in a Sql update query but I'd like it to return "empty" if no date has been selected... I cannot use...
5
51540
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
9
1333
by: Tony Van | last post by:
They're driving me nuts, especially with the Datareader. There are times when I want to pass a null date on to a date variable and I have to cast the null as a date!!!??? I'm wondering if I...
4
10677
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember =...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.