asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET入门教程 13.4.5 计划结账[3]


发布日期:2020年07月18日
 
ASP.NET入门教程 13.4.5 计划结账[3]

)在这个代码上添加如下代码

<%@ Import Namespace=SystemDataSqlClient %>

<%@ Import Namespace=WroxCommerce %>

)保存该设计

)转到Solution Explorer选择checkoutaspxvb

)添加如下后台代码取代已经位于其中的代码

Imports System

Imports SystemDataSqlClient

Imports WroxCommerce

Partial Class Checkout

Inherits SystemWebUIPage

Sub Page_Load(ByVal sender As Object ByVal e As SystemEventArgs) Handles MeLoad

TraceWrite(In Page_Load)

If Not PageIsPostBack Then

If ProfileCart Is Nothing Then

NoCartlabelVisible = True

WizardVisible = False

End If

TraceWrite(In Page_Load UserIdentityIsAuthenticatedToString())

If UserIdentityIsAuthenticated Then

WizardActiveStepIndex =

Else

WizardActiveStepIndex =

End If

End If

End Sub

Sub chkUseProfileAddress_CheckedChanged(ByVal sender As Object ByVal e As SystemEventArgs)

fill the delivery address from the profile but only if its empty

we dont want to overwrite the values

If chkUseProfileAddressChecked AndAlso txtNameTextTrim() = Then

txtNameText = ProfileName

txtAddressText = ProfileAddress

txtCityText = ProfileCity

txtCountyText = ProfileCounty

txtPostCodeText = ProfilePostCode

txtCountryText = ProfileCountry

End If

End Sub

Sub Wizard_FinishButtonClick(ByVal sender As Object ByVal e As SystemWebUIWebControlsWizardNavigationEventArgs)

Insert the order and order lines into the database

Dim conn As SqlConnection = Nothing

Dim trans As SqlTransaction = Nothing

Dim cmd As SqlCommand

Try

conn = New SqlConnection(ConfigurationManagerConnectionStrings(WroxUnited)ConnectionString)

connOpen()

trans = connBeginTransaction

cmd = New SqlCommand()

cmdConnection = conn

cmdTransaction = trans

set the order details

cmdCommandText = INSERT INTO Orders(MemberName OrderDate Name Address County PostCode Country SubTotal Discount Total) & _

VALUES (@MemberName @OrderDate @Name @Address @County @PostCode @Country @SubTotal @Discount @Total)

cmdParametersAdd(@MemberName DataSqlDbTypeVarChar )

cmdParametersAdd(@OrderDate DataSqlDbTypeDateTime)

cmdParametersAdd(@Name DataSqlDbTypeVarChar )

cmdParametersAdd(@Address DataSqlDbTypeVarChar )

cmdParametersAdd(@County DataSqlDbTypeVarChar )

cmdParametersAdd(@PostCode DataSqlDbTypeVarChar )

cmdParametersAdd(@Country DataSqlDbTypeVarChar )

cmdParametersAdd(@SubTotal DataSqlDbTypeMoney)

cmdParametersAdd(@Discount DataSqlDbTypeMoney)

cmdParametersAdd(@Total DataSqlDbTypeMoney)

cmdParameters(@MemberName)Value = UserIdentityName

cmdParameters(@OrderDate)Value = DateTimeNow()

cmdParameters(@Name)Value = CType(WizardFindControl(txtName) TextBox)Text

cmdParameters(@Address)Value = CType(WizardFindControl(txtAddress) TextBox)Text

cmdParameters(@County)Value = CType(WizardFindControl(txtCounty) TextBox)Text

cmdParameters(@PostCode)Value = CType(WizardFindControl(txtPostCode) TextBox)Text

cmdParameters(@Country)Value = CType(WizardFindControl(txtCountry) TextBox)Text

cmdParameters(@SubTotal)Value = ProfileCartSubTotal

cmdParameters(@Discount)Value = ProfileCartMemberDiscount

cmdParameters(@Total)Value = ProfileCartTotal

Dim OrderID As Integer

OrderID = ConvertToInt(cmdExecuteScalar())

change the query and parameters for the order lines

cmdCommandText = INSERT INTO OrderLines(OrderID ProductID Quantity Price) & _

VALUES (@OrderID @ProductID @Quantity @Price)

cmdParametersClear()

cmdParametersAdd(@OrderID DataSqlDbTypeInt)

cmdParametersAdd(@ProductID DataSqlDbTypeInt)

cmdParametersAdd(@Quantity DataSqlDbTypeInt)

cmdParametersAdd(@Price DataSqlDbTypeMoney)

cmdParameters(@OrderID)Value = OrderID

For Each item As CartItem In ProfileCartItems

cmdParameters(@ProductID)Value = itemProductID

cmdParameters(@Quantity)Value = itemQuantity

cmdParameters(@Price)Value = itemPrice

cmdExecuteNonQuery()

Next

commit the transaction

transCommit()

Catch SqlEx As SqlException

some form of error rollback the transaction

and rethrow the exception

If trans IsNot Nothing Then

transRollback()

End If

Log the exception

Toolslog(An error occurred while creating the order SqlEx)

Throw New Exception(An error occurred while creating the order SqlEx)

CreateOrderErrorLabelVisible = True

Return

Finally

If conn IsNot Nothing Then

connClose()

End If

End Try

we will only reach here if the order has been created sucessfully

so clear the cart

ProfileCartItemsClear()

End Sub

Sub Wizard_NextButtonClick(ByVal sender As Object ByVal e As SystemWebUIWebControlsWizardNavigationEventArgs)

TraceWrite(next: CurrentStep eCurrentStepIndexToString())

If eCurrentStepIndex = Then

Dim l As SystemWebUIWebControlsLogin = CType(WizardFindControl(Login) Login)

If MembershipValidateUser(lUserName lPassword) Then

FormsAuthenticationSetAuthCookie(lUserName lRememberMeSet)

eCancel = False

Else

TraceWrite(lUserName lPassword)

lInstructionText = Your login attempt was not successful Please try again

lInstructionTextStyleForeColor = SystemDrawingColorRed

eCancel = True

End If

Else

If Not UserIdentityIsAuthenticated Then

eCancel = True

WizardActiveStepIndex =

End If

End If

End Sub

Protected Sub Wizard_ActiveStepChanged(ByVal sender As Object ByVal e As SystemEventArgs) Handles WizardActiveStepChanged

If Not UserIdentityIsAuthenticated Then

TraceWrite(step changed not authenticated returning to step )

WizardActiveStepIndex =

End If

End Sub

End Class

[] [] [] [] [] [] [] []

               

上一篇:ASP.NET入门教程 13.4.5 计划结账[1]

下一篇:ASP.NET入门教程 13.4.5 计划结账[8]