哪位哥帮我改改代码啊?
代码在下面,这是contextMenuStrip控件的删除事件 在调试后在数据点删除,它弹出未将对象引用设置到对象的实例。
我想请教下,应该怎样去改下面的代码?
private void CMdelete_Click(object sender, EventArgs e)
{
DialogResult choice = MessageBox.Show(
"确定要删除该用户吗?", "操作提示", MessageBoxButtons.YesNo
, MessageBoxIcon.Warning);
if (choice == DialogResult.Yes)
{
int resulf = 0;
// string id = listView1.SelectedItems[0].Tag.ToString();
try
{
string sql = string.Format("delete from kuangchunshui where id ='{0}'",listView1.SelectedItems[0].Tag.ToString());
SqlCommand command = new SqlCommand(sql, DBHelper.Connection);
DBHelper.Connection.Open();
resulf = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.Connection.Close();
}
if (resulf < 1)
{
MessageBox.Show("删除失败!", "操作结果", MessageBoxButtons
.OK, MessageBoxIcon.Exclamation);
}
else
{
MessageBox.Show("删除成功", "操作结果", MessageBoxButtons
.OK, MessageBoxIcon.Information);
}
}
}