Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
cmd.Parameters.AddWithValue("@review_date",Convert.ToDateTime(review_date));


In this case review_date is null Exception will be occurs

Below This Query not working

cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? (DateTime?)null or DateTime.MinValue or DBNULL.Value: Convert.ToDateTime(review_date));

DBNULL.Value:no conversion between System.datetime and system.DBNull Exception occurs
DateTime.MinValue: stack overflow Exception occurs

But Below This type use Query its working

string mindate="01/01/1990";
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? Convert.ToDateTime(mindate) : Convert.ToDateTime(review_date));
Posted

The (DateTime?)null option is close - the code will compile, but the parameter won't be passed to the query. If the parameter had a default value of Null, the code would work; otherwise, you'll get a missing parameter error.

In order to pass Null as the parameter value, you have to use DBNull.Value. However, if your ternary expression has return values of DBNull and DateTime, the compiler won't be able to determine the return type of the expression.

The trick is to force the return type to be object, by explicitly boxing the DateTime value. (The overhead of boxing the value is irrelevant, since it's being passed to a method parameter of type object, so it would need to be boxed anyway.)

This should work:
C#
cmd.Parameters.AddWithValue("@review_date", string.IsNullOrEmpty(review_date) ? DBNull.Value : (object)Convert.ToDateTime(review_date));
 
Share this answer
 
check following condition........Hope this helps
if(review_date!=null)
{
cmd.Parameters.AddWithValue("@review_date",Convert.ToDateTime(review_date));
}
else
{
//Whatever you want to send
}
 
Share this answer
 
v2
This should do it:
cmd.Parameters.AddWithValue("@review_date", 
  String.IsNullOrEmpty(review_date) ? DBNull.Value : Convert.ToDateTime(review_date)); 
 
Share this answer
 
try this

cmd.Parameters.AddWithValue("@review_date",Convert.ToDateTime(review_date == null?DateTime.MinValue:review_date));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900