PVFC ASP.NET Web Forms - Layered Architecture (PVFC_DataLayer + PVFC_WebApp)
Back to DashboardUses CustomerRepository from PVFC_DataLayer to insert the customer.
Imports PVFC_DataLayer
Public Partial Class Registration
Inherits System.Web.UI.Page
Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsValid Then Return
Dim fullName As String = (txtFirstName.Text.Trim() & " " & txtLastName.Text.Trim()).Trim()
Dim customerId As Integer
If Not Integer.TryParse(txtCustomerId.Text.Trim(), customerId) Then
lblMessage.Text = "Error: Customer ID must be a number."
lblMessage.Visible = True
Return
End If
Try
' Delegates to PVFC_DataLayer.CustomerRepository
CustomerRepository.AddCustomer(customerId, fullName, txtAddress.Text.Trim(), txtCity.Text.Trim(), txtState.Text.Trim().ToUpper(), txtPostalCode.Text.Trim())
lblMessage.Text = "Success: Customer " & fullName & " registered! (ID: " & customerId & ")"
lblMessage.Visible = True
ClearForm()
Catch ex As SqlException
Dim msg As String = If(ex.Number = 2627, "A customer with ID " & customerId & " already exists.", ex.Message)
lblMessage.Text = "Error: " & msg
lblMessage.Visible = True
End Try
End Sub
Private Sub ClearForm()
txtCustomerId.Text = ""
txtFirstName.Text = ""
txtLastName.Text = ""
txtAddress.Text = ""
txtCity.Text = ""
txtState.Text = ""
txtPostalCode.Text = ""
End Sub
End Class
Uses OrderRepository and ProductRepository from PVFC_DataLayer. OrderLine class stored in ViewState.
Imports PVFC_DataLayer
Public Partial Class Order
Inherits System.Web.UI.Page
Private Const VS_LINES As String = "OrderLines"
Private ReadOnly Property Lines As List(Of OrderLine)
Get
If ViewState(VS_LINES) Is Nothing Then ViewState(VS_LINES) = New List(Of OrderLine)()
Return DirectCast(ViewState(VS_LINES), List(Of OrderLine))
End Get
End Property
Protected Sub btnAddToOrder_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim productId As Integer = Integer.Parse(ddlProducts.SelectedValue)
Dim qty As Integer = 1
Integer.TryParse(Request.Form("txtQtySelect"), qty)
' Calls PVFC_DataLayer.ProductRepository.GetProductById()
Dim row As DataRow = ProductRepository.GetProductById(productId)
If row Is Nothing Then Return
Dim existing As OrderLine = Lines.Find(Function(ln) ln.ProductId = productId)
If existing IsNot Nothing Then
existing.Qty += qty
Else
Lines.Add(New OrderLine With {.ProductId = productId, .ProductName = row("Product_Description").ToString(), .UnitPrice = Convert.ToDecimal(row("Standard_Price")), .Qty = qty})
End If
RefreshOrderLines()
End Sub
Protected Sub btnPlaceOrder_Click(ByVal sender As Object, ByVal e As EventArgs)
If Lines.Count = 0 Then Return
Dim custId As Integer = Integer.Parse(txtCustomerId.Text.Trim())
Dim orderId As Integer = OrderRepository.GetNextOrderId()
Dim productIds As Integer() = Lines.Select(Function(ln) ln.ProductId).ToArray()
Dim quantities As Integer() = Lines.Select(Function(ln) ln.Qty).ToArray()
Try
' PVFC_DataLayer.OrderRepository handles SQL Transaction internally
OrderRepository.PlaceOrder(orderId, custId, DateTime.Parse(txtOrderDate.Text), productIds, quantities)
lblMessage.Text = "Order #" & orderId & " placed successfully!"
lblMessage.Visible = True
ClearAll()
Catch ex As Exception
lblMessage.Text = "Error: " & ex.Message
lblMessage.Visible = True
End Try
End Sub
End Class
<Serializable>
Public Class OrderLine
Public Property ProductId As Integer
Public Property ProductName As String
Public Property UnitPrice As Decimal
Public Property Qty As Integer
End Class
Imports PVFC_DataLayer
Public Partial Class Search
Inherits System.Web.UI.Page
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim term As String = txtSearch.Text.Trim()
Dim lineId As String = ddlLineFilter.SelectedValue
' Delegates to PVFC_DataLayer.ProductRepository
Dim dt As DataTable = ProductRepository.SearchProducts(term, lineId)
gvResults.DataSource = dt
gvResults.DataBind()
lblCount.Text = "Found " & dt.Rows.Count & " item(s)."
End Sub
End Class
Imports PVFC_DataLayer
Public Partial Class Catalog
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then BindGrid()
End Sub
Private Sub BindGrid()
gvCatalog.DataSource = ProductRepository.GetAllProducts()
gvCatalog.DataBind()
End Sub
Protected Sub gvCatalog_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim productId As Integer = Convert.ToInt32(gvCatalog.DataKeys(e.RowIndex).Value)
Dim description As String = DirectCast(gvCatalog.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
Dim price As Decimal = Convert.ToDecimal(DirectCast(gvCatalog.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text)
ProductRepository.UpdateProduct(productId, description, price)
gvCatalog.EditIndex = -1
BindGrid()
End Sub
Protected Sub gvCatalog_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim productId As Integer = Convert.ToInt32(gvCatalog.DataKeys(e.RowIndex).Value)
ProductRepository.DeleteProduct(productId)
BindGrid()
End Sub
End Class
Imports PVFC_DataLayer
Public Partial Class Payment
Inherits System.Web.UI.Page
Protected Sub btnLookup_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim orderId As Integer = Integer.Parse(txtOrderId.Text.Trim())
Dim amount As Decimal = OrderRepository.GetOrderTotal(orderId)
txtTotalAmount.Text = amount.ToString("F2")
End Sub
Protected Sub btnPayment_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim orderId As Integer = Integer.Parse(txtOrderId.Text.Trim())
Dim amount As Decimal = Decimal.Parse(txtTotalAmount.Text)
OrderRepository.RecordPayment(orderId, amount)
lblMessage.Text = "Payment of $" & amount.ToString("N2") & " recorded for Order #" & orderId & "."
lblMessage.Visible = True
End Sub
End Class
Public Partial Class Login
Inherits System.Web.UI.Page
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs)
If txtUsername.Text = "Admin" AndAlso txtPassword.Text = "Admin123" Then
Session("Username") = txtUsername.Text
Session("IsAdmin") = True
Response.Redirect("Default.aspx")
Else
lblMessage.Text = "Invalid credentials."
lblMessage.Visible = True
End If
End Sub
End Class