我的窗口上有一个组合框。我正在使用参数化查询来插入组合框中的项目。我想验证我的控制。如果我没有选择任何项目,那么应显示消息框"请选择项目"或者如果我选择项目,那么我在组合框中选择的项目应插入到我的数据库表中。我试过我的代码如下。但我收到一条错误消息
必须声明标量变量@ProductCategory。
if (comboproductcategory.SelectedIndex < 0)
{
MessageBox.Show("Please select the item");
}
else
{
cmd.Parameters.AddWithValue("@ProductCategory", comboproductcategory.SelectedItem);
}
请指导我如何解决这个问题。
答案 0 :(得分:0)
尝试将代码更改为:
if (comboproductcategory.SelectedIndex < 0)
{
cmd.Parameters.AddWithValue("@ProductCategory", DBNull.Value);
MessageBox.Show("Please select the item");
}
else
{
cmd.Parameters.AddWithValue("@ProductCategory", comboproductcategory.SelectedItem);
}