Ado.net

Solo disponible en BuenasTareas
  • Páginas : 9 (2176 palabras )
  • Descarga(s) : 0
  • Publicado : 9 de agosto de 2010
Leer documento completo
Vista previa del texto
PRACTICA DE MANTENIMIENTO ADO.net

Imports System.Data
Imports System.Data.SqlClient

Public Class Categoria
Public cnx2 As New SqlConnection("user id= sa; server= . ;database= miniedu")
Public dap2 As New SqlDataAdapter("select * from categoria", cnx2)
Public dst2 As New DataSet
Public cmd2 As New SqlCommand

Public cont

Private Sub Categoria_Load(ByValsender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dap2.Fill(dst2, "cat")
cont = 0
If dst2.Tables("cat").Rows.Count 0 Then
Call mostrar_categoria(cont)
End If
End Sub

Private Sub mostrar_categoria(ByVal a As Integer)
txtCodCat.Text = dst2.Tables("cat").Rows(a).Item(0)
txtNomCat.Text =dst2.Tables("cat").Rows(a).Item(1)
End Sub

Private Sub hab_Categoria(ByVal a As Boolean, ByVal b As Boolean, ByVal c As Boolean)
btnNuevoCat.Enabled = a
btnModificarCat.Enabled = b
btnEliminarCat.Enabled = c
End Sub

Private Sub btnNuevoCat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevoCat.Click
If btnNuevoCat.Text= "Nuevo" Then
txtCodCat.Text = "" : txtNomCat.Text = ""
Call hab_Categoria(True, False, False)
btnNuevoCat.Text = "Grabar"
txtNomCat.Focus()
ElseIf btnNuevoCat.Text = "Grabar" Then
Dim cod_c$, tmpc$, iuec%
If dst2.Tables("cat").Rows.Count = 0 Then
cod_c = "000"
Elseiuec = dst2.Tables("cat").Rows.Count - 1
cod_c = dst2.Tables("cat").Rows(iuec).Item(0)
End If
tmpc = Str(Val(Mid(cod_c, 2)) + 1)
cod_c = Mid(cod_c, 1, 3 - Len(Trim(tmpc))) & Trim(tmpc)
txtCodCat.Text = cod_c

cnx2.Open()
cmd2 = New SqlCommand("insert into categoria values('" & txtCodCat.Text & "','"& txtNomCat.Text & "')", cnx2)
cmd2.ExecuteScalar()
cmd2.Dispose()
cnx2.Close()

MsgBox("Datos almacenados correctamente", , "Atencion")
dst2.Tables("cat").Clear()
dap2.SelectCommand.CommandText = "select * from categoria"
dap2.Fill(dst2, "cat")
cont = dst2.Tables("cat").Rows.Count - 1Call hab_Categoria(True, True, True)
btnNuevoCat.Text = "Nuevo"
End If
End Sub

Private Sub btnModificarCat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarCat.Click
If btnModificarCat.Text = "Modificar" Then
Call hab_Categoria(False, True, False)
btnModificarCat.Text = "Confirmar"txtNomCat.Focus()
ElseIf btnModificarCat.Text = "Confirmar" Then
cnx2.Open()
cmd2 = New SqlCommand("update categoria set nomCat='" & txtNomCat.Text & "'where idCat='" & txtCodCat.Text & "'", cnx2)
cmd2.ExecuteScalar()
cmd2.Dispose()
cnx2.Close()
MsgBox("Se actualizaron los datos correctamente", ,"Atencion")
dst2.Tables("cat").Clear()
dap2.SelectCommand.CommandText = "select * from categoria"
dap2.Fill(dst2, "cat")
Call hab_Categoria(True, True, True)
btnModificarCat.Text = "Modificar"
End If
End Sub

Private Sub btnEliminarCat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesbtnEliminarCat.Click
Dim M As Object = MsgBox("¿Usted va ha eliminar un registro?", MsgBoxStyle.YesNo, "Cuidado!")
If M = 6 Then
cnx2.Open()
cmd2 = New SqlCommand("delete categoria where idCat='" & txtCodCat.Text & "'", cnx2)
cmd2.ExecuteScalar()
cmd2.Dispose()
cnx2.Close()
MsgBox("Los datos se eliminaron...
tracking img