Está en la página 1de 5

3RA CLASE ADONET CRISTHIAN SANCHEZ

FORMULARIO 01 CONSULTA DE PRODUCTOS POR CATEGORIAS DATASET - DESCONECTADO

Public Class Form1 Dim dtCategorias As New DataTable Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dtCategorias = DevuleveCategorias() cmbCategorias.DataSource = dtCategorias cmbCategorias.DisplayMember = "NombreCategora" 'para el Listbox LlenarCategorias(lstCategorias) End Sub Private Sub cmbCategorias_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCategorias.SelectedIndexChanged Dim index As Integer = cmbCategorias.SelectedIndex Dim codigo As Integer = dtCategorias.Rows(index).Item(0) dgProductos.DataSource = DevuelveProductos(codigo) End Sub End Class

FORMULARIO NRO 03 MANTENIMIENTO CON PROCEDIMIENTOS ALMACENADOS

Public Class Form3 Dim registro, sw As Integer Public Sub cargaDatos(ByVal fila As Integer) With ds.Tables(0).Rows(fila) txtCodigo.Text = .Item(0).ToString txtApe.Text = .Item(1).ToString txtNom.Text = .Item(2).ToString txtSueldo.Text = .Item(3).ToString dtFIngre.Value = .Item(4) End With End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'insert into persona values('01','Sanchez','Fatima',2850,'12/12/2010') RUNsql("Select * from persona") registro = 0 cargaDatos(registro) End Sub Private Sub Proceso(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click Select Case sender.name Case "btn1" : registro = 0 '<< Case "btn4" : registro = ds.Tables(0).Rows.Count - 1 '>> Case "btn2" registro -= 1 If registro < 0 Then registro = 0 Case "btn3" registro += 1 If registro > ds.Tables(0).Rows.Count - 1 Then

registro = ds.Tables(0).Rows.Count - 1 End If End Select cargaDatos(registro) End Sub Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click sw = 0 txtCodigo.Enabled = False Dim num As Integer txtCodigo.Clear() : txtApe.Clear() : txtNom.Clear() : txtSueldo.Clear() dtFIngre.Value = Today registro = ds.Tables(0).Rows.Count - 1 num = Integer.Parse(ds.Tables(0).Rows(registro).Item(0).ToString.Substring(0, 2)) txtCodigo.Text = (num + 1).ToString("00") txtApe.Focus() End Sub Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click sw = 1 txtCodigo.Enabled = False MsgBox("Puede Modificar") End Sub Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click Dim fecha As String = dtFIngre.Value.Date.ToString("MM/dd/yyyy") Dim sql As String If sw = 0 Then 'se desea insertar, es uno nuevo sql = "Insert Persona values(@IdPer,@Ape,@Nom,@Sue,@Fingre)" Else 'el sw=1, y se dese actualizar sql = "Update Persona Set Ape=@Ape,Nom=@Nom,Sue=@Sue," sql = sql + "Fingre=@Fingre Where IdPer=@IdPer" End If cn.Open() cmd.CommandType = CommandType.Text cmd.CommandText = sql cmd.Connection = cn With cmd.Parameters 'cargo los valores a los parametros asignados .Add("@IdPer", SqlDbType.VarChar, 2).Value = txtCodigo.Text .Add("@Ape", SqlDbType.VarChar, 25).Value = txtApe.Text .Add("@Nom", SqlDbType.VarChar, 25).Value = txtNom.Text .Add("@Sue", SqlDbType.Decimal).Value = Double.Parse(txtSueldo.Text) .Add("@Fingre", SqlDbType.DateTime).Value = dtFIngre.Value.Date End With Dim n As Integer = cmd.ExecuteNonQuery If n = 1 And sw = 0 Then : MsgBox("Registro Grabado") ElseIf n = 1 And sw = 1 Then : MsgBox("registro Actualizado") Else : MsgBox("Error de la Transaccion") End If cmd.Parameters.Clear() 'limpiar los parametros cn.Close() RUNsql("Select * from Persona") End Sub Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click If sw = 0 Then

registro = 0 End If cargaDatos(registro) End Sub Private Sub btnBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrar.Click Dim sql As String If MessageBox.Show("Seguro de BORRAR?", "Aviso", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _ Windows.Forms.DialogResult.Yes Then sql = "Delete Persona Where IdPer='" & txtCodigo.Text & "'" cn.Open() cmd.CommandType = CommandType.Text cmd.CommandText = sql cmd.Connection = cn Dim n As Integer = cmd.ExecuteNonQuery cn.Close() If n = 1 Then MessageBox.Show("Registro eliminado con Exito", "AVISO") Else MessageBox.Show("Problemas de Eliminacion", "AVISO") End If RUNsql("select * from persona") End If End Sub End Class

CREAMOS UN MODULO Imports System.Data Imports System.Data.SqlClient Imports System.Configuration.ConfigurationManager Module MdGeneral Public cn As New SqlConnection(ConnectionStrings("cnx").ConnectionString) Public da As New SqlDataAdapter Public cmd As New SqlCommand Public ds As New DataSet 'procedimiento para ejecutar sentencias sql Public Sub RUNsql(ByVal comando As String) cmd.CommandType = CommandType.Text cmd.CommandText = comando cmd.Connection = cn da.SelectCommand = cmd ds = New DataSet da.Fill(ds) End Sub 'procedimiento para ejecutar procedimientos almacenados Public Sub RUNStore(ByVal Nombre As String) cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = Nombre cmd.Connection = cn da.SelectCommand = cmd ds = New DataSet da.Fill(ds) End Sub

End Module

APP

CONFIG

<connectionStrings> <add name="cnx" connectionString="Server=ADMIN\SQLEXPRESS;Database=NegocioWeb;Integrated Security=true;"/> <add name="cnx1" connectionString="Server=ADMIN\SQLEXPRESS;Database=BdSistemas;Integrated Security=true;"/> </connectionStrings>

También podría gustarte