0

我有一个 Windows 应用程序,我想在其中填写采购订单。它有一个名为 uom 的字段。现在我正在使用参数化查询插入。但我收到了这个错误

必须声明标量变量@uom。

请帮忙。

private void btnsave_Click(object sender, EventArgs e)
{
    con.Open();
    if (IsAllValid())
    {
        cmd.CommandText = "insert into purchaseorder(req_no,purchaseorder_no,purchaseorder_date,costcenter_id,vendor_id,payment_terms,delivery_schedule,emp_id,commercial_name) values(@req_no,@purchaseorder_no,@purchaseorder_date,@costcenter_id,@vendor_id,@payment_terms,@delivery_schedule,@emp_id,@commercial_name)";
        cmd.Parameters.AddWithValue("@req_no", cmbreqno.SelectedValue);
        cmd.Parameters.AddWithValue("@purchaseorder_no", txtpno.Text);
        cmd.Parameters.AddWithValue("@purchaseorder_date", dtpo.Value);
        cmd.Parameters.AddWithValue("@costcenter_id", Convert.ToString(txtcc.Tag));
        cmd.Parameters.AddWithValue("@vendor_id", cmbvendorname.SelectedValue);
        cmd.Parameters.AddWithValue("@payment_terms", txtpaymentterms.Text);
        cmd.Parameters.AddWithValue("@delivery_schedule", txtdeliveryschedule.Text);
        cmd.Parameters.AddWithValue("@emp_id", cmbcontactperson.SelectedValue);
        cmd.Parameters.AddWithValue("@commercial_name", txtcommercial.Text);
        cmd.ExecuteScalar();


        foreach (var item in poitemlist)
        {
            cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)values(@uom,@item_id,@quantity,@item_cost,@total_cost)";
            cmd.Parameters.AddWithValue("@item_id", item.Item_ID.ToString());
            cmd.Parameters.AddWithValue("@uom", item.uom.ToString());
            cmd.Parameters.AddWithValue("@quantity", item.Quantity.ToString());
            cmd.Parameters.AddWithValue("@item_cost", item.price.ToString());
            cmd.Parameters.AddWithValue("@total_cost", item.TotalCost.ToString());
            cmd.Parameters.Clear();
            cmd.ExecuteScalar();
        }

        MessageBox.Show("Purchase Order saved", "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    else
    {
        MessageBox.Show("error", "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }


    con.Close();
}
4

2 回答 2

2

Move the line that clear the parameter collection after the ExecuteScalar

   cmd.ExecuteScalar();
   cmd.Parameters.Clear();

By the way, if you have many items to insert, probably is better to have a StoredProcedure, or get a little optimization moving the creation of the parameters outside the loop and setting only the value inside the loop

cmd.Parameters.Clear();
cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)" + 
                  "values(@uom,@item_id,@quantity,@item_cost,@total_cost)";

// Create the parameters collection with fake values 
// It is better to use the Add with the specific datatype, 
// but for this example I will use AddWithValue
cmd.Parameters.AddWithValue("@item_id", string.Empty);
cmd.Parameters.AddWithValue("@uom", string.Empty);
cmd.Parameters.AddWithValue("@quantity", string.Empty);
cmd.Parameters.AddWithValue("@item_cost", string.Empty);
cmd.Parameters.AddWithValue("@total_cost", string.Empty);
foreach (var item in poitemlist)
{
    cmd.Parameters["@item_id"].Value =, item.Item_ID.ToString();
    cmd.Parameters["@uom"].Value = item.uom.ToString();
    cmd.Parameters["@quantity"].Value = item.Quantity.ToString();
    cmd.Parameters["@item_cost"].Value = item.price.ToString();
    cmd.Parameters["@total_cost"].Value = item.TotalCost.ToString();
    cmd.ExecuteScalar();
}
于 2013-05-15T08:46:47.073 回答
1

Remove this line

cmd.Parameters.Clear(); 

from

foreach (var item in poitemlist)
        {
            cmd.CommandText = "insert into purchaseorderitem(uom,item_id,quantity,item_cost,total_cost)values(@uom,@item_id,@quantity,@item_cost,@total_cost)";
            cmd.Parameters.AddWithValue("@item_id", item.Item_ID.ToString());
            cmd.Parameters.AddWithValue("@uom", item.uom.ToString());
            cmd.Parameters.AddWithValue("@quantity", item.Quantity.ToString());
            cmd.Parameters.AddWithValue("@item_cost", item.price.ToString());
            cmd.Parameters.AddWithValue("@total_cost", item.TotalCost.ToString());
            cmd.Parameters.Clear();
            cmd.ExecuteScalar();
        }
于 2013-05-15T08:47:37.267 回答