DateTime.Parse is min date if null

I have an asp.net application in which I have a text box for entering a datetime value and it is stored in the database.

Now when I try to get the date, it shows 1/1/0001 12:00:00 AM if the date is zero.

this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString()); 

Apologies, the requirement has changed. Now I want to show blank ('') if FirstReceivedDate is null.

How can i do this?

+4
source share
7 answers

You can try something like below:

 DateTime date = DateTime.Parse(dr["FirstReceivedDate"].ToString()); this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now; 

since it is properly understood, this should work.

Alternatively, if you need a value when the date is zero, you can try something like this:

 DateTime date; if(DateTime.TryParse(dr["FirstReceivedDate"], out date)) this.FirstReceivedDate = date != DateTime.MinValue ? date : DateTime.Now; else this.FirstReceivedDate = DateTime.Now; // or whatever you want to do if "FirstReceivedDate" is not a valid date. 
+1
source

DateTime variables (along with all other value types) cannot be empty (unless you explicitly declare them nullable), because they must hold the value. (You now said that it is reset)

The required fix is ​​quite simple - you can simply check the null value of the field in datarow and assign it a null DateTime value, if any, otherwise assign a value to the database of the type using the DataRow.Field<T> method:

 this.FirstReceivedDate = dr["FirstReceivedDate"] == DBNull.Value ? (DateTime?)null : dr.Field<DateTime>("FirstReceivedDate"); 

Actually thinking about it, you should simply do the following to propagate a null value (untested):

 this.FirstReceivedDate = dr.Field<DateTime?>("FirstReceivedDate"); 

As you stated that you want to get an empty string if the database value is null, then just when you show it, you can just check the DateTime value is null:

 textBox1.Text = this.FirstReceivedDate == null ? "" : this.FirstReceivedDate.ToString(); 
+1
source
 this.FirstReceivedDate = DateTime.Parse(dr["FirstReceivedDate"].ToString()); if (this.FirstReceivedDate == DateTime.MinValue) this.FirstReceivedDate = DateTime.Now; 

Check if FirstReceivedDate minimum value for DateTime (which you indicated in your question). If so, just set it to the current date / time.

0
source
 DateTime date = DateTime.Parse(dr["FirstReceivedDate"].ToString()); this.FirstReceivedDate = date = =default(DateTime) ? DateTime.Now : date; 

The real question here is: why do you get 1/1/0001 12:00:00 AM when you say datetime is NULL? , you should get DBNull, check your database, the field should take zeros.

0
source

I created this extension method to try to convert it to datetime, if the function returns null, then the value is not a valid date-time

  public static DateTime? ToDateTimeCheck2(this string valor) { var isDate = true; DateTime dt; isDate = DateTime.TryParse(valor, out dt); if (isDate) return dt; else return null; } 

Then in your code you can say

 this.FirstReceivedDate =( dr["FirstReceivedDate"].ToDatetimeCheck2() == null)? "" : dr["FirstReceivedDate"].ToDatetimeCheck2(); 

Another option using the extension method:

  string t = "123"; DateTime result; string testing = (DateTime.TryParse(t,out result ))? result.ToShortDateString() : string.Empty; 
0
source

Try the following:

 DataRow dbField = dr["FirstReceivedDate"]; this.FirstReceivedDate = dbField == null ? String.Empty : dbField.ToString() ; 
-one
source

The code is executed below.

 string date = dr["FirstReceivedDate"].ToString(); this.FirstReceivedDate = date == "" ? (DateTime?)null : DateTime.Parse(date); 

Thank you all for your help.

-one
source

All Articles