Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Demo 12: 1. Productocatalogo 2. Abre Producto Carrito 3. Abre Compra - Aspx Web Config
Demo 12: 1. Productocatalogo 2. Abre Producto Carrito 3. Abre Compra - Aspx Web Config
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
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
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