I have a SQL Table with the following columns:
PROFILE
----------------------------------
Name | DateOfBirth | Address | ID
----------------------------------
| | |
| | |
[Name] is a required field while the ID is an autoincrement column.
The remaining fields are not required.
When it comes to displaying again the information of a user in my form, since the DateOfBirth field is not required I always got this value in my textBox 1900-01-01 00:00:00.000 even if it not supplied upon registration.
What I’m trying to accomplish is to display an empty string inthe textBox if the there’s no date of birth.
With my naive way of thinking, I wrote a method where it checks first the DateOfBirth column, if it is null, it will return an empty string, else, return the date of birth as a string ex. txtDateOfBirth.Text = GetDateOfBirth();
but it always return the value `1900-01-01 00:00:00.000′ cause the field in the database always inserts this value even the user leaves this field as blank.
Can you please help me… thanks…
I also tried this but still don’t work:
SELECT BIRTHDATE = CASE BIRTHDATE
WHEN CAST('1900-01-01' AS DATETIME) THEN ''
ELSE BIRTHDATE END
FROM USER
I got it already! 🙂
Here’s my original code:
In my original code, even though
txtDateofBirthis empty, it will always pass an empty string to the parameter causing the database to insert ‘1900-01-01 00:00:00.00’, not theNULLvalue. Here’s the modified code. I check first iftxtDateOfBirthis not empty (assuming the value entered is a valid date) if true, addcmd.Parameters.Add(new SqlParameter("@DATEOFBIRTH", txtDateOfBirth.Text.Trim()));as a parameter toINSERT_USERstored procedure. Else,@DATEOFBIRTHparameter is not being created and passed at all.SP: