You are on page 1of 10

Partial Public Class _Default Inherits System.Web.UI.

Page Dim objdatos As New GestorBD =============================================================== #Region "Controles" Sub Listado() Me.GridView1.DataSource = objdatos.Consultas_SQL("Sp_Listado") Me.GridView1.DataBind() End Sub Sub Bloquear() Me.txtCodigo.Enabled = False Me.txtPaterno.Enabled = False Me.txtCuota.Enabled = False Me.txtFecha.Enabled = False End Sub Sub Desbloquear() Me.txtPaterno.Enabled = True Me.txtCuota.Enabled = True Me.txtFecha.Enabled = True End Sub Sub Limpiar() Me.txtCodigo.Text = "" Me.txtPaterno.Text = "" Me.txtCuota.Text = ""

Me.txtFecha.Text = "" End Sub #End Region ====================================================================== Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Listado() Bloquear() End Sub ========================================================================= Protected Sub BtnGrabar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnGrabar.Click Dim NewCodigo As String NewCodigo = objdatos.AutogeneraCodigo objdatos.Mantenimiento_SQL _ ("Sp_InsertaReg", NewCodigo, Me.TxtPaterno.Text, Me.TxtCuota.Text, Me.TxtFecha.Text) Listado() Me.lblMensaje.Text = "DATOS INGRESADOS CORRECTAMENTE" End Sub ========================================================================= Protected Sub BtnEliminar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnEliminar.Click objdatos.Mantenimiento_SQL("Sp_EliminaReg", Me.TxtCodigo.Text) Listado() Limpiar() Me.lblMensaje.Text = "SE ELIMINO EL REGISTRO ACTUAL" End Sub ======================================================================== Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged Me.TxtCodigo.Text = Me.GridView1.SelectedRow.Cells(1).Text Me.TxtPaterno.Text = Me.GridView1.SelectedRow.Cells(2).Text Me.TxtCuota.Text = Me.GridView1.SelectedRow.Cells(3).Text Me.TxtFecha.Text = FormatDateTime(Me.GridView1.SelectedRow.Cells(4).Text) Me.lblMensaje.Text = "SELECCIONANDO REGISTRO ACTUAL" End Sub Protected Sub BtnActualizar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnActualizar.Click objdatos.Mantenimiento_SQL _ ("Sp_ActualizaReg", Me.TxtCodigo.Text, Me.TxtPaterno.Text, Me.TxtCuota.Text, Me.TxtFecha.Text) Listado() Me.lblMensaje.Text = "REGISTRO ACTUALIZADO CORRECTAMENTE" End Sub ======================================================================== Protected Sub BtnNuevo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnNuevo.Click Limpiar() Me.TxtCodigo.Text = "Autogenerado" Me.lblMensaje.Text = "INGRESANDO NUEVO REGISTRO" Desbloquear()

End Sub ========================================================================= Protected Sub BtnCancelar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCancelar.Click Bloquear() Limpiar() Me.lblMensaje.Text = "OPERACIN CANCELADA" End Sub ======================================================================== Protected Sub BtnModificar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnModificar.Click Desbloquear() End Sub End Class =========================SQL=============================================
EXISTS(SELECT * FROM sysdatabases WHERE name='BDInstituto') begin DROP DATABASE BDInstituto --GO end --Crear la base de datos BDInstituto Create Database BDInstituto go use BDInstituto go -- Creacion de las Tablas IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE NAME='Alumnos' AND TYPE ='U') DROP table Alumnos GO --Crear la Tabla Alumnos Create Table Alumnos( Codigo_Alum char(5) primary key, Paterno_Alum varchar(30), Cuota_Alum Numeric(5,2), FechaIng_Alum DateTime ) go --SP para ingresar Registros if exists(Select name from Sysobjects where name='Sp_InsertaReg' and type='P') Drop Procedure sp_InsertaReg Go Create Procedure Sp_InsertaReg @Codigo Char(5), @Paterno Varchar(30), @Cuota Numeric(5,2), @Fecha Datetime as Insert into Alumnos values(@Codigo,@Paterno,@Cuota,@Fecha) go Exec Sp_InsertaReg '00001','Mendez',250.00,'12/10/2004' IF

