循环不递增ASP.NET VB

时间:2011-04-11 03:38:58

标签: asp.net

'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)"



            myCommand2.ExecuteNonQuery()


        Next
    Catch ex As Exception

    Finally
        If con2.State = ConnectionState.Open Then
            con2.Close()
        End If
    End Try
End Function

循环没有递增(intCounter)。请帮助...

3 个答案:

答案 0 :(得分:0)

你最好不要使用:

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

答案 1 :(得分:0)

'custID'未在任何地方定义..

无论如何,你应该插入一个断点并使用调试器进入。然后观察数据以捕获导致异常的语句。然后相应地修改代码以处理它。

答案 2 :(得分:0)

你在哪里:

For intCounter = 0 To objDT.Rows.Count - 1

替换为:

For intCounter = 0 To objDT.Rows.Count - 1 Step 1

将每个循环增加1个intCounter。