Reputation: 191
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
Upvotes: 0
Views: 184
Reputation: 10026
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);
Upvotes: 0
Reputation: 148110
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;
Upvotes: 1