0

Hi, I've got a DropDown list which is populated here.

with the use of a break in the code there is a value of 4 for shippingInfo.ShippingID.ToString()

if (addressOK) //&& cardOK
        {
            int shippingRegionId = int.Parse(Profile.ShippingRegion);
            List<ShippingInfo> shippingInfoData =
                CommerceLibAccess.GetShippingInfo(shippingRegionId);
            foreach (ShippingInfo shippingInfo in shippingInfoData)
            {
                shippingSelection.Items.Add(
                    new ListItem(shippingInfo.ShippingType,
                        shippingInfo.ShippingID.ToString()));
            }
            shippingSelection.SelectedIndex = 0;
        }

then when the checkout button is selected here:

protected void placeOrderButton_Click(object sender, EventArgs e)
    {
            // store the total amount
        decimal amount = ShoppingCartAccess.GetTotalAmount();
            // get shippingId or default to 0
        int shippingId = 0;
        int.TryParse(shippingSelection.SelectedValue, out shippingId);
        string shippingRegion =
            (HttpContext.Current.Profile as ProfileCommon).ShippingRegion;
        int taxId;
        switch (shippingRegion)
        {
            case "2":
                taxId = 1;
                break;
            default:
                taxId = 2;
                break;
        }
            // create the order and store the order id
        string orderId =
            ShoppingCartAccess.CreateCommerceLibOrder(shippingId, taxId);
        OrderProcessor processor = new OrderProcessor(orderId);
        processor.Process();
        Response.Redirect("OrderPlaced.aspx");
    }

with the use of a breakpoint the shippingId is 0 although a selection is made. Therefore I get an error

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_Shipping". The conflict occurred in database "firebyte", table "dbo.Shipping", column 'ShippingID'.

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by jellybeannn
0

i had a similar problem with a drop down list. My problem was if the user didn't change the ddl it didn't return any values, although the ddl was propperly populated
I used something like

if(shippingSelection.SelectedValue != 0)
{
shippingId = shippingSelection.SelectedItem.Value;
}
else
{
shippingId = 1; //or whatever value a user might use most frequently
}

I know its not at all elegant but it worked for me when i needed a quick fix

Edited by ious: n/a

0

check if shippingSelection.SelectedValue is actually an integer
If it's not it's gonna pass it with the initial value.
if you put a int.Parse(shippingSelection.SelectedValue) my guess is that you're gonna have "Input string was not in a correct format"

Edited by ious: n/a

0

I just removed

shippingSelection.SelectedIndex = 0;

now it works, I think it was changing the DropDown to 0 the entire time.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.