behnam
behnam

Reputation: 1143

The parameterized query expects the parameter '@Id', which was not supplied

I am writing a software I use method (parameters.add) but when I am adding data to a database I get the error below:

The parameterized query expects the parameter '@Id', which was not supplied.

I saw several topic but I can't solve my problem.

The parameterized query expects the parameter ###### which was not supplied

The parameterized query expects the parameter , which was not supplied

I put c# code and stored procedure code below:

private void btnSave_Click(object sender, EventArgs e)
        {
            string cs = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TranscactionAccountNumber;Data Source=DESKTOP-5DJFFQP";
            string name = txtName.Text;
            int shomarepeygiri = int.Parse(txtShomarePeygiri.Text);
            string date = dtpDate.Shamsi;
            decimal mablagh = decimal.Parse(txtMablagh.Text);
            string comment = txtComment.Text;
            using (cn = new SqlConnection(cs))
            using (cmd = new SqlCommand("InsertTransaction", cn))
            {
                cmd.Parameters.Add("@Id",SqlDbType.Int);
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = name;
                cmd.Parameters.Add("@ShomarePeygiri", SqlDbType.Int).Value = shomarepeygiri;
                cmd.Parameters.Add("@Mablagh", SqlDbType.Decimal).Value = mablagh;
                cmd.Parameters.Add("@Comment", SqlDbType.NVarChar).Value = comment;
                cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Transaction Added Successfully..", "Register Transaction", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

Create procedure InsertTransaction
(
@id int,
@Name nvarchar(100),
@ShomarePeygiri int,
@Mablagh decimal(18, 0),
@Comment nvarchar(MAX),
@PersianDate varchar(10)

) 
AS
Insert Into TransactionTable(id,[Name],ShomarePeygiri,Mablagh,Comment,PersianDate)
 values (@id,@Name,@ShomarePeygiri,@Mablagh,@Comment,@PersianDate)

Upvotes: 2

Views: 8313

Answers (2)

mansi
mansi

Reputation: 877

Alter your procedure as below

Alter procedure InsertTransaction
(
@Name nvarchar(100),
@ShomarePeygiri int,
@Mablagh decimal(18, 0),
@Comment nvarchar(MAX),
@PersianDate varchar(10)
) 
AS
Insert Into TransactionTable([Name],ShomarePeygiri,Mablagh,Comment,PersianDate)
values (@Name,@ShomarePeygiri,@Mablagh,@Comment,@PersianDate)

Upvotes: 2

mybirthname
mybirthname

Reputation: 18127

cmd.Parameters.Add("@Id",SqlDbType.Int);

You are Adding a parameter, but you are not giving a value for it. Because of that you are receiving this error.

If the ID column is Auto Incremented just remove it from the Stored Procedure.

EDIT: If you don't know how to create the column auto incremented check this answer: Auto increment primary key in SQL Server Management Studio 2012

Upvotes: 8

Related Questions