user979331
user979331

Reputation: 11871

ASP.NET Failed to convert parameter value from a String to a DateTime

I am inserting data into a database stored procedure like so:

SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime);
                            parameter2.Value = sortedCells[i].finishedDate;
                            parameter2.Direction = ParameterDirection.Input;
                            command.Parameters.Add(parameter2);

My issue I am having is when the I try to insert an empty date "" I get this error:

Failed to convert parameter value from a String to a DateTime

the column I am inserting into can allow NULLs....so how would I say if this is "" then give it a NULL

Upvotes: 1

Views: 610

Answers (4)

Chris Bergin
Chris Bergin

Reputation: 415

Check for an empty string and substitute DBNull.Value.

parameter2.Value = string.IsNullOrWhiteSpace(sortedCells[i].finishedDate) 
    ? DBNull.Value 
    : sortedCells[i].finishedDate;

Alternatively, give the parameter a default value of NULL in the stored procedure definition, then only set the value when it's not empty.

For the stored procedure:

CREATE PROCEDURE [YourSchema].[YourProcedure]
...snip...
@actualFinish DATETIME = NULL
...snip...

Then:

if(!String.IsNullOrWhiteSpace(sortedCells[i].finishedDate)
    parameter2.Value = sortedCells[i].finishedDate;

Editing to add:

Using a nullable DateTime would be clearer, I think, then letting the implicit string conversion do its thing.

DateTime? finishedDate = null;
if(!String.IsNullOrWhiteSpace(sortedCells[i].finishedDate))
        finishedDate = DateTime.Parse(sortedCells[i].finishedDate);

Then finishedDate is always your parameter value. You could be even safer by using TryParse and informing the user when the date fails parsing that way. TryParse doesn't take nullable types, though, so you could use two variables or you could use a regular DateTime variable and use DateTime.Min as a sentinel value for "no date" and use the ternary operator to switch that to null instead. Lots of ways to skin this cat.

Upvotes: 0

In the appropriate table, Add NULL to the appropriate date column.

[FinishedDate] [datetime] NULL.

Hope this will work.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

You should always convert it to the correct type before you pass the value.

var actualFinishParameter = new SqlParameter("@actualFinish", SqlDbType.DateTime);
Object actualFinish = DBNull.Value;
DateTime finishDate;
if(DateTime.TryParse(sortedCells[i].finishedDate, out finishDate))
    actualFinish = finishDate;
actualFinishParameter.Value = actualFinish;
command.Parameters.Add(actualFinishParameter);

Upvotes: 0

Rion Williams
Rion Williams

Reputation: 76557

You may want to consider explicitly parsing your date prior to passing it in as a parameter and do a check to see if it contains a value to determine if you should pass the DateTime object or DBNull.Value :

DateTime finishDate = DateTime.MinValue;
// This will attempt to parse the value if possible
DateTime.TryParse(sortedCells[i].finishedDate, out finishDate);

// Build your parameter here
SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime); 
parameter2.Direction = ParameterDirection.Input;
// If you are using a nullable field, you may want to explicitly indicate that
parameter2.IsNullable = true;

// Then when setting the value, check if you should use the value or null
if(finishDate == DateTime.MinValue)
{
     parameter2.Value = DBNull.Value;
}
else
{
     parameter2.Value = finishDate;
}

// Finally add your parameter
command.Parameters.Add(parameter2);

Upvotes: 2

Related Questions