--Sp para listar Registros if exists(Select name from Sysobjects where name='Sp_Listado' and type='P') Drop Procedure sp_Listado go Create Procedure sp_Listado as Select * from Alumnos go --Sp para Eliminar Registros if exists(Select name from Sysobjects where name='Sp_EliminaReg' and type='P') Drop Procedure sp_EliminaReg go Create Procedure Sp_EliminaReg @Cod Char(5) as Delete from Alumnos where Codigo_Alum=@Cod go --Sp para Eliminar Registros if exists(Select name from Sysobjects where name='Sp_ActualizaReg' and type='P') Drop Procedure sp_ActualizaReg go Create Procedure Sp_ActualizaReg @Cod Char(5), @Paterno Varchar(30), @Cuota Numeric(5,2), @Fecha DateTime as Update Alumnos Set Paterno_Alum=@Paterno,Cuota_Alum=@Cuota,FechaIng_Alum=@Fecha where Codigo_Alum=@Cod go --STORED PROCEDURE PARA GENERAR CODIGOS if exists(Select name from Sysobjects where name='sp_AutogeneraCodigo' and type='P') Drop Procedure sp_AutogeneraCodigo go Create Procedure sp_AutogeneraCodigo @Cod char(5) output as Declare @Aux Char(5) Select @Aux=Max(Codigo_Alum) from Alumnos Select @Cod=right(str(Convert(int,@Aux)+100001),5) go --PROBANDO EL SP PARA GENERAR CODIGOS Declare @Cod Char(5) Exec sp_AutogeneraCodigo @Cod output Select @Cod

Public Partial Class WebForm1 Inherits System.Web.UI.Page Dim objdatos As New GestorBD #Region "Controles" Sub Listado() Me.GridView1.DataSource = objdatos.Consultas_SQL("Sp_Listado") Me.GridView1.DataBind() End Sub Sub Bloquear() Me.TxtCodigo1.Enabled = False Me.TxtDescripcion.Enabled = False Me.TxtPrecio.Enabled = False Me.TxtCantidad.Enabled = False Me.TxtUniMedida.Enabled = False End Sub Sub Desbloquear() Me.TxtDescripcion.Enabled = True Me.TxtPrecio.Enabled = True Me.TxtCantidad.Enabled = True Me.TxtUniMedida.Enabled = True End Sub

Sub Limpiar() Me.TxtCodigo1.Text = "" Me.TxtDescripcion.Text = "" Me.TxtPrecio.Text = "" Me.TxtCantidad.Text = "" Me.TxtUniMedida.Text = "" End Sub #End Region Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Listado() Bloquear() End Sub Protected Sub BtnGrabar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnGrabar1.Click Dim NewCodigo As String NewCodigo = objdatos.AutogeneraCodigo objdatos.Mantenimiento_SQL _ ("Sp_InsertaReg", NewCodigo, Me.TxtDescripcion.Text, Me.TxtPrecio.Text, Me.TxtCantidad.Text, Me.TxtUniMedida.Text) Listado() Me.LblMensaje1.Text = "DATOS INGRESADOS CORRECTAMENTE" End Sub Protected Sub BtnEliminar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnEliminar1.Click objdatos.Mantenimiento_SQL("Sp_EliminaReg", Me.TxtCodigo1.Text) Listado() Limpiar() Me.LblMensaje1.Text = "SE ELIMINO EL REGISTRO ACTUAL" End Sub Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged Me.TxtCodigo1.Text = Me.GridView1.SelectedRow.Cells(1).Text Me.TxtDescripcion.Text = Me.GridView1.SelectedRow.Cells(2).Text Me.TxtPrecio.Text = Me.GridView1.SelectedRow.Cells(3).Text Me.TxtCantidad.Text = Me.GridView1.SelectedRow.Cells(4).Text Me.TxtUniMedida.Text = Me.GridView1.SelectedRow.Cells(5).Text Me.LblMensaje1.Text = "SELECCIONANDO REGISTRO ACTUAL" End Sub

