Page 1 of 1

Adding an order via an external method

Posted: Tue Jul 08, 2008 5:21 pm
by RevereRichard
Hello,

I have need to create an order via a web service. I tried the following code for a test:

Order or = new Order();
or.UserId = UserID;
or.OrderDate = oDate;
LSDecimal lsd = new LSDecimal(oAmount);
or.TotalCharges = lsd;
or.Save();

and received the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint "ac_Orders_ac_OrderNotes_FK1"

Any help?

Thanks,

-Richard

Re: Adding an order via an external method

Posted: Tue Jul 08, 2008 10:28 pm
by AbleMods
First of all, "or" is a *really* bad variable name - hopefully that's just an example for the post.

Second, where are you getting the user ID? Web services don't have a .Net user associated with them during the authentication process. Hopefully this is something that you are obtaining elsewhere in the web service.

I can't reproduce what you're trying to do because the code you've posted isn't functional by itself. I can't be of much help.

Creating a new order is just a matter of creating a new instance of the order class and calling the save method. But there are several fields in the data classes that are not actual fields in the tables - they are calculated fields and were not designed to populated directly.

Re: Adding an order via an external method

Posted: Tue Jul 08, 2008 11:56 pm
by RevereRichard
The variables UserID, oDate and oAmount are supplied by an external source. For the purposes of this discussion just assume they are correct.

static void test(int UserID, DateTime oDate, decimal oAmount)
{
above code...
}

heh. yes or was just an example. :lol:

Re: Adding an order via an external method

Posted: Wed Jul 09, 2008 7:10 am
by jmestep
When I look at orders placed by customers, there is an order not associated at the time. Maybe that is a requirement? And/or if you are trying to insert it, maybe the key for the order is not generated before the insert to the Order notes table?

Re: Adding an order via an external method

Posted: Wed Jul 09, 2008 7:41 am
by AbleMods
Ok I see what you are trying to do.

The orders data class doesn't work that way though. It's designed from the perspective of a shopping cart system. You can't just make a new order class, load up a few fields and save it - you have to create a basket instance and process it through the checkoutrequest handler. You can do it with products, users etc because they are sub-components. But an order class combines all the various aspects of AC7 together and is dependent on many other data classes being properly prepared in advance.

This snippet is from the ~/Admin/Orders/PlaceOrder2.aspx.cs code file. Study this code file and you should be able to figure out how to do it. The basket is populated in ../PlaceOrder1.aspx.cs.

Code: Select all

//PROCESS THE CHECKOUT
            _Basket.Recalculate();
            Payment payment = new Payment();
            payment.Amount = _Basket.Items.TotalPrice();
            CheckoutRequest checkoutRequest = new CheckoutRequest(null);
            CheckoutResponse checkoutResponse = _Basket.Checkout(checkoutRequest);
            if (checkoutResponse.Success)
            {
                Session.Remove("PlaceOrderUserId");
                Response.Redirect("ViewOrder.aspx?OrderId=" + checkoutResponse.OrderId);
            }

Re: Adding an order via an external method

Posted: Wed Jul 09, 2008 12:19 pm
by RevereRichard
Thanks, will look into it.

-R