473,804 Members | 3,462 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).V alue;

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 7197
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).V alue;

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...@nn owslpianmk.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).V alue;
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).V alue ??
DateTime.UtcNow ;

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

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).V alue ??
DateTime.UtcNow ;
You can only use nullable types with the coalescing operator. The
Nullable<DateTi me>.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...@nn owslpianmk.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).V alue ??
DateTime.UtcNow ;

You can only use nullable types with the coalescing operator. *The *
Nullable<DateTi me>.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
24677
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 in the xml.
8
19251
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 is null or nor before processing it, but I am not able to figure out the syntax.
1
2436
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 could be NULL Thank you.
2
15903
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 in/select the activity or followup date. If, for this exercise, the followupdate textfield is empty, then when the insert method of the class calls the InsertActivityLog stored procedure, how can I get nulls inserted instead of 1/1/1900 because of
8
4344
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 procedure and must save either a null or a real date. However, passing a hardcoded "null" to call this function causes a compilation error : cx.cs(136): Argument '16': cannot convert from '<null>' to 'System.DateTime'
3
1928
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 return null; because "Cannot convert null to 'System.DateTime' because it is a value type" How can this be done?
5
51682
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 DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
9
1355
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 should just forget it and dates and times as a string in the database table. Anyone wrestle with this decision?
4
10699
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 = DateTime.Parse(oldRow.ToString()); The program is failing on the if saying that the null value in the field in
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.