Imports System
Imports System.Web.UI
Imports Northwind45.BusinessObject
Imports System.Web.Services
Imports System.Runtime.InteropServices
 
Namespace Northwind45
    Partial Public Class GridViewAddEdit_Products
        Inherits System.Web.UI.Page
 
 
        Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
            If Not IsPostBack Then
                DdlSupplierID.DataSource = Suppliers.SelectSuppliersDropDownListData()
                DdlSupplierID.DataBind()
 
                DdlCategoryID.DataSource = Categories.SelectCategoriesDropDownListData()
                DdlCategoryID.DataBind()
 
            End If
        End Sub
 
        Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
            Functions.GridViewRowDataBound(sender, e, 1)
        End Sub
 
        Protected Sub GridView1_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
            Functions.GridViewRowCreated(sender, e, 1)
        End Sub
 
        Public Function GetGridData(maximumRows As Integer, startRowIndex As Integer, <Out()> ByRef totalRowCount As Integer, sortByExpression As StringAs ProductsCollection
            Return Products.SelectSkipAndTake(maximumRows, startRowIndex, totalRowCount, sortByExpression)
        End Function
 
        Public Function GetSuppliersDropDownListData() As SuppliersCollection
            Return Suppliers.SelectSuppliersDropDownListData()
        End Function
 
        Public Function GetCategoriesDropDownListData() As CategoriesCollection
            Return Categories.SelectCategoriesDropDownListData()
        End Function
 
        Public Sub DeleteGridItem(ByVal productID As Integer)
            Try
                Products.Delete(productID)
            Catch ex As Exception
                Functions.ShowModalError(ex, Me)
            End Try
        End Sub
 
        Protected Sub BtnAddRecord_Click(sender As Object, e As EventArgsHandles BtnAddRecord.Click
            AddOrUpdateRecord("add")
        End Sub
 
        Protected Sub BtnUpdateRecord_Click(sender As Object, e As EventArgsHandles BtnUpdateRecord.Click
            AddOrUpdateRecord("update")
        End Sub
 
        Private Sub AddOrUpdateRecord(operation As String)
            If IsValid Then
                Dim objProducts As Products
 
                If operation = "update" Then
                    objProducts = Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(HfldProductID.Value))
                Else
                    objProducts = New Products()
                End If
 
                objProducts.ProductName = TxtProductName.Text
                objProducts.Discontinued = CbxDiscontinued.Checked
 
                If [String].IsNullOrEmpty(DdlSupplierID.SelectedValue) Then
                    objProducts.SupplierID = Nothing
                Else
                    objProducts.SupplierID = Convert.ToInt32(DdlSupplierID.SelectedValue)
                End If
 
                If [String].IsNullOrEmpty(DdlCategoryID.SelectedValue) Then
                    objProducts.CategoryID = Nothing
                Else
                    objProducts.CategoryID = Convert.ToInt32(DdlCategoryID.SelectedValue)
                End If
 
                If [String].IsNullOrEmpty(TxtQuantityPerUnit.Text) Then
                    objProducts.QuantityPerUnit = Nothing
                Else
                    objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text
                End If
 
                If [String].IsNullOrEmpty(TxtUnitPrice.Text) Then
                    objProducts.UnitPrice = Nothing
                Else
                    objProducts.UnitPrice = Convert.ToDecimal(TxtUnitPrice.Text)
                End If
 
                If [String].IsNullOrEmpty(TxtUnitsInStock.Text) Then
                    objProducts.UnitsInStock = Nothing
                Else
                    objProducts.UnitsInStock = Convert.ToInt16(TxtUnitsInStock.Text)
                End If
 
                If [String].IsNullOrEmpty(TxtUnitsOnOrder.Text) Then
                    objProducts.UnitsOnOrder = Nothing
                Else
                    objProducts.UnitsOnOrder = Convert.ToInt16(TxtUnitsOnOrder.Text)
                End If
 
                If [String].IsNullOrEmpty(TxtReorderLevel.Text) Then
                    objProducts.ReorderLevel = Nothing
                Else
                    objProducts.ReorderLevel = Convert.ToInt16(TxtReorderLevel.Text)
                End If
 
                ' the insert method returns the newly created primary key
                Dim newlyCreatedPrimaryKey As Integer
 
                Try
                    If operation = "update" Then
                        objProducts.Update()
                    Else
                        newlyCreatedPrimaryKey = objProducts.Insert()
                    End If
                Catch ex As Exception
                    If operation = "update" Then
                        Functions.ShowModalError(ex, Me"An error occured during item update!")
                    Else
                        Functions.ShowModalError(ex, Me"An error occured during item addition!")
                    End If
                End Try
 
                GridView1.DataBind()
            End If
        End Sub
 
        <WebMethod()> _
        Public Shared Function GetProducts(productID As StringAs Products
            Return Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(productID))
        End Function
    End Class
End Namespace