违反了 PRIMARY KEY 约束 'PK_Admir1'。不能在对象 'dbo.Admir1' 中插入重复键。
语句已终止。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 违反了 PRIMARY KEY 约束 'PK_Admir1'。不能在对象 'dbo.Admir1' 中插入重复键。
语句已终止。源错误: 
行 56:             string sqlIns = "update Admir1 set name='" + TextBox1.Text + "',passwd='" + TextBox2.Text + "',createtime='" + gg + "',ID='" + TextBox3 .Text  + "' ";
行 57:             SqlCommand com = new SqlCommand(sqlIns, con);
行 58:             int count = Convert.ToInt32(com.ExecuteScalar());
行 59:             if (count != 0)
行 60:             {
 
后台代码:
  if (isName())
        {
            Response.Write("<script>alert('id编号已存在,请重试!');</script>");        }
        else
        {
            string gg = DateTime.Now.ToString();
            string str = "Data Source=.;Initial Catalog=shoping;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string sqlIns = "update Admir1 set name='" + TextBox1.Text + "',passwd='" + TextBox2.Text + "',createtime='" + gg + "',ID='" + TextBox3 .Text  + "' ";
            SqlCommand com = new SqlCommand(sqlIns, con);
            int count = Convert.ToInt32(com.ExecuteScalar());
            if (count != 0)
            {
                Response.Write("<script>alert('更新失败!')</script>");
            }
            else
            {
                Response.Write("<script>alert('修改成功!');location.href('../houtai.aspx');</script>");
            }
            string userName = Session["ID"].ToString();
        }
其中ID是主键!!
怎么解决啊!
这个问题我实在没辙;
我改成如下:
    string sqlIns = "update Admir1 set name='" + TextBox1.Text + "',passwd='" + TextBox2.Text + "',createtime='" + gg + "',ID='" + TextBox3 .Text  + "' where  ID='" + TextBox3 .Text  + "'";
可提示了修改成功,然而数据库中数据却没有根本更新!

解决方案 »

  1.   

    string sqlIns = "update Admir1 set name='" + TextBox1.Text + "',passwd='" + TextBox2.Text + "',createtime='" + gg + "',ID='" + TextBox3 .Text + "' ";
    这里应该使用Insert语句
      

  2.   

    修改语句:
      string sqlIns = "update Admir1 set name='" + TextBox1.Text + "',passwd='" + TextBox2.Text + "',createtime='" + gg + "' where ID='" + TextBox3 .Text + "'";
    添加语句
    INSERT INTO ADMIR1 (ID,name,passwd,createtime,)
    VALUES('" + TextBox3 .Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + gg + "');
      

  3.   

    是什么数据库?ID可以设置为自增吗?如果可以设置为自增,在insert的时候就不需要处理他了
      

  4.   

    谁说Update的时候不能更新主键的???
      

  5.   

    Sample测试更新主键。
    create table test
    (
    productID varchar(30) NOT NULL PRIMARY KEY,
    valuesName nvarchar(300) not null
    )
    goinsert into test values ('0300001','无光驱')
    insert into test values ('0300002','集成')SELECT * FROM test t;UPDATE test
    SET
    productID = '0300003',
    valuesName='集成2'
    WHERE productID = '0300001';
      

  6.   

    错误很清楚啊,插入时主键重复!
    “违反了 PRIMARY KEY 约束 'PK_Admir1'。不能在对象 'dbo.Admir1' 中插入重复键”