当我从组合框(cb_oname)中选择一个项目并输入顺序时,程序崩溃并在executenonquery()处给出错误。我在下面突出显示 实际上,当我单击提交订单按钮时,此代码会运行。 cb_ocat是其数据类型为vnarchar(50)
的项的类别string query = @"Insert into dbo.orders
(OrderType,OrderID,Product_Name, Product_category,Product_Quantity,
Product_Price,Date,Discount,Order_Price,Phone) values
(@txt_rdvalue,@txt_orderid,@cb_oname,@cb_ocat,@cb_oqty,
@txt_oprice,@Date,@txt_disc,@txt_orderprice,@txt_call)";
if (string.IsNullOrWhiteSpace(txt_rdvalue.Text) || string.IsNullOrWhiteSpace(txt_orderid.Text) || string.IsNullOrWhiteSpace(cb_oname.Text) || string.IsNullOrWhiteSpace(cb_ocat.Text) || string.IsNullOrWhiteSpace(cb_oqty.Text) || string.IsNullOrWhiteSpace(txt_oprice.Text) || string.IsNullOrWhiteSpace(txt_disc.Text))
{
lbl_incorrect.Visible = true;
}
else
{
using (SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Pizza Mania;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.Parameters.Add(new SqlParameter("@Date", dateTimePicker1.Value.Date));
cmd.Parameters.AddWithValue("@txt_rdvalue", txt_rdvalue.Text);
cmd.Parameters.AddWithValue("@txt_orderid", Convert.ToDouble(txt_orderid.Text));
cmd.Parameters.AddWithValue("@cb_oname", cb_oname.SelectedItem);
cmd.Parameters.AddWithValue("@cb_ocat", cb_ocat.SelectedItem);
cmd.Parameters.AddWithValue("@cb_oqty", Convert.ToDouble(cb_oqty.SelectedItem));
cmd.Parameters.AddWithValue("@txt_oprice", Convert.ToDouble((txt_oprice.Text)));
cmd.Parameters.AddWithValue("@txt_disc", Convert.ToDouble(txt_disc.Text));
cmd.Parameters.AddWithValue("@txt_orderprice", txt_orderprice.Text);
cmd.Parameters.AddWithValue("@txt_call", txt_call.Text);
if (txt_call.Text == null)
{
cmd.Parameters.AddWithValue(@"txt_call", txt_call.Text == null);
}
cmd.ExecuteNonQuery();// here i am getting the error that the parameterized query cb_' expects the parameter cb_ocat which was not supplied.
//MessageBox.Show("Order Inserted");
con.Close();
}
答案 0 :(得分:1)
对此不太确定,但
如果cb_oact是字符串,请尝试将其转换为String cmd.Parameters.AddWithValue(" @ cb_ocat",cb_ocat.SelectedItem.ToString()); 希望这有效