I've been working on an e-commerce web site written in asp.net 1.1 and
vb.net. Within the shopping cart I have created a sub procedure that calculates the total of the order , which is called "Sub ComputeSum".
Additionally, within this sub procedure there is some code that allows this total value to be updated if a customer continues shopping and adds more items to their cart and therefore increases the total value. This total value is then updated within a table in an access database.
This code works by checking to see if there are any rows in the table corresponding to that cartID. If not, the total value is inserted into the database. If the cartID already exists, i.e. the customer has updated their existing order, the total value is updated in the database. However this update does not seem to work - any ideas where this may be failing ? Here's the main code from the "Sub ComputeSum" sub procedure - thanks:
Code:
Sub ComputeSum(sender As Object, e As DataGridItemEventArgs)
'Ensure dealing with an Item or AlternatingItem
If e.Item.ItemType = ListItemType.Item OR _
e.Item.ItemType = ListItemType.AlternatingItem then
'Define the ViewCount
Dim viewCount as Integer = Amount
viewCountSum += viewCount
'Otherwise display total info in footer
ElseIf e.Item.ItemType = ListItemType.Footer then
e.Item.Cells(7).Text = "Total: £" & String.Format("{0:F2}", viewCountSum)
'If no CartID in table = insert data
'Define required variables
Dim intCartID As Double
intCartID = Session("CartID")
Dim curGrandTotal As Decimal
curGrandTotal = viewCountSum
Dim DataSet As DataSet
Dim DTable As DataTable
Dim loop1, numrows As Integer
Dim SessionID = Session("CartID")
'SQL statement to look up required data
Dim strSQL As String = "SELECT [tblTotal].[intCartID], [tblTotal].[curGrandTotal] FROM [tblTotal] WHERE ([tblTotal].[intCartID]) = " & Session("CartID") & " ; "
'Create a connection to data source
Dim strConnString As String = ConfigurationSettings.AppSettings.Get("ConnectionString")
strConnString = String.Format(strConnString, Server.MapPath("\db\nwguitars.mdb"))
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(strConnString)
'Create a command object with SQL statement
Dim DAdapter As OleDbDataAdapter
DAdapter = New OleDbDataAdapter(strSQL,dbConnection)
'Fill dataset with data from database
DataSet = New System.Data.DataSet
DAdapter.Fill(Dataset)
'Create a new DataTable object and assign to it
'the new table in the tables collection
DTable = New DataTable
DTable = DataSet.Tables(0)
'Find out how many rows of new DataTable object
numrows = DTable.Rows.Count
If numrows = 0
'If number of rows found = zero then insert total and cartID into table in db
Dim queryString As String = "INSERT INTO [tblTotal] ([intCartID] , [curGrandTotal]) " & _
"VALUES (@intCartID, @curGrandTotal)"
'Define command objects
Dim dbComm As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbComm.CommandText = queryString
dbComm.Connection = dbConnection
'Define CartID and Total parameters
Dim dbParam_intCartID As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_intCartID.ParameterName = "@intCartID"
dbParam_intCartID.Value = intCartID
dbParam_intCartID.DbType = System.Data.DbType.Double
dbComm.Parameters.Add(dbParam_intCartID)
Dim dbParam_curGrandTotal As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_curGrandTotal.ParameterName = "@curGrandTotal"
dbParam_curGrandTotal.Value = curGrandTotal
dbParam_curGrandTotal.DbType = System.Data.DbType.Decimal
dbComm.Parameters.Add(dbParam_curGrandTotal)
'Connect to db and perform query
dbConnection.Open()
dbComm.ExecuteNonQuery()
dbConnection.Close()
'If CartID already exists = update data
Else