0
     private void btnInsert_Click(object sender, EventArgs e)
     {     
       empCode = txtCode.Text;
       empName = txtName.Text;
       empCell = txtCell.Text;
       empAddress = txtAddress.Text;
        try
        {
            using (cmd = new SqlCommand(" empInsert ", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@empcode", SqlDbType.VarChar).Value = empCode;
                cmd.Parameters.Add("@empname", SqlDbType.VarChar).Value = empName;
                cmd.Parameters.Add("@empcell", SqlDbType.VarChar).Value = empCell;
                cmd.Parameters.Add("@empaddress", SqlDbType.VarChar).Value = empAddress;
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("succesfully inserted", "Congrates");
        }
        catch (Exception ex)
        {
            MessageBox.Show("can't Insert there is error :" + ex, "Error");
        }

        finally
        {
            conn.Close();
        }
    }

Here is Stored procedure on SQL DB side.

use GDK
GO
create PROCEDURE dbo.empInsert

@id     as    VARCHAR(10,
@name as VARCHAR(10),
@cell as  VARCHAR(10),
@address     as   VARCHAR(20) 

AS

BEGIN

INSERT INTO EmployeeRecord(empcode,empname,empcell,empaddress) VALUES( @id, @name,  @cell, @address)   
END

I am unable to INSERT in DB.

Kindly help in this regard

1
  • The parameter names in the application code must match those of the underlying stored procedure in the database that you are calling. Commented Mar 24, 2014 at 19:47

2 Answers 2

1

You have parameter name @id in stored procedure but you are passing @empcode

Change

cmd.Parameters.Add("@empcode", SqlDbType.VarChar).Value = empCode;

To

cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = empCode;
Sign up to request clarification or add additional context in comments.

1 Comment

Actually none of them match. @id is just one example.
0

The problem is that your parameter names do not match.

From MSDN:

When using parameters with a SqlCommand to execute a SQL Server stored procedure, the names of the parameters added to the Parameters collection must match the names of the parameter markers in the stored procedure.

So this is what you need:

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", empCode);
cmd.Parameters.AddWithValue("@name", empName);
cmd.Parameters.AddWithValue("@cell", empCell);
cmd.Parameters.AddWithValue("@address", empAddress);

2 Comments

Thanks....the problem was here... using (cmd = new SqlCommand(" empInsert ", conn))......it was .... using (cmd = new SqlCommand(" dbo.empInsert ", conn))
I would check to see if it is successful when you trim the leading and trailing whitespace off the stored procedure name. That might be the problem as well, try it like this: new SqlCommand("empInsert", conn)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.