0

I have a stored procedure accepting @from_date = NULL.
I have a variable dayCount. If its value is 0, I have to pass Null as param. Else DateTime.Today.AddDays(dayCount).

My code goes like this.

int dayCount = -7;
SqlParameter[] parameters =
{    
new SqlParameter("@from_date", SqlDbType.VarChar) { Value = (dayCount==0)? null : (DateTime.Today.AddDays(dayCount)) }                        
};

i'm getting error in (dayCount==0)? null : (DateTime.Today.AddDays(dayCount)).

Help

0

1 Answer 1

1

Try this:

int dayCount = -7;
SqlParameter[] parameters =
{
       new SqlParameter("@from_date", SqlDbType.VarChar) 
       { 
            Value = dayCount == 0 ? null : (DateTime?)(DateTime.Today.AddDays(dayCount))
       }
};

DateTime cannot be null (as it is a struct) so you have to cast it to Nullable DataTime to use it in conditional operator.

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.