Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
4. Relleno de tablas
4.1. Tabla “Categoria”
6. Generación de consultas
6.1. Consulta para la “Categoria”
create proc mostrar_categoria
as
select * from categoria order by idcategoria desc
go
7.
7.1. Consulta para “ventas”
End Class
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 dni
As String)
gidcliente = idcliente
gnombre = nombre
gapellidos = apellidos
gdireccion = direccion
gtelefono = telefono
gdni = dni
End Sub
End Class
11.2. Creación de nuestro formulario
Imports System.ComponentModel
Private dt As New DataTable
.
.
Pseudocodigo del formulario “frmcliente”
.
.
End Class
f
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ocultar_columnas()
datalistado.Columns(1).Visible = False
End Sub
Private Sub txtnombre_Validating(sender As Object, e As CancelEventArgs) Handles
txtnombre.Validating
If DirectCast(sender, TextBox).Text.Length > 0 Then
Me.erroricono.SetError(sender, "")
Else
Me.erroricono.SetError(sender, "Ingrese el nombre del cliente, dato obligatorio")
End If
End Sub
End Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Guardando registros",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Modificando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
End Sub
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlClient.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
Public Function insertar(ByVal dts As vproducto) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("insertarr_producto")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idcategoria", dts.gidcategoria)
cmd.Parameters.AddWithValue("@nombre", dts.gnombre)
cmd.Parameters.AddWithValue("@descripcion", dts.gdescripcion)
cmd.Parameters.AddWithValue("@precio_compra", dts.gprecio_compra)
cmd.Parameters.AddWithValue("@precio_venta", dts.gprecio_venta)
cmd.Parameters.AddWithValue("@fecha_vencimiento",
dts.gfecha_vencimiento)
cmd.Parameters.AddWithValue("@imagen", dts.gimagen)
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
Public Function editar(ByVal dts As vproducto) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("editar_producto")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idproducto", dts.gidproducto)
cmd.Parameters.AddWithValue("@idcategoria", dts.gidcategoria)
cmd.Parameters.AddWithValue("@nombre", dts.gnombre)
cmd.Parameters.AddWithValue("@descripcion", dts.gdescripcion)
cmd.Parameters.AddWithValue("@precio_compra", dts.gprecio_compra)
cmd.Parameters.AddWithValue("@precio_venta", dts.gprecio_venta)
cmd.Parameters.AddWithValue("@fecha_vencimiento",
dts.gfecha_vencimiento)
cmd.Parameters.AddWithValue("@imagen", dts.gimagen)
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
Public Function eliminar(ByVal dts As vproducto) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("eliminar_producto")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.Add("@idproducto", SqlDbType.NVarChar, 50).Value =
dts.gidproducto
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
Imports System.ComponentModel
imagen.Image = Nothing
imagen.BackgroundImage = My.Resources.transparente
imagen.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub mostrar()
Try
Dim func As New fproducto
dt = func.mostrar
datalistado.Columns.Item("Eliminar").Visible = False
If dt.Rows.Count <> 0 Then
datalistado.DataSource = dt
txtbuscar.Enabled = True
datalistado.ColumnHeadersVisible = True
inexistente.Visible = False
Else
datalistado.DataSource = Nothing
txtbuscar.Enabled = False
datalistado.ColumnHeadersVisible = False
inexistente.Visible = True
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
btnnuevo.Visible = True
btneditar.Visible = False
buscar()
End Sub
Private Sub buscar()
Try
Dim ds As New DataSet
ds.Tables.Add(dt.Copy)
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = cbocampo.Text & " like '" & txtbuscar.Text & "%'"
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ocultar_columnas()
datalistado.Columns(1).Visible = False
datalistado.Columns(2).Visible = False
End Sub
End Sub
If func.insertar(dts) Then
MessageBox.Show("Producto registrado satisfactoriamente",
"Guardando registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
Else
MessageBox.Show("Producto no registrado, intente nuevamente ",
"Guardando registros", MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Guardando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
imagen.BackgroundImage = Nothing
Dim b() As Byte = datalistado.SelectedCells.Item(9).Value
Dim ms As New IO.MemoryStream(b)
imagen.Image = Image.FromStream(ms)
imagen.SizeMode = PictureBoxSizeMode.StretchImage
btneditar.Visible = True
btnguardar.Visible = False
End Sub
If func.editar(dts) Then
MessageBox.Show("Producto modificado satisfactoriamente",
"Modificando registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
mostrar()
limpiar()
Else
MessageBox.Show("Producto no modificado, intente nuevamente
", "Modificando registros", MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Modificando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
End Sub
End Sub
End Class
End Sub
End Sub
End Class
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlClient.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
Public Function insertar(ByVal dts As vventas) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("insertar_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idcliente", dts.gidcliente)
cmd.Parameters.AddWithValue("@fecha_venta", dts.gfecha_venta)
cmd.Parameters.AddWithValue("@tipo_documento", dts.gtipo_documento)
cmd.Parameters.AddWithValue("@num_documento", dts.gnum_documento)
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
Public Function editar(ByVal dts As vventas) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("editar_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idventa", dts.gidventa)
cmd.Parameters.AddWithValue("@idcliente", dts.gidcliente)
cmd.Parameters.AddWithValue("@fecha_venta", dts.gfecha_venta)
cmd.Parameters.AddWithValue("@tipo_documento", dts.gtipo_documento)
cmd.Parameters.AddWithValue("@num_documento", dts.gnum_documento)
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
Public Function eliminar(ByVal dts As vventas) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("eliminar_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.Add("@idventa", SqlDbType.NVarChar, 50).Value =
dts.gidventa
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ocultar_columnas()
datalistado.Columns(1).Visible = False
datalistado.Columns(4).Visible = False
End Sub
End Sub
Else
MessageBox.Show("Venta no registrado, intente nuevamente ",
"Guardando registros", MessageBoxButtons.OK, MessageBoxIcon.Error)
mostrar()
limpiar()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Guardando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Modificando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
frmdetalle_venta.ShowDialog()
End Sub
End Class
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlClient.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
Public Function insertar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("insertar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idventa", dts.gidventa)
cmd.Parameters.AddWithValue("@idproducto", dts.gidproducto)
cmd.Parameters.AddWithValue("@cantidad", dts.gcantidad)
cmd.Parameters.AddWithValue("@precio_unitario", dts.gprecio_unitario)
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
Public Function editar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("editar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@iddetalle_venta", dts.giddetalle_venta)
cmd.Parameters.AddWithValue("@idventa", dts.gidventa)
cmd.Parameters.AddWithValue("@idproducto", dts.gidproducto)
cmd.Parameters.AddWithValue("@cantidad", dts.gcantidad)
cmd.Parameters.AddWithValue("@precio_unitario", dts.gprecio_unitario)
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
Public Function eliminar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("eliminar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.Add("@iddetalle_venta", SqlDbType.NVarChar, 50).Value =
dts.giddetalle_venta
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlClient.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
Public Function insertar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("insertar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idventa", dts.gidventa)
cmd.Parameters.AddWithValue("@idproducto", dts.gidproducto)
cmd.Parameters.AddWithValue("@cantidad", dts.gcantidad)
cmd.Parameters.AddWithValue("@precio_unitario", dts.gprecio_unitario)
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
Public Function editar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("editar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@iddetalle_venta", dts.giddetalle_venta)
cmd.Parameters.AddWithValue("@idventa", dts.gidventa)
cmd.Parameters.AddWithValue("@idproducto", dts.gidproducto)
cmd.Parameters.AddWithValue("@cantidad", dts.gcantidad)
cmd.Parameters.AddWithValue("@precio_unitario", dts.gprecio_unitario)
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
Public Function eliminar(ByVal dts As vdetalle_venta) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("eliminar_detalle_ventas")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.Add("@iddetalle_venta", SqlDbType.NVarChar, 50).Value =
dts.giddetalle_venta
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
Imports System.ComponentModel
Public Class frmdetalle_venta
Private dt As New DataTable
Private Sub frmdetalle_venta_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
mostrar()
End Sub
Public Sub limpiar()
btnguardar.Visible = True
txtidproducto.Text = ""
txtnombre_producto.Text = ""
txtprecio_unitario.Text = ""
txtcantidad.Text = 0
End Sub
Private Sub mostrar()
Try
Dim func As New fdetalle_venta
dt = func.mostrar
datalistado.Columns.Item("Eliminar").Visible = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ocultar_columnas()
datalistado.Columns(1).Visible = False
datalistado.Columns(2).Visible = False
datalistado.Columns(3).Visible = False
End Sub
End Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Guardando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Else
MessageBox.Show("Articulo fue quitado de la venta",
"Eliminando registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Next
Call mostrar()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Cancelando eliminacion de registros", "Eliminando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call mostrar()
End If
Call limpiar()
End Sub
End Sub
End Sub
End Sub
Public Sub New(ByVal idcategoria As Integer, nombre_categoria As String)
gidcategoria = idcategoria
gnombre_categoria = nombre_categoria
End Sub
End Class
cmd.Connection = cnn
If cmd.ExecuteNonQuery Then
Dim dt As New DataTable
Dim da As New SqlClient.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
Public Function insertar(ByVal dts As vcategoria) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("insertar_categoria")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@nombre_categoria", dts.gnombre_categoria)
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
Public Function editar(ByVal dts As vcategoria) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("editar_categoria")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.AddWithValue("@idcategoria", dts.gidcategoria)
cmd.Parameters.AddWithValue("@nombre_categoria", dts.gnombre_categoria)
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
Public Function eliminar(ByVal dts As vcategoria) As Boolean
Try
conectado()
cmd = New SqlClient.SqlCommand("eliminar_categoria")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = cnn
cmd.Parameters.Add("@idcategoria", SqlDbType.NVarChar, 50).Value =
dts.gidcategoria
If cmd.ExecuteNonQuery Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
Imports System.ComponentModel
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ocultar_columnas()
datalistado.Columns(1).Visible = False
End Sub
End Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Guardando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Try
Else
MessageBox.Show("Falta ingresar algunos de los datos", "Modificando
registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub