how to pass default value to date parametere in storedprocedure - Sqlserver 2008?

半城伤御伤魂 提交于 2019-12-02 08:26:16

In addition to sachin's answer i would recomed you to use datepicker control or validate textbox to only accept date values using validators.

1- How to pass default date value:

A parameter ,with default value, inside SP makes it optional. You may set a default inside SP, for a parameter and do not pass the parmeter at all when calling the SP from code, in that way it would consider your default value.

In your SP

@pdoj date='1111-1-1' -- Make sure to use a valid date. The min valid date for SQL server is `1753-1-1`

Is an optional parameter, when you do not have any value available at UI, then do not pass the parameter while calling SP, hence it would consider the default value.

2- Make sure, not to pass empty string/invalid date instead, just bypass the adding date parameter when no date is entered by user OR validate it as per your business rule

So, adding date parameter should be added conditionally, something like this:

if(  IsValidDate(txtdoj.Text) )
{
cmd.Parameters.Add(new SqlParameter("@pdoj", SqlDbType.Date)).Value = Convert.ToDateTime(txtdoj.Text);
}

//* IsValidDate is just a hint that you need to validate date as per business rule.. wither to consider empty date as invalid or allow default date to be considered.

You can set the default parameter of date type as follows:

@Date Date = 'october 10, 2012'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!