0
I’m making a people registration form which when I finish entering the data it shows a MessageBox
telling which ID the person was registered with.
For that I use a query
of my dataset
(I’m using Entity Framework model first), but for some reason I’m making an error that "One or more lines contain values that violate non-null, Unique or Foreign-key restrictions." because I think it’s a mistake in the bank code, maybe not.
Follow the query code:
SELECT MAX(ID_Pacientes) as UltimoID FROM Pacientes_TCC
The field ID_Pacientes
is autro increment and is a primary key.
Code where I implement the query:
private void btnInserir_Click(object sender, EventArgs e)
{
Pacientes_TCCTableAdapter TaPaciente = new Pacientes_TCCTableAdapter();
TaPaciente.Insert(txtNome.Text, txtCPF.Text, txtRG.Text, cbxSexo.Text, dtpData.Value, txtCidade.Text, txtEstado.Text, txtBairro.Text, txtCEP.Text,
txtRua.Text, txtNumero.Text, txtCelular.Text, txtFixo.Text, txtEmail.Text, cbxConvenio.Text);
if (MessageBox.Show("Pessoa gravado com o ID" +TaPaciente.UltimoID()+ " Deseja fazer outro cadastro?", "Confirma", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
Close();
}
}
I don’t quite understand, a field
identity
(auto increment) can only be primary key, not foreign...– Ricardo Pontual
Sorry, it was typo, the Id_patients field is Primary key.
– Gabriel Arruda
I get it. Edit your question to leave no doubt.
– Ricardo Pontual
But the message has nothing to do with
select max
, seems to be in Insert. Some field not null is valueless or the value of some FK is invalid. Have you tried debugging and see the values being passed toentity
when doing the?– Ricardo Pontual
the
insert
works normally, although I’ve had trouble with it before, but after insertion I check the fields and this is okay, the Id_patients field and the Fk but it doesn’t go through theinsert
for being auto increment.– Gabriel Arruda
As @Ricardopunctual mentioned, the error is in your
insert
. A simple test is to replace the current parameters of yourinsert
by fixed values.– Ismael