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

青春壹個敷衍的年華 提交于 2019-12-04 06:30:00

问题


Here it is my stored procedure:

ALTER proc [dbo].[allrecp]
    @peid int=0,
    @pename varchar(20)='',
    @pdes varchar(20)='',
    @pdoj date='1111-1-1',
    @sal money=0,
    @dept int=0,
    @loc int=0,@i int=0
    as begin
    if(@i=1)
    begin
    insert into Employee values(@pename,@pdes,@pdoj,@sal,@dept,@loc)
    end
    if(@i=2)
    begin
    update Employee set ENAME=@pename,DESEGNATION=@pdes,DOJ=@pdoj,SALARY=@sal,DEPTID=@dept,LOCATIONID=@loc WHERE EMPID=@peid
    end
    if(@i=3)
    delete EMPLOYEE where EMPID=@peid
end

And my c# code is:

private void btndelete_Click(object sender, EventArgs e)
{
    parameteres(3);
}

private void btnupdate_Click(object sender, EventArgs e)
{
    parameteres(2);
}

private void BTNINSERT_Click(object sender, EventArgs e)
{
    parameteres(1);
}

public void parameteres(int i)
{
    cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "allrecp";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@peid", SqlDbType.Int)).Value = txtempid.Text;
    cmd.Parameters.Add(new SqlParameter("@pename", SqlDbType.VarChar, 20)).Value = txtename.Text;
    cmd.Parameters.Add(new SqlParameter("@pdes", SqlDbType.VarChar, 20)).Value = txtdesg.Text;
    cmd.Parameters.Add(new SqlParameter("@pdoj", SqlDbType.Date)).Value = txtdoj.Text;

Here i face the problem:

Failed to convert parameter value from a String to a DateTime

Code:

cmd.Parameters.Add(new SqlParameter("@dept", SqlDbType.Int)).Value = txtdept.Text;
cmd.Parameters.Add(new SqlParameter("@sal", SqlDbType.Money)).Value = txtsal.Text;
cmd.Parameters.Add(new SqlParameter("@loc", SqlDbType.Int)).Value = txtlocation.Text;
cmd.Parameters.Add(new SqlParameter("@i", SqlDbType.Int)).Value = i;
con.Open();
int x = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(x + " rows effected");

here i face the problem:

cmd.Parameters.Add(new SqlParameter("@dept", SqlDbType.Int)).Value = txtdept.Text;

How to pass default values in to stored procedure?


回答1:


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




回答2:


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.




回答3:


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

@Date Date = 'october 10, 2012'


来源:https://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008

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