Buenas chicos estoy haciendo un proyecto, que es un sistema de inventario !!
Tengo problemas con el boton, agregar, modificar y eliminar ! yo uso como base de datos My sql@localhost (MYSQL) ( ese es el nombre que me aparece cuando inicializo el wamp server ) muy bien, hace unos dias podia agregar bien, pero ahora me sale este error ( Error en tabla !!! Duplicate entry '0' for key 'PRIMARY' ) eso me sale cuando le doy al boton GUARDAR !! Es extraño puesto que en estos dias me guardaba perfectamente sin ningun tipo de problemas ! con los botones modificar y agregar, me salia otro error, pero se los comentare unas vez q arregle este problema ! Alguna solucion ?
Este es el codigo de mi boton GUARDAR
Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
'ConsultaSQL.Close()
If txtcodigoclientes.Text = "" Then
MsgBox("El Código no debe estar en blanco!!!")
txtcodigoclientes.Focus()
ElseIf txtnombres.Text = "" Then
MsgBox("El Nombre no debe estar en blanco!!!")
txtnombres.Focus()
ElseIf txtapellidos.Text = "" Then
MsgBox("El Apellido no debe estar en blanco!!!")
txtapellidos.Focus()
ElseIf cbosexo.Text = "Seleccione:" Then
MsgBox("Debe seleccionar un Sexo!!!")
cbosexo.Focus()
ElseIf txtdireccion.Text = "" Then
MsgBox("La direccion no debe estar en blanco!!!")
txtdireccion.Focus()
ElseIf txttelefono.Text = "" Then
MsgBox("El numero de Telefono no puede quedar en blanco !!!")
txttelefono.Focus()
ElseIf txtcelular.Text = "" Then
MsgBox("El numero de Celular no puede quedar en blanco!!!")
txtcelular.Focus()
ElseIf txtemail.Text = "" Then
MsgBox("Debe colocar un email!!!")
txtemail.Focus()
Else
Try
sSqlAgregaClientes = "INSERT INTO clientes " & _
"(nombres_clientes,apellidos_clientes,sexo_clientes,direccion_clientes,telefonos_clientes,celular_clientes,email_clientes) " & _
" VALUES ('" & _
txtnombres.Text & "','" & _
txtapellidos.Text & "','" & _
cbosexo.Text & "','" & _
txtdireccion.Text & "','" & _
txttelefono.Text & "','" & _
txtcelular.Text & "','" & _
txtemail.Text & "')"
' Se crean los comandos para el SQL
cmdClientes = New MySqlCommand()
cmdClientes.Connection = conexionBD
cmdClientes.CommandText = sSqlAgregaClientes
cmdClientes.CommandType = CommandType.Text
ConsultaSQL = cmdClientes.ExecuteReader()
ConsultaSQL.Read()
Dim iNroRegistos As Integer = ConsultaSQL.RecordsAffected
If iNroRegistos = 0 Then
MessageBox.Show("No se grabó el registro en la tabla!!!")
Else
MessageBox.Show("Registro grabado en la tabla!!!")
fLimpiar_textbox()
End If
ConsultaSQL.Close()
Catch ex As Exception
MessageBox.Show("Error en la tabla!!! " & vbCrLf & ex.Message.ToString)
Me.Visible = False
frmMenu.Show()
End Try
End If
End Sub
Y esto de aki abajo es mi codigo del formulario y mi public class
Public Class frmClientes
Public conexionBD As MySqlConnection
Friend ConsultaSQL, ActualizaSQL, EliminaSQL As System.Data.IDataReader
Friend sServidor As String = "localhost"
Friend sUsuario As String = "root"
Friend sClave As String = ""
Friend cmdClientes, cmdActualizado, cmdEliminado As MySqlCommand
Friend dtClientes As New DataTable
Friend sSqlClientes, sSqlAgregaClientes, sLogin, sNombres, sApellidos, sAccion, sSqlEliminaClientes As String
' Declaramos una variable para la tabla en memoria como si fuese un recordset
Private dt As DataTable
Private fila As Integer
Dim BaseDatos As String
Dim iCod_Clientes As Integer
Dim dFecha As Date
Dim sFecha As String
Private Sub frmClientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conexionBD = New MySqlConnection
' Conexion al servidor y a la base de datos
fConexionbd()
'declaro combo box
cbosexo.Items.Add("Masculino")
cbosexo.Items.Add("Femenino")
' Deshabilitar los textbox
fHabilitar_textbox(False)
' Ocultar los botones
fBotones(False)
' Mostrar el boton Buscar
If frmMenu.smnuOpcion = "Agregar" Then
' Limpiar los textbox
fLimpiar_textbox()
sAccion = "I"
lblMensaje.Visible = True
lblMensaje.Text = "Incluya los datos del Cliente"
sSqlClientes = "SELECT * FROM clientes " ' WHERE cod_clientes = '" & txtcodigoclientes.Text & "';"
btnguardar.Visible = True
LeerTabla(sSqlClientes)
ElseIf frmMenu.smnuOpcion = "Actualizar" Then
' Actualizar datos de usuario
' Limpiar los textbox
fLimpiar_textbox()
txtcodigoclientes.Enabled = True
sAccion = "A"
btnbuscar.Visible = True
ElseIf frmMenu.smnuOpcion = "Eliminar" Then
' Actualizar datos de usuario
' Limpiar los textbox
fLimpiar_textbox()
txtcodigoclientes.Enabled = True
sAccion = "E"
btnbuscar.Visible = True
End If
End Sub |