Through a form you can enter a date and then I want to insert it into the database. The question is that I want it to allow entering a NULL date (the field in the form is left empty).
Datetime fecha = Request.Form["fecha"];
<form action="#" method="post">
<label>Fecha Impresion:</label>
<input type="text" name="fecha" value="" />
<br />
</form>
db.Query("INSERT INTO Fecha VALUES(@0)", fecha);
How can I do? Since it throws me an error that date cannot be null and I don't know how to parse it so that it sends it as null.
It seems to me that the problem is not because it is null, but because the date field is blank. Try to use this code:
Or maybe:
I don't have now to test it :)
The problem is not in the code but in the database. Modify your table
fecha
so that the relevant field accepts the valueNULL
:Where
your_column
is the name of the column in your tableFecha
https://stackoverflow.com/questions/13934621/insert-null-empty-value-in-sql-datetime-column-by-default
Following the previous link you will only need to validate the date you send from the form by casting "Request.Form["date"]"
I would make two changes to your code. The first is to declare the date as
Nullable
:The second, format the string so that it doesn't throw exceptions in the
query
: