TONY
TONY
TONY
1
Añadimos una nueva columna IdTrabajador Con la consulta
alter table DETALLE_VENTA
add IdTrabajador int
2
TRABAJADOR
3
Se aplica la regla de eliminación
5
No aplicamos la regla de eliminación
Código de conexión
Imports System.Data.SqlClient
6
End Try
End Function
Inherits conexion
Dim cmd As New SqlCommand
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Return dt
Else
Return Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
desconectado()
End Try
End Function
End Class
7
creamos el formulario frmcliente
Codigo frmcliente
mostrar()
End Sub
Try
Dim func As New fcliente
dt = func.mostrar
datalistado.Columns.Item("Eliminar").Visible = False
8
btnNuevo.Visible = True
btnEditar.Visible = False
buscar()
End Sub
dv.RowFilter = cboCampo.Text & "like '" & txtBuscar.Text & "% '"
Else
Inexistente.Visible = True
datalistado.DataSource = Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'seeter y getter
9
Nombre = value
End Set
End Property
'constructores
End Sub
Public Sub New(ByVal IdCliente As Integer, ByVal Nombre As String, ByVal Apellidos As String, ByVal
Direccion As String, ByVal Telefono As String, ByVal Celular As String, ByVal CI As String)
gIdCliente = IdCliente
gNombre = Nombre
gApellidos = Apellidos
gDireccion = Direccion
10
gTelefono = Telefono
gCelular = Celular
gCI = CI
End Sub
End Class
En SQL hacemos el procedimiento almacenado insertar_cliente
create proc insertar_cliente
@Nombre varchar (50),
@Apellidos varchar (50),
@Direccion varchar (100),
@Telefono varchar (8),
@Celular varchar (8),
@CI varchar (8)
as
insert into CLIENTE (Nombre,Apellidos,Direccion,Telefono,celular,CI) values
(@Nombre,@Apellidos,@Direccion,@Telefono,@celular,@CI)
Go
Volvemos a Visual Basic y creamos una función que nos permita Insertar y Enviar datos desde Visual a SQL
Try
conectado()
cmd = New SqlCommand("insertar_cliente")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@Nombre", dts.gNombre)
cmd.Parameters.AddWithValue("@Apellidos", dts.gApellidos)
cmd.Parameters.AddWithValue("@Direccion", dts.gDireccion)
cmd.Parameters.AddWithValue("@Telefono", dts.gTelefono)
cmd.Parameters.AddWithValue("@Celular", dts.gCelular)
cmd.Parameters.AddWithValue("@CI", dts.gCI)
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
Finally
desconectado()
End Try
End Function
End Sub
12
Private Sub txtApellidos_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles txtApellidos.TextChanged
End Sub
End Sub
End Sub
Creamos el botón Guardar y lo ponemos subre el botón Editar eso será controlado mediante código
Creamos un procedimiento llamado limpiar dentro del frmcliente que se encuentra al medio de…
mostrar()
End Sub
btnGuardar.Visible = True
btnEditar.Visible = False
txtNombre.Text = ""
13
txtApellidos.Text = ""
txtDireccion.Text = ""
txtTelefono.Text = ""
txtCelular.Text = ""
txtCI.Text = ""
txtIdCliente.Text = ""
End Sub
limpiar()
mostrar()
End Sub
If Me.ValidateChildren = True And txtNombre.Text <> "" And txtApellidos.Text <> "" And
txtDireccion.Text <> "" And txtTelefono.Text <> "" And txtCelular.Text <> "" And txtCI.Text <> "" Then
Try
Dim dts As New vcliente
Dim func As New fcliente
dts.gNombre = txtNombre.Text
dts.gApellidos = txtApellidos.Text
dts.gDireccion = txtDireccion.Text
dts.gTelefono = txtTelefono.Text
dts.gCelular = txtCelular.Text
dts.gCI = txtCI.Text
If func.insertar(dts) Then
MessageBox.Show("Cliente Registrado Correctamente", "Guardando Registro",
MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
Else
MessageBox.Show("Cliente No Fue Registrado Correctamente", "Intente de Nuevo",
MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta Ingresar Algunos Datos", "Intente de Nuevo", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
go
volvemos a Visual
Escribimos el código
Private Sub datalistado_CellClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles datalistado.CellClick
txtIdCliente.Text = datalistado.SelectedCells.Item(1).Value
txtNombre.Text = datalistado.SelectedCells.Item(2).Value
txtApellidos.Text = datalistado.SelectedCells.Item(3).Value
txtDireccion.Text = datalistado.SelectedCells.Item(4).Value
txtTelefono.Text = datalistado.SelectedCells.Item(5).Value
txtCelular.Text = datalistado.SelectedCells.Item(6).Value
txtCI.Text = datalistado.SelectedCells.Item(7).Value
btnEditar.Visible = True
btnGuardar.Visible = False
End Sub
End Sub
15
Volvemos a la función fcliente
cmd.Parameters.AddWithValue("@IdCliente", dts.gIdCliente)
cmd.Parameters.AddWithValue("@Nombre", dts.gNombre)
cmd.Parameters.AddWithValue("@Apellidos", dts.gApellidos)
cmd.Parameters.AddWithValue("@Direccion", dts.gDireccion)
cmd.Parameters.AddWithValue("@Telefono", dts.gTelefono)
cmd.Parameters.AddWithValue("@celular", dts.gcelular)
cmd.Parameters.AddWithValue("@CI", dts.gCI)
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
Finally
desconectado()
End Try
End Function
Volvemos a frmcliente
If Me.ValidateChildren = True And txtNombre.Text <> "" And txtApellidos.Text <> "" And
txtDireccion.Text <> "" And txtTelefono.Text <> "" And txtCelular.Text <> "" And txtCI.Text <> "" And
txtIdCliente.Text <> "" Then
Try
Dim dts As New vcliente
Dim func As New fcliente
dts.gIdCliente = txtIdCliente.Text
dts.gNombre = txtNombre.Text
dts.gApellidos = txtApellidos.Text
dts.gDireccion = txtDireccion.Text
dts.gTelefono = txtTelefono.Text
16
dts.gcelular = txtCelular.Text
dts.gCI = txtCI.Text
If func.editar(dts) Then
MessageBox.Show("Cliente Modificado Correctamente", "Modificando Registro",
MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
Else
MessageBox.Show("Cliente no fue Modificado", "Intente de Nuevo",
MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta Ingresar Algunos Datos", "Intente de Nuevo", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End If
End Sub
Try
conectado()
cmd = New SqlCommand("eliminar_cliente")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
17
Volvemos al frmcliente
Click en Agregar..
Y Agregar
If marcado Then
Dim onekey As Integer = Convert.ToInt32(row.Cells("IdCliente").Value)
Dim vdb As New vCLIENTE
Dim func As New fCLIENTE
vdb.gIdCliente = onekey
If func.eliminar(vdb) Then
Else
MessageBox.Show("Cliente No Fue Eliminado", "Intente de Nuevo",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Next
Call mostrar()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Cancelando Eliminación", "Cancelado", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Call mostrar()
End If
Call limpiar()
End Sub
End Class
19
20