Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problema con INSERT de SQL

Author  Topic 

jotae
Starting Member

3 Posts

Posted - 2009-09-09 : 21:09:45
My App have 3 tables: cursos (ID=PK), meses (ID = FK), pagos (ID =FK).

I try to insert a record with this code:

Private Sub tbGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbGrabar.Click
vFecha = ""
vFecha = txtInicio.Text
vEdad = cbEdad.SelectedItem
vGrupo = cbGrupo.SelectedItem
vDia = cbDia.SelectedItem
vHora = cbHora.SelectedItem
mesactual()
If txtNombre.Text = "" Or txtAcudiente.Text = "" Or vFecha = "" Or vGrupo = "" Or vHora = "" Or vDia = "" Or vEdad = "" Then
MessageBox.Show("Nombre, Acudiente, Grupo, Hora, Inicio, Día o Edad no pueden estar en blanco", "Error en Grabación",

MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
Else
Try
cmd = con.CreateCommand
cmd.Connection = con
cmd.CommandText = "INSERT into cursos
(nombre,edad,colegio,inicio,grupo,dia,hora,acudiente,telof,telcel,telres,email,notas,saldo,total) values
(@nombre,@edad,@colegio,@inicio,@grupo,@dia,@hora,@acudiente,@telof,@telcel,@telres,@email,@notas,0,0)"

cmd.Parameters.Add("@Nombre", SqlDbType.VarChar, 50).Value = txtNombre.Text
cmd.Parameters.Add("@edad", SqlDbType.VarChar, 10).Value = vEdad
cmd.Parameters.Add("@colegio", SqlDbType.VarChar, 50).Value = txtColegio.Text
cmd.Parameters.Add("@inicio", SqlDbType.VarChar, 50).Value = vFecha
cmd.Parameters.Add("@grupo", SqlDbType.VarChar, 3).Value = vGrupo
cmd.Parameters.Add("@dia", SqlDbType.VarChar, 10).Value = vDia
cmd.Parameters.Add("@hora", SqlDbType.VarChar, 12).Value = vHora
cmd.Parameters.Add("@acudiente", SqlDbType.VarChar, 50).Value = txtAcudiente.Text
cmd.Parameters.Add("@telres", SqlDbType.VarChar, 20).Value = txtResidencia.Text
cmd.Parameters.Add("@telof", SqlDbType.VarChar, 20).Value = txtOficina.Text
cmd.Parameters.Add("@telcel", SqlDbType.VarChar, 20).Value = txtCelular.Text
cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = txtEmail.Text
cmd.Parameters.Add("@notas", SqlDbType.VarChar, 100).Value = txtNotas.Text
cmd.ExecuteNonQuery()

cmd.CommandText = "INSERT into meses (id,nombre,monto,mesactual,status) values ('" & vUltimo &
"',@nombre,@monto,@mesactual,'ACTIVO')"
cmd.Parameters.Add("@Nombre", SqlDbType.VarChar, 50).Value = txtNombre.Text
cmd.Parameters.Add("@monto", SqlDbType.Money, 20).Value = txtMensualidad.Text
cmd.Parameters.Add("@mesactual", SqlDbType.VarChar, 12).Value = vMes
cmd.ExecuteNonQuery()
MessageBox.Show("Alumno Creado OK")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
reset()
End Sub


But, receive this error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_meses_cursos". The conflict occurred in database "alumnos", table "dbo.cursos", column 'ID'.


The relations has veryfied and are OK. The tables has no errors. Really is a very simple database.

Please, can help me about this problem. I'm very in SQL.

Jorge E. Villamizar

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-10 : 01:51:51
Before you can insert the value of vUltimo into meses.id you have to make sure this id (value) is already inserted into cursos.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jotae
Starting Member

3 Posts

Posted - 2009-09-10 : 02:14:54
Thanks very much!!!! Solved.

Jorge E. Villamizar
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-10 : 02:56:29
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -