'Add items to db'
Function recordOrder()
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
Dim sql As String
myCommand2.Connection = con2
con2.Open()
'variables'
Dim order_date As String
Dim coupon_ID As String
Dim customer_id As String
Dim quantity As String
'variables'
Try
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.Parameters.Add("@order_date", SqlDbType.VarChar).Value = order_date
myCommand2.Parameters.Add("@coupon_ID", SqlDbType.VarChar).Value = coupon_ID
myCommand2.Parameters.Add("@customer_id", SqlDbType.VarChar).Value = custID
myCommand2.Parameters.Add("@quantity", SqlDbType.VarChar).Value = quantity
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( @order_date ,@coupon_ID,@customer_id,@quantity)"
myComm开发者_如何学Pythonand2.ExecuteNonQuery()
Next
Catch ex As Exception
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
The loop is not incrementing (intCounter). Please HELP...
Would you not be better to use:
For Each objDR In objDT.Rows
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.Parameters.Add("@order_date", SqlDbType.VarChar).Value = order_date
myCommand2.Parameters.Add("@coupon_ID", SqlDbType.VarChar).Value = coupon_ID
myCommand2.Parameters.Add("@customer_id", SqlDbType.VarChar).Value = custID
myCommand2.Parameters.Add("@quantity", SqlDbType.VarChar).Value = quantity
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( @order_date ,@coupon_ID,@customer_id,@quantity)"
myCommand2.ExecuteNonQuery()
Next
'custID' is not defined anywhere ..
anyway you should insert a break point and step in with the debugger. Then watch the data to catch which statement is causing the exception. Then modify the code accordingly to handle it.
Where you have:
For intCounter = 0 To objDT.Rows.Count - 1
Replace with:
For intCounter = 0 To objDT.Rows.Count - 1 Step 1
that will increment intCounter by 1 each loop.
精彩评论