必须声明标量变量“@uom”

时间:2013-05-15 08:42:59

标签: c# winforms

我有一个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();
}

2 个答案:

答案 0 :(得分:2)

在ExecuteScalar

之后移动清除参数集合的行
   cmd.ExecuteScalar();
   cmd.Parameters.Clear();

顺便说一下,如果你要插入很多项目,最好有一个StoredProcedure,或者稍微优化一下,在循环外部创建参数并只设置循环内的值

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();
}

答案 1 :(得分:1)

删除此行

cmd.Parameters.Clear(); 

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();
        }