C# 简单删除语句问题.?

来源:百度知道 编辑:UC知道 时间:2024/05/25 14:22:23
就我删除数据的时候! 显示数据删除成功./
但是数据库中的数据显示已经删除了.!
为什么报这样的错误,请问是什么原因? <SQL数据库表中有数据>

InvalidArgument=“0”的值对于“index”无效。
参数名: index

我的删除代码如下:
//删除数据:
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Database =第七章;User Id=sa;pwd=sa");
String id = this.listView1.SelectedItems[0].SubItems[0].Text;
String strSql = String.Format("delete from dbo.[User] where userId={0}",id);
SqlCommand com = new SqlCommand(strSql,con);
try
{
con.Open();
int result = com.ExecuteNonQuery();
if (result == 1)
{
DialogResult jieguo=MessageBox.Show("您确认要删除吗?","删除提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information );

加一句
if (listView1.SelectedItems.Count > 0)

String id = this.listView1.SelectedItems[0].SubItems[0].Text;
String strSql = String.Format("delete from dbo.[User] where userId={0}",id); //就可以了

索引值有些从0开始有些从1开始

要么数据库没数据
要么user 表的 id 与你输入的类型不匹配