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

C#
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
Updated 21-Dec-12 6:49am
v2
Comments
Surendra0x2 21-Dec-12 12:39pm    
You Posted This Question Twice :|
__TR__ 21-Dec-12 12:52pm    
Don't Repost[^] your question. Use "Imporve Question" widget to update your question.

C#
parSalary = cmd.Parameters.Add("@salary",SqlDbType.Money);

should be
C#
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.
 
Share this answer
 
v2
Comments
okishore7 21-Dec-12 12:44pm    
yes i am passing
Member 9581488 21-Dec-12 12:46pm    
did it work?
okishore7 21-Dec-12 12:53pm    
not working same problem
Member 9581488 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)
the error could because of this conversion change this in addition to the above said parameter change.
C#
parName.Value =int.Parse(txtname.Text);
parSalary.Value =int.Parse( txtsalary.Text);

to
C#
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.
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900