Vous êtes sur la page 1sur 2

CODIGO ASP.

NET APLICACIN ASIGNATURA


Public Class asignaturas
Inherits System.Web.UI.Page
Dim encontro As Boolean
Protected Sub BtnIngresar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles BtnIngresar.Click
verificar()
Try
If encontro = True Then
Lblmsg.Text = "La materia ya se encuentra Ingresado"
TxtMateria.ReadOnly = True
txtdescripcion.Focus()
BtnIngresar.Visible = False
BtnActualizar.Visible = True
BtnCancelar.Visible = True
Else
Dim Condata As String = "Data Source= CASTILLO ; Initial
Catalog=Data;Integrated Security=True"
Dim Cmd As String = "INSERT INTO materias (idmateria,descripcion) VALUES (" &
_
"'" & TxtMateria.Text & "','" & Txtdescripcion.Text & "')"
Dim myCon As New SqlConnection(Condata)
Dim myCom As New SqlCommand(Cmd, myCon)
myCon.Open()
myCom.ExecuteNonQuery()
myCon.Close()
Lblmsg.Text = ""
limpiar()
End If
Catch ex As SqlException
Lblmsg.Text = ex.Message
End Try
GrdData.DataBind()
End Sub
Protected Sub BtnActualizar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles BtnActualizar.Click
actualizar()
BtnActualizar.Visible = False
BtnIngresar.Visible = True
BtnCancelar.Visible = False
TxtMateria.ReadOnly = False
limpiar()
Lblmsg.Text = ""
End Sub
Protected Sub BtnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles BtnCancelar.Click
TxtMateria.ReadOnly = False
Lblmsg.Text = ""
limpiar()
BtnIngresar.Visible = True
BtnActualizar.Visible = False
BtnCancelar.Visible = False
End Sub
Protected Sub limpiar()
TxtMateria.Text = ""

txtdescripcion.Text = ""
TxtMateria.Focus()
End Sub
Protected Sub verificar()
encontro = False
Dim con As String = "Data Source= CASTILLO ; Initial Catalog=Data;Integrated
Security=True"
Dim strSQL As String = "Select idmateria,descripcion from materias where
idmateria='" & TxtMateria.Text & "'"
Dim mycon As New SqlConnection(con)
Dim mycomand As New SqlCommand(strSQL, mycon)
Dim myreader As SqlDataReader
Try
'abrimos la conexion
mycon.Open()
'almacenamos la data en lectura
myreader = mycomand.ExecuteReader()
'realiza un vaciado por medio de un ciclo para determinar su cointenido en la base
de datos
While myreader.Read
'en la variable nombre le asignamos el nombre que viene de la tabla, la columna
2 nos indica que es el nombre
'si se fijan en la instruccion SQL 0 es la columna del iduser, 1 es de la columna
clave y 2 que es de la columna nombre
TxtMateria.Text = myreader.Item(0).ToString
txtdescripcion.Text = myreader.Item(1).ToString
encontro = True
End While
'si no tiene informacion al respecto del datareader entonces nos desplegara un msg
de error
myreader.Close()
Catch ex As Exception
Lblmsg.Text = ex.Message
Finally
mycon.Close()
End Try
End Sub
Protected Sub actualizar()
Dim Condata As String = "Data Source= CASTILLO ; Initial Catalog=Data;Integrated
Security=True"
Dim Cmd As String = "UPDATE materias set descripcion='" & txtdescripcion.Text & "'
where idmateria='" & TxtMateria.Text & "'"
Dim myCon As New SqlConnection(Condata)
Dim myCom As New SqlCommand(Cmd, myCon)
myCon.Open()
myCom.ExecuteNonQuery()
myCon.Close()
GrdData.DataBind()
End Sub
Protected Sub BtnSalir_Click(sender As Object, e As EventArgs) Handles BtnSalir.Click
End Sub
End Class

Vous aimerez peut-être aussi