asked on
ASKER
ASKER
I'm trign to return a date from a table but if there are no records in the table i get the Error The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type. How do i stop this null error if there are no records matching the criteria
System.Nullable<DateTime> DateToSelect =
(from p in db.Archived
select p.TestDate).Max();
ASKER
ASKER
protected void Page_Load(object sender, EventArgs e)
{
TestDataContext TestData1 = new TestDataContext();
DateTime myDate = (DateTime)DateTime.Now;
//Both of the following works. Max date shown.
DateTime? DateToSelect = (from p in TestData1.SAMPLEs
where p.Status_Updated_Date
select p.Status_Updated_Date).Max();
DateTime? DateToSelect = (from p in TestData1.SAMPLEs
where p.Status_Updated_Date != null
select p.Status_Updated_Date).Max();
//This works. Nothing is shown and no error.
DateTime? DateToSelect = (from p in TestData1.SAMPLEs
where p.Status_Updated_Date == null
select p.Status_Updated_Date).Max();
//I also tried to return zero rows and it works just fine.
DateTime? DateToSelect = (from p in TestData1.SAMPLEs
where p.Status_Updated_Date > myDate
select p.Status_Updated_Date).Max();
Response.Write(DateToSelect);
}
ASKER
// A Nullable DateTime variable
DateTime? dt;
// Then you assign to dt a Nullable type a DateTime value
// it will have no problem storing that value in dt.
dt = DateTime.Today;
// And of course you can also assign a null value to a Nullable type
dt = null;
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
http://msdn.microsoft.com/en-us/library/ms173224%28v=VS.100%29.aspx
For example, see the code snippet below:
Open in new window