Está en la página 1de 6

DEMO 12

1. PRODUCTOCATALOGO
2. ABRE PRODUCTO CARRITO
3. ABRE COMPRA.ASPX
WEB CONFIG
<configuration>
<connectionStrings>
<appSettings>
<add key="Con"
value="server=.\SQLEXPRESS;database=Northwind;integrated
security=true"/>
</appSettings>
<connectionStrings/>

PRODUCTO CATALOGO

Partial Class ProductoCatalogo


Inherits System.Web.UI.Page
#Region "Procedimientos"
Private Sub EnlazarDatos()
Dim SqlConn As New
SqlConnection(ConfigurationManager.AppSettings.Get("Con"))
Dim strSQL As String = "SELECT ProductID,
ProductName,UnitPrice,UnitsInStock FROM Products ORDER BY
ProductName"
Dim dap As New SqlDataAdapter(strSQL, SqlConn)
Dim dst As New DataSet
dap.Fill(dst, "Productos")
grdProducto.DataSource = dst.Tables("Productos")
grdProducto.DataBind()

End Sub
Private Sub CrearCarrito()
'Declara la tabla
Dim objDT As New DataTable("Carrito")
'Crea las columnas
With objDT.Columns
.Add("ProductID", GetType(Integer))
.Add("ProductName", GetType(String))
.Add("UnitPrice", GetType(Decimal))
.Add("Quantity", GetType(Integer))
.Add("Subtotal", GetType(Decimal))
End With
'Guardo la tabla en una variable de Session
Session("Carrito") = objDT
End Sub
Private
Dim
Dim
Dim
Dim
Dim

Sub AgregarItemAlCarrito()
objDT As DataTable
objDR As DataRow
Quantity As Integer
UnitPrice, Subtotal As Decimal
ExisteItem As Boolean = False

'Realizando calculos
UnitPrice = Decimal.Parse(txtUnitPrice.Text)
Quantity = Int32.Parse(txtQuantity.Text)
Subtotal = UnitPrice * Quantity
'Obtener el carrito existente
objDT = Session("Carrito")
'Comprobar si el Producto ya existe en el carrito.
'Si ya existe, slo se incrementa la cantidad.
For Each objDR In objDT.Rows
If objDR.Item("ProductID") = txtProductId.Text Then
ExisteItem = True
objDR.Item("Quantity") += Quantity
objDR.Item("Subtotal") = objDR.Item("UnitPrice") *
objDR.Item("Quantity")
Exit For
End If
Next
'Crear nueva fila.
If Not ExisteItem Then
'Creando la fila nueva
objDR = objDT.NewRow()
'Carga los campos de la fila
With objDR
.Item("ProductID") = txtProductId.Text
.Item("ProductName") = txtProductName.Text
.Item("UnitPrice") = UnitPrice 'Variable local
.Item("Quantity") = Quantity 'Variable local
.Item("Subtotal") = Subtotal 'Variable local
End With
'Agrega la fila a la tabla
objDT.Rows.Add(objDR)

