Ver Mensaje Individual
  #14 (permalink)  
Antiguo 04/11/2011, 12:44
sachis77
 
Fecha de Ingreso: octubre-2011
Ubicación: COLOMBIA, Medellín
Mensajes: 42
Antigüedad: 12 años, 5 meses
Puntos: 0
Respuesta: programacion el visual basic

Hola, este es el codigo que tengo para guardar datos en la tabla "articulos" que en la base de datos es :


CREATE TABLE articulos
(codt CHAR(10),
codsubtp CHAR(10),
refer VARCHAR(50),
nombre VARCHAR(50),
prcosto INTEGER,
existencias INTEGER,
codigo INT(10) not null,
nitp CHAR(10) not null,
PRIMARY KEY (codt, codsubtp, refer),
FOREIGN KEY (codt) REFERENCES tproductos (codt),
FOREIGN KEY (codt, codsubtp) REFERENCES subtprod (codt, codsubtp),
FOREIGN KEY (nitp) REFERENCES proveedores (nitp),
FOREIGN KEY (codigo) REFERENCES medidas (codigo));

y en visual el codigo para guardar lo tengo asi:

Private Sub cmdguardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdguardar.Click
Dim ct As Integer
Dim cs As Integer
Dim ref As String
Dim noma As String
Dim vl As Long
Dim ex As Integer
Dim nit As Integer
Dim cod As Integer
Dim sql As String

If txtctipo.Text.Length = 0 Then
MessageBox.Show("codigo no valido", "ERROR")
txtctipo.Focus()
Exit Sub
End If

If xopcion = 1 Then
Dim xcantidad As Long = -1

miconexion = New MySqlConnection("server=localhost;user=root;passwo rd=111;database=inventariop")

miconexion.Open()

micomando = New MySqlCommand("select count(*)from articulos where refer='" & txtrefer.Text & "'", miconexion)

xcantidad = micomando.ExecuteScalar
miconexion.Close()
If xcantidad > 0 Then
MessageBox.Show("referencia repetida ", "ERROR")
txtctipo.Focus()
Exit Sub
End If
End If

If txtcsubtipo.Text.Length = 0 Then
MessageBox.Show("descripcion no valida", "ERROR")
txtcsubtipo.Focus()
Exit Sub

ElseIf txtrefer.Text.Length = 0 Then
MessageBox.Show("referencia no valida", "ERROR")
txtrefer.Focus()
Exit Sub

ElseIf txtnomart.Text.Length = 0 Then
MessageBox.Show("referencia no valida", "ERROR")
txtnomart.Focus()
Exit Sub

ElseIf txtvalor.Text.Length = 0 Then
MessageBox.Show("valor no valido", "ERROR")
txtvalor.Focus()
Exit Sub

ElseIf txtexistencia.Text.Length = 0 Then
MessageBox.Show("descripcion no valida", "ERROR")
txtexistencia.Focus()
Exit Sub
ElseIf txtnit.Text.Length = 0 Then
MessageBox.Show("nit no valido", "ERROR")
txtnit.Focus()
Exit Sub

ElseIf txtcodigo.Text.Length = 0 Then
MessageBox.Show("referencia no valida", "ERROR")
txtcodigo.Focus()
Exit Sub
End If

ct = Integer.Parse(txtctipo.Text)
cs = Integer.Parse(txtcsubtipo.Text)
ref = txtrefer.Text
noma = txtnomart.Text
vl = Long.Parse(txtvalor.Text)
ex = Integer.Parse(txtexistencia.Text)
nit = Integer.Parse(txtnit.Text)
cod = Integer.Parse(txtcodigo.Text)

If xopcion = 1 Then
sql = ct & "," & cs & ",'" & ref & "','" & noma & "'," & vl & "," & ex & "," & nit & "," & cod & ""

miconexion = New MySqlConnection("server=localhost;user=root;passwo rd=111;database=inventariop")
miconexion.Open()
micomando = New MySqlCommand("insert into articulos values(" & sql & ");", miconexion)

micomando.ExecuteNonQuery()
miconexion.Close()
MessageBox.Show("Registro almacenado con exito", "observacion")

ElseIf xopcion = 2 Then
sql = "update articulos set codigo=" & cod & ", nitp=" & nit & ", existencias=" & ex & ", prcosto=" & vl & " where codt= " & ct & " and codsubtp=" & cs & " and refer='" & ref & "' and nitp=" & nit & " and codigo=" & cod & ""
miconexion = New MySqlConnection("server=localhost;user=root;passwo rd=111;database=inventariop")
miconexion.Open()
micomando = New MySqlCommand(sql, miconexion)
micomando.ExecuteNonQuery()
miconexion.Close()
MessageBox.Show("registro Actualizado con exito", "Observacion")

End If

activar(False)
txtctipo.Clear()
txtcsubtipo.Clear()
txtrefer.Clear()
txtnomart.Clear()
txtvalor.Clear()
txtexistencia.Clear()
txtnit.Clear()
txtcodigo.Clear()

cmdagregar.Enabled = True
cmdguardar.Enabled = False
cmdborrar.Enabled = True
cmdbuscar.Enabled = True
cmdsalir.Enabled = True
cmdeditar.Enabled = True
cmdmayre.Enabled = True
cmdmenre.Enabled = True
cmdsig.Enabled = True
cmdant.Enabled = True
cmddeshace.Enabled = True

End Sub

el error que me aparece es:

Cannot add or update a child row: a foreign key constraint fails (`inventariop`.`articulos`, CONSTRAINT `articulos_ibfk_3` FOREIGN KEY (`nitp`) REFERENCES `proveedores` (`nitp`))


la verdad estoy muy preocupada porque cualquier error me quita mucho tiempo y debo de terminar esta base de datos para hoy , y tanbien realizarle consultas.