2011-04-11 99 views
0
'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)。請幫助...循環不增加ASP.NET VB

回答

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 
+0

仍然沒有循環... – Farax 2011-04-11 03:54:44

+0

你確定你有多個記錄嗎?你確定沒有錯誤被吞噬嗎?我注意到你的try/catch塊只是將任何異常拋出。也許你可以在這個異常處理程序上進行擴展,看看結果如何? – 2011-04-11 04:09:15

+0

這是拋出的異常:System.NullReferenceException:未將對象引用設置爲對象的實例。 – Farax 2011-04-11 04:13:53

0

「CUSTID」不被任何定義..

反正你要插入一個破發點,並與調試器介入。然後觀察數據以捕捉哪個語句導致異常。然後相應地修改代碼來處理它。

0

如果您有:

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

替換爲:

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

將由1每次循環遞增intCounter。