当您从列表框中选择项目时,您想删除选定的项目。为什么从数据库中删除选定的数据时它不起作用?我肯定错过了什么。我收到错误消息 没有来自对象类型的映射。
这是一个方法参数:
IsDelete = _dinnerRemover.RemoveDinners(lstDinner.SelectedItems);
这个类是从数据库中删除数据
public bool RemoveDinners(dynamic dinnerItems)
{
Dinners = new List<FoodInformation>();
using (var sqlConn = new SqlConnection(_sqlConnectionString))
{
const string sqlQuery = "delete from DinnerTemplates where Dinner = @dinner";
using (var command = new SqlCommand(sqlQuery, sqlConn))
{
try
{
//command.CommandType = CommandType.StoredProcedure;
//command.CommandText = "sp_dinner";
foreach (var item in dinnerItems)
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@dinner", item);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlConn.Close();
}
}
}
return Dinners;
}