Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

IDAT CHICLAYO ASP .

NET CARRITO DE COMPRAS

Cdigo Fuente:
Imports System.Data Imports System.Data.SqlClient Partial Class Default10 Inherits System.Web.UI.Page Dim con As New SqlConnection("user id=sa;data source=(local);Initial catalog=ventas") Dim cmd As SqlCommand Dim dtb As New DataTable("Carrito") Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand Dim f As GridViewRow = Me.GridView1.Rows(e.CommandArgument) 'Response.Write(f.Cells(0).Text) 'Obtiene el valor del campo clave Dim cod As Integer = Me.GridView1.DataKeys(e.CommandArgument).Value 'Obtiene el valor del cuadro de texto TextBox1 Dim t As TextBox = f.Cells(4).FindControl("textBox1") If con.State = ConnectionState.Closed Then con.Open()

Ing. Juan Zevallos V.

IDAT CHICLAYO
cmd = New SqlCommand("select stock from productos where codpro=" & cod & "", con) Dim s As Integer = cmd.ExecuteScalar If CInt(t.Text) > s Then Me.Label2.Text = "<b>Stock insuficiente</b>" Else Dim pro As String = f.Cells(1).Text Dim pre As String = CDbl(f.Cells(2).Text) Dim tot As Double = pre * CDbl(t.Text) Dim car As New DataTable car = Session("aux") Dim fi As DataRow = car.NewRow fi(0) = cod fi(1) = pro fi(2) = pre fi(3) = CInt(t.Text) fi(4) = tot car.Rows.Add(fi) 'Response.Write(car.Rows.Count) Session("aux") = car End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim col1 As New DataColumn("Codigo", GetType(Integer)) Dim col2 As New DataColumn("Producto", GetType(String)) Dim col3 As New DataColumn("Precio", GetType(Double)) Dim col4 As New DataColumn("Cantidad", GetType(Integer)) Dim col5 As New DataColumn("Total", GetType(Double)) dtb.Columns.Add(col1) dtb.Columns.Add(col2) dtb.Columns.Add(col3) dtb.Columns.Add(col4) dtb.Columns.Add(col5) Session("aux") = dtb End If End Sub Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click Me.GridView2.DataSource = Session("aux") Me.GridView2.DataBind() Total(Session("aux")) End Sub Protected Sub GridView2_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView2.RowDeleting Dim n As Integer = e.RowIndex Dim ocar As New DataTable ocar = Session("aux") ocar.Rows(n).Delete() Total(ocar) GridView2.DataSource = ocar GridView2.DataBind() End Sub

Ing. Juan Zevallos V.

IDAT CHICLAYO
Sub Total(ByVal dt As DataTable) Dim t As Double For Each f As DataRow In dt.Rows t += Double.Parse(f(4)) Next Me.Label3.Text = "Total : " & t End Sub End Class

Ing. Juan Zevallos V.

You might also like