End If
'Actualiza la variable de Session del Carrito
Session("Carrito") = objDT
End Sub
#End Region
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
If Not Page.IsPostBack Then
'LLAMA AL METODO
CrearCarrito() 'esta en region
EnlazarDatos()
Else
EnlazarDatos()
End If
End Sub
Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As
AgregarItemAlCarrito()
Response.Redirect("ProductoCarrito.aspx")
End Sub
Protected Sub grdProducto_PageIndexChanging(ByVal sender As
Handles grdProducto.PageIndexChanging
grdProducto.PageIndex = e.NewPageIndex
EnlazarDatos()
End Sub
Protected Sub grdProducto_SelectedIndexChanged(ByVal sender As
grdProducto.SelectedIndexChanged
txtProductId.Text =
grdProducto.Rows.Item(grdProducto.SelectedIndex).Cells(1).Text
txtProductName.Text =
grdProducto.Rows.Item(grdProducto.SelectedIndex).Cells(2).Text
txtUnitPrice.Text =
grdProducto.Rows.Item(grdProducto.SelectedIndex).Cells(3).Text
txtQuantity.Text = "1"
End Sub
End Class
PRODUCTO CARRITO

Protected Sub Page_Load(ByVal sender As Object, ByVal e As


If Not Page.IsPostBack Then
MostrarCarrito()
End If
End Sub
Protected Sub btnComprar_Click(ByVal sender As Object, ByVal e As
Response.Redirect("Comprar.aspx")
End Sub
Protected Sub grdDetalle_RowDeleting(ByVal sender As Object, ByVal
'cargamos el objeto datatable declarada a nivel de modulo si
no existiera se debe declarar
objDT = Session("Carrito")
'elimina una fila
objDT.Rows.Item(e.RowIndex).Delete()
'una ves borrado actualizo el carrito

Session("Carrito") = objDT
'actualiza la grilla
grdDetalle.DataSource = objDT
grdDetalle.DataBind()
'cada vez que quito un item debo actualizar el total
lblTotal.Text = HallarTotalCarrito().ToString("N2")
'refresco
grdDetalle.DataBind()
End Sub
COMPRA
Partial Class Compra
Inherits System.Web.UI.Page
'creamos un procedimiento
Private Sub ClienteTemporal()
txtCustomerID.Text = "SV009"
txtCustomerName.Text = "Super
Veliz"
txtOrderDate.text =
Today.ToString()
txtAdress.Text = "Av. Peru 1840"
txtEmail.Text =
"Fveliz@uni.edu.pe"
txtCCExpired.text = Today.AddYears(2).ToString
txtCCType.Text = "Visa"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ClienteTemporal()
End If
End Sub
DEMO 14

CODIGO DE GLOBAL.ASAX
<%@ Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As
EventArgs)
' Cdigo que se ejecuta al iniciarse la aplicacin
'creo y le asigno un valor a la variable application
Application("contador") = 0
'agrego la cadena de conexion para que sea compartido por
todos algo como publico
Application("cno") =
"server=.\SQLEXPRESS;database=northwind;integrated security=true"
'puedo crear proced.publicos
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As

' Cdigo que se ejecuta durante el cierre de aplicaciones


End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As
' Cdigo que se ejecuta al producirse un error no controlado
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Cdigo que se ejecuta cuando se inicia una nueva sesin
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Cdigo que se ejecuta cuando finaliza una sesin.
' Nota: El evento Session_End se desencadena slo con el modo
sessionstate
' se establece como InProc en el archivo Web.config. Si el
modo de sesin se establece como StateServer
' o SQLServer, el evento no se genera.
End Sub
</script>
Partial Class Incio
Inherits System.Web.UI.Page
Protected Sub btnAceptar_Click(ByVal sender As Object, ByVal e As
'uso una session que guarda el nombre del usuario en esta pagina
'este valor lo va a recuperar en otra pagina consulta
Session("usuario") = txtUsuario.Text
Server.Transfer("Consulta.aspx")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
'LA VARIABLE de estado de application se llama contador
'esta variable se inicializa en global.asax
'cuando viene un usuario se va acumulando porque el dato esta
guardado en la memoria cache del servidor web
'que funciona las 24 horas 365 dias
'los datos aqui estan en el IIS Servidor Web
If Not Page.IsPostBack Then
Application("contador") += 1
End If
lblVisita.Text = "Visitante Nro. : " & Application("contador")
End Sub
End Class

Private dvw As DataView


Sub Enlazar_Datagrid()
dgdEmpleado.DataSource = dvw
dgdEmpleado.DataBind()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As

If Not Page.IsPostBack Then


txtUsuario.Text = Session("usuario")
Dim con As New SqlConnection(Application("Cno"))
Dim dap As New SqlDataAdapter("Select
EmployeeID,LastName,FirstName From Employees", con)
Dim dst As New DataSet()
dap.Fill(dst, "Empleados")
dvw = dst.Tables("Empleados").DefaultView
Enlazar_Datagrid()
Session("vista") = dvw
End If
End Sub
Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal
'primero recupero la vista luego el filtro
dvw = Session("vista")
dvw.RowFilter = "LastName Like '" & txtApellido.Text & "%'"
Enlazar_Datagrid()
End Sub
Private Sub btnDetalle_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnDetalle.Click
dvw = Session("vista")
Session("fila") =
dvw.Item(dgdEmpleado.SelectedIndex)
Server.Transfer("Detalle.aspx")
End Sub
End Class

Private Sub Page_Load(ByVal


sender As System.Object, ByVal e As
If Not Page.IsPostBack Then
'recupero la session usuario y fila
txtUsuario.Text = Session("usuario")
Dim fila As DataRowView = Session("fila")
With fila
txtCodigo.Text = .Item(0)
txtApellido.Text = .Item(1)
txtNombre.Text = .Item(2)
End With
End If
End Sub
Private Sub btnRegresar_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnRegresar.Click
Server.Transfer("Consulta.aspx")
End Sub
End Class

También podría gustarte