Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare null value for datetime

Tags:

c#

asp.net

I am storing date as string in sql table. If date is not passed then it will be null. Now I want to check whether date is null from the design page of asp.net. I am getting error of type cast. Any one is having idea how to overcome this?

<%# ((DateTime)Eval("Date")).ToString("yyy-MM-dd hh:mm tt") %>">

Here data is getting as string input.

Thanks

like image 723
Swapnali Hadawale Avatar asked Nov 03 '25 11:11

Swapnali Hadawale


1 Answers

I am storing date as string in SQL table.

This is your first problem (here is a good explanation of why this is a problem).

I am getting error of type cast.

This is how your first problem (storing a date as a string) causes your second problem (type cast error). Although it is possible to fix this by parsing the date from a string using DateTime.Parse, DateTime.ParseExact, or DateTime.TryParse, I would strongly recommend against using any of these approaches.

The good news is that once you fix the first problem, the second problem goes away by itself!

Use this approach to change the type of the column without losing the data:

  • Add a new column of type date to your table
  • Run an update statement to populate the new date column from the old varchar column that contains string representations of dates
  • Drop the old varchar column
like image 85
Sergey Kalinichenko Avatar answered Nov 06 '25 01:11

Sergey Kalinichenko