0

I have a web forms application which allows to update database columns. One of the database columns is Finish Date which is of date datatype in the SQL Server database.

I use SQL query with parameters and textbox to provide the new value for the finish date.

This is a code which gets me the value of the date from a text box within the grid:

TextBox tFD = (TextBox)grdProjectUpdate.Rows[e.RowIndex].Cells[11].FindControl("proFDTextBox");

then I use a parameter:

cmd.Parameters.Add("@proFD", SqlDbType.Date).Value = tFD.Text.Trim();

to update the value using this SQL statement:

UPDATE PMSprojects SET proFD = @proFD ,...    

This solution works fine whenever there is an actual date provided. However, it does not save nulls. If I provide an empty string, it is being converted into 1900-01-01 date, even though the column in the database allows nulls.

How could I solve this issue and save null to the database?

1 Answer 1

3

You have to write code around this to handle, and pass DbNull.Value when the value is null.

cmd.Parameters.Add("@proFD", SqlDbType.Date).Value = (tFD.Text.Trim().Length > 0) ? (object)tFD.Text.Trim() : DbNull.Value;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.