Question

When ever i insert an order with more than one items in the order details, only the last oder details item get inserted.

Am inserting into a database using the following code

Try
    order_id = value.ID
    If order_id = 0 Then
        cmd = New SQLiteCommand(insert_order, conn)
        cmd.Parameters.AddWithValue("@customer_id", value.CustomerID)
        cmd.Parameters.AddWithValue("@status", value.Status)
        cmd.Parameters.AddWithValue("@deliver", value.Deliver)
        cmd.Parameters.AddWithValue("@created", Now.ToString("yyyy-MM-dd"))
        cmd.Parameters.AddWithValue("@deleted", "0")
        order_id = Convert.ToInt32(cmd.ExecuteScalar())
    Else
        cmd = New SQLiteCommand(update_order, conn)
        cmd.Parameters.AddWithValue("@customer_id", value.CustomerID)
        cmd.Parameters.AddWithValue("@status", value.Status)
        cmd.Parameters.AddWithValue("@deliver", value.Deliver)
        cmd.Parameters.AddWithValue("@created", Now.ToString("yyyy-MM-dd"))
        cmd.Parameters.AddWithValue("@deleted", "0")
        cmd.Parameters.AddWithValue("@id", order_id)
        cmd.ExecuteNonQuery()
        '// delete previous order details
        cmd = New SQLiteCommand(delete_order_details, conn)
        cmd.Parameters.AddWithValue("@id", order_id)
        cmd.ExecuteNonQuery()
    End If

    '// Add order details
    For Each itm As typOrderDetail In value.orderDetails
        cmd = New SQLiteCommand(insert_order_details, conn)
        cmd.Parameters.AddWithValue("@cloth_id", itm.ClothID)
        cmd.Parameters.AddWithValue("@order_id", order_id)
        cmd.Parameters.AddWithValue("@color", itm.Color)
        cmd.Parameters.AddWithValue("@desc", itm.Desc)
        cmd.Parameters.AddWithValue("@qty", itm.Qty)
        cmd.Parameters.AddWithValue("@cost", itm.Cost)
        cmd.Parameters.AddWithValue("@service_id", itm.ServiceID)
        cmd.Parameters.AddWithValue("@deleted", "0")
        cmd.ExecuteNonQuery()
    Next
    sqlTrans.Commit()
    'If bSave Then sqlTrans.Commit() Else sqlTrans.Rollback()
Catch ex As Exception
    'sqlTrans.Rollback()
    Throw
Finally

    cmd.Dispose()
    conn.Dispose()
End Try

and here are the sql string

INSERT INTO orders ([customer_id],[status],[deliver],[created],[deleted]) VALUES (@customer_id,@status,@deliver,@created,@deleted);select last_insert_rowid();

INSERT INTO orders_details ([order_id],[service_id],[cloth_id],[desc],[deleted],[qty],[color],[cost]) VALUES (@order_id,@service_id,@cloth_id,@desc,@deleted,@qty,@color,@cost)

Pls waht am i doing wrong?

Was it helpful?

Solution

Don't see how you handle transactions in your code. Try this (in C#, sorry):

using(SqliteTransaction tr = con.BeginTransaction()){
   foreach(var itm in value.orderDetails) {
      //construct the command, add parameters, etc.
      ...
      //link the command to the transaction
      cmd.Transaction = tr;
      //execute the command
      cmd.ExecuteNonQuery();
   }
   tr.Commit();
}

OTHER TIPS

Not 100% sure this will fix it, but keep the same SQLiteCommand object for all of the order details. Declare it outside of the loop, and just update parameter values and run the query in the loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top