插入时有界Combobox consraint值错误

时间:2016-11-30 19:59:33

标签: c# sql-server combobox

我已将3个组合框分别绑定到3个不同的表格。每当我选择metroComboBox2的值时,它都会给我这个错误" Column' Cor_ID'被限制为独特的。价值观' 103'已经存在了。"我不知道为什么会出现这个错误。导致此错误的原因是什么?

con = new SqlConnection(constr);
        con.Open();
        DataTable dt = new DataTable();
        String query = "Insert into Teacher (ID,Name,FName,Age,Qualification,Dep_ID,Cor_ID,Sem_ID,Password) VALUES(@ID,@Name,@FName,@Age,@Qualification,@Dep_ID,@Cor_ID,@Sem_ID,@Password)";

        cmd = new SqlCommand(query, con);


        cmd.Parameters.AddWithValue("@ID", metroTextBox1.Text);
        cmd.Parameters.AddWithValue("@Name", metroTextBox2.Text);
        cmd.Parameters.AddWithValue("@FName", metroTextBox3.Text);
        cmd.Parameters.AddWithValue("@Age", metroTextBox4.Text);
        cmd.Parameters.AddWithValue("@Qualification", metroTextBox5.Text);
        cmd.Parameters.AddWithValue("@Dep_ID", metroComboBox1.GetItemText(metroComboBox1.SelectedItem));
        cmd.Parameters.AddWithValue("@Cor_ID", metroComboBox2.GetItemText(metroComboBox2.SelectedItem));
        cmd.Parameters.AddWithValue("@Sem_ID", metroComboBox3.GetItemText(metroComboBox3.SelectedItem));
        cmd.Parameters.AddWithValue("@Password", metroTextBox7.Text);

        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Record Inserted");
}

0 个答案:

没有答案