What is the standard way to handle Null dates in .NET.

I have an asp.net form with C # where I take user information to insert into the database, as usual, using Linq. Well. Where, how do I get the date of birth also from the user, but if the user skips the date field text box from ui, then I get the date as '01 / 01/0001 ', something like this, which, of course, database security will not allow keep it.

So I need to check somewhere in my code that it is null or in this (above) format. If it is null or in the format '01 / 01/0001 ', then what exactly do I need to do? I have no default settings for dates.

So, what is the standard way of processing if the date is zero (but not necessary). Please guide me. So many times I have been trapped by accessing null for different types.

Edited let's see what I did, it seems to work here. but I don't think this is the standard way:

DateTime? otxtDOB = new DateTime(); if (!string.IsNullOrEmpty(DOB)) { if (Convert.ToDateTime(DOB) != DateTime.MinValue) { otxtDateOfPurchese = Convert.ToDateTime(Convert.ToDateTime(DOB).ToString("dd-MMM-yyyy")); } else { otxtDOB = null; } } 

Please confirm that this is the right way?

+4
source share
7 answers

Creating a date Nullable property (ie, " DateTime? ") Should allow it to actually be null if the user did not set it. (And if your database column is nullable, it can be stored as null in the database)

Otherwise, it will default to DateTime.MinValue , which you see here. When added to the database, you will have to run the DateTime.MinValue explication test.

+10
source

DateTime is a value type (e.g. a number), so you cannot define a null value. Mane people use DateTime.MinValue or DateTime.MaxValue instead, but I prefer to use nullable types:

 DateTime? nullableDate; dateSample.Value = null; 
+1
source

You have the option of using Nullable<DateTime> (an alias of DateTime? ). This allows you to treat the date as zero in all applications.

However, I personally cannot find nullables and would prefer this second way: you can use DateTime.MinValue (which 01/01/0001) as a significant constant in your application and check DateTime.MinValue in your data access level.

The database, if it is SQL Server, and the field is of type smalldatetime, overflows and throws an exception if you tried to save DateTime.MinValue . However, Null can be stored in the database for any type.

Here's how you can parse your strings into nullable types:

 private delegate bool TryParseDelegate<T>(string s, out T t); private static T? TryParseNullable<T>(string s, TryParseDelegate<T> tryParse) where T : struct { if (string.IsNullOrEmpty(s)) return null; T t; if(tryParse(s, out t)) return t; return null; } 

using:

 var nullableDateTime = TryParseNullable<DateTime>("01/01/0001", DateTime.TryParse); 
0
source

you can do something like this, C # has some functions of type null type that you can use it will save you part of the code and it will be more reliable.

 Public int InsertData(int? ouId) { chkValue = ouId.HasValue ? ouId.Value : 0; } 
0
source

use

 DateTime dt; if(DateTime.TryParse(DatetImeValue.Tostring(),dt)) // datetimevalue is your db value { datetimeproperty = dt; // in your class declare it as DateTime? datetimeproperty } else { datetimeproperty = null; } 

So far, a check for null is displayed if its null set is empty.

[Update]

Do one thing: keep the nullable property. In your database. Set the field to allow null and in the parameter user @DateTimeParam = null.

OR FAST BASIS OPTIONS DATABASE AND VARCHAR PARAMETERS INSTEAD OF DATE, IN DATETIMEVALUE.TOSHORTDATESTRING () AND OPTIONAL PASS. IN THIS MANNER, IT WILL BE EASY FOR YOU TO DISPLAY THE DATE AND TIME. YOU SHOULD NOT BE OR FORBIDDEN OF A PART OF TIME IF YOU DO NOT NEED IT

0
source
 obj.BirthDate = Convert.ToDateTime(string.IsNullOrEmpty(txtBirthDate.Text.ToString()) ? System.Data.SqlTypes.SqlDateTime.MinValue.Value : Convert.ToDateTime(txtBirthDate.Text.ToString())); 
0
source

You can use this when transferring to the database.

 object datetimeObj = null; if (datetimefromUI == DateTime.MinValue) // This is put in the DateTime object by default datetimeObj = DBNull.Value; else datetimeObj = datetimefromUI; // Post datetimeObj to parameter in database... 
-3
source

Source: https://habr.com/ru/post/1343742/


All Articles