Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
**here it is my stored procedure:**
SQL
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:**


C#
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 is:

C#
Failed to convert parameter value from a String to a DateTime

                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 iam face the problem :
                cmd.Para
meters.Add(new SqlParameter("@dept", SqlDbType.Int)).Value = txtdept.Text;

i.e: Failed to convert parameter value from a String to a DateTime. plz give me solution the solution and how to pass default values in to stored procedure.
Posted
Updated 20-Mar-13 22:16pm
v3

Convert.ToDateTime(txtdoj.Text);
 
Share this answer
 
Try Replacing
C#
cmd.Parameters.Add(new SqlParameter("@pdoj", SqlDbType.Date)).Value = txtdoj.Text;

With
C#
DateTime dt;
DateTime.TryParse(txtdoj.Text, out dt);
cmd.Parameters.Add(new SqlParameter("@pdoj", SqlDbType.Date)).Value = dt;



--Amit
 
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