Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
This is my stored procedure:
create PROCEDURE insertEmp(@Id int output,@Name varchar(50),@Sal money) AS
begin
    insert into EMP1(Empname,EmpSalary)values (@Name,@Sal)
    select @Id=@@Identity
END
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace SP2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
                InitializeComponent();
        }
 
	private void button1_Click(object sender, EventArgs e)
	{
		SqlConnection con = new SqlConnection(helpercs.connectionstring);
		SqlCommand cmd = new SqlCommand("insertEmp", con);
		cmd.CommandType=CommandType.StoredProcedure;
		SqlParameter parId, parName, parSalary;
		parId = cmd.Parameters.Add("@Id", SqlDbType.Int);
		parId.Direction = ParameterDirection.Output;
		parName = cmd.Parameters.Add("@name",SqlDbType.VarChar,50);
		parSalary = cmd.Parameters.Add("@salary",SqlDbType.Money);
		parName.Value =int.Parse(txtname.Text);
		parSalary.Value =int.Parse( txtsalary.Text);
		con.Open();
		cmd.ExecuteNonQuery();
		con.Close();
		txtid.Text = parId.Value.ToString();
	}
    }
}
Please help me, I tried for about 2 hours.
Posted 21-Dec-12 7:35am
Edited 21-Dec-12 7:49am
v2
Comments
Surendra0x2 at 21-Dec-12 12:39pm
   
You Posted This Question Twice :|
__TR__ at 21-Dec-12 12:52pm
   
Don't Repost[^] your question. Use "Imporve Question" widget to update your question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

parSalary = cmd.Parameters.Add("@salary",SqlDbType.Money);
should be
parSalary = cmd.Parameters.Add("@Sal",SqlDbType.Money);
As you are passing @Sal money to your stored procedure.
 
Both parameters (C# & SQL SP's) should match.
  Permalink  
v2
Comments
okishore7 at 21-Dec-12 12:44pm
   
yes i am passing
Member 9581488 at 21-Dec-12 12:46pm
   
did it work?
okishore7 at 21-Dec-12 12:53pm
   
not working same problem
Member 9581488 at 21-Dec-12 12:55pm
   
Did you change??
parSalary = cmd.Parameters.Add("@Sal",SqlDbType.Money);
 
or else try running it in SQL
exec insertEmp 'test',1000 --(see if it has any error)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

the error could because of this conversion change this in addition to the above said parameter change.
parName.Value =int.Parse(txtname.Text);
parSalary.Value =int.Parse( txtsalary.Text);
to
parName.Value =txtname.Text;
parSalary.Value =int.Parse( txtsalary.Text);
here int.Parse returns int was assigned to the name field which is Varchar type.
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.150302.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100