我用DataGrid绑定数据库后,能够对数据库操作包括(插入,删除,取消,和修改数据)
但是在修改int类型字段时出现错误,语句如下:
void DataGrid_Update(object sender, DataGridCommandEventArgs e) {        // update the database with the new values        // get the edit text boxes
        int CustomersID= int.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
        string name = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
        // TODO: update the Command value for your application
        SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlCommand UpdateCommand = new SqlCommand();
        UpdateCommand.Connection = myConnection;        if (AddingNew)
            UpdateCommand.CommandText = "INSERT INTO Customers(CustomersID,name) VALUES (@CustomersID, @name)";
        else
            UpdateCommand.CommandText = "UPDATE Customers SET CustomersID=@CustomersID,name = @name where CustomersID=@CustomersID";        UpdateCommand.Parameters.Add("@CustomersID", SqlDbType.Int).Value = CustomersID;
        UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = name;对CustomersID字段更新不了,请问高手这是为什么?

解决方案 »

  1.   

    UpdateCommand.CommandText = "UPDATE Customers SET CustomersID=@CustomersID,name = @name where CustomersID=@CustomersID";
    ---->
    UpdateCommand.CommandText = "UPDATE Customers SET name = @name where CustomersID=@CustomersID";
      

  2.   

    如果CustomersID是 主键 自动增加的ID 是无法更改的
      

  3.   

    你的SQL好象有问题
    把它写出来看看是否正确
      

  4.   

    UpdateCommand.CommandText = "UPDATE Customers SET CustomersID=@CustomersID//这个用@CustomersID1标志
    ,name = @name where CustomersID=@CustomersID//这个用@CustomersID2标志
    ";       
    @CustomersID2和@CustomersID1应该不一样的所以你用 UpdateCommand.Parameters.Add("@CustomersID", SqlDbType.Int).Value = CustomersID;,肯定不对
      

  5.   

    UpdateCommand.Parameters.Add("@CustomersID", SqlDbType.Int).Value = CustomersID;
    UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = name;
    改为:
    UpdateCommand.Parameters.Add("@CustomersID",CustomersID);
    UpdateCommand.Parameters.Add("@name",name);