Protected Sub BtnActualizar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnActualizar1.Click objdatos.Mantenimiento_SQL _ ("Sp_ActualizaReg", Me.TxtCodigo1.Text, Me.TxtDescripcion.Text, Me.TxtPrecio.Text, Me.TxtCantidad.Text, Me.TxtUniMedida.Text) Listado() Me.LblMensaje1.Text = "REGISTRO ACTUALIZADO CORRECTAMENTE" End Sub Protected Sub BtnNuevo1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnNuevo1.Click Limpiar() Me.TxtCodigo1.Text = "Autogenerado" Me.LblMensaje1.Text = "INGRESANDO NUEVO REGISTRO" Desbloquear() End Sub Protected Sub BtnCancelar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCancelar1.Click Bloquear() Limpiar() Me.LblMensaje1.Text = "OPERACIN CANCELADA" End Sub Protected Sub BtnModificar1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnModificar1.Click Desbloquear() End Sub End Class ========================SQL============================================
USE master go IF EXISTS(SELECT * FROM sysdatabases WHERE name='BDAlmacen') begin DROP DATABASE BDAlmacen end Create Database BDAlmacen go use BDAlmacen go IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE NAME='Productos' AND TYPE ='U') DROP table Productos GO Create Table Productos( Cod_Producto char(5) primary key, descripcion varchar(50), Precio_Unitario Numeric(5,2), Cantidad varchar(3), Unidad_Medida varchar(20), ) go

if exists(Select name from Sysobjects where name='Sp_InsertaReg' and type='P') Drop Procedure sp_InsertaReg go Create Procedure Sp_InsertaReg @Codigo Char(5), @descripcion Varchar(50), @Precio_Unitario Numeric(5,2), @Cantidad varchar(3), @Unidad_Medida varchar(20) as Insert into Productos values(@Codigo,@descripcion,@Precio_Unitario,@Cantidad,@Unidad_Medida) go Exec Sp_InsertaReg '00001','Lacteos',250.00,'250','ltrs' --Sp para listar Registros if exists(Select name from Sysobjects where name='Sp_Listado' and type='P') Drop Procedure sp_Listado go Create Procedure sp_Listado as Select * from Productos go --Sp para Eliminar Registros if exists(Select name from Sysobjects where name='Sp_EliminaReg' and type='P') Drop Procedure sp_EliminaReg go Create Procedure Sp_EliminaReg @Cod Char(5) as Delete from Productos where Cod_Producto=@Cod go --Sp para Eliminar Registros if exists(Select name from Sysobjects where name='Sp_ActualizaReg' and type='P') Drop Procedure sp_ActualizaReg go Create Procedure Sp_ActualizaReg @Codigo Char(5), @descripcion Varchar(50), @Precio_Unitario Numeric(5,2), @Cantidad varchar(3), @Unidad_Medida varchar(20) as Update Productos Set descripcion=@descripcion,Precio_Unitario=@Precio_Unitario,Cantidad=@Cantidad, Unidad_Medida=@Unidad_Medida where Cod_Producto=@Codigo go

--STORED PROCEDURE PARA GENERAR CODIGOS if exists(Select name from Sysobjects where name='sp_AutogeneraCodigo' and type='P') Drop Procedure sp_AutogeneraCodigo go Create Procedure sp_AutogeneraCodigo @Cod char(5) output as Declare @Aux Char(5) Select @Aux=Max(Cod_Producto) from Productos Select @Cod=right(str(Convert(int,@Aux)+100001),5) go --PROBANDO EL SP PARA GENERAR CODIGOS Declare @Cod Char(5) Exec sp_AutogeneraCodigo @Cod output Select @Cod

=========================GESTOR============================================= Public Function AutogeneraCodigo() As String Dim cn As New SqlConnection(conex) Dim cmd As New SqlCommand("sp_AutogeneraCodigo", cn) cmd.CommandType = CommandType.StoredProcedure cn.Open() cmd.Parameters.Add("@Cod", SqlDbType.Char, 5).Direction = ParameterDirection.Output cmd.ExecuteNonQuery() Dim NewCodigo As String NewCodigo = cmd.Parameters(0).Value.ToString cn.Close() Return NewCodigo End Function End Class

You might also like