CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2010
    Posts
    2

    DropDownlist value returns 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()
    ASP.NET Syntax (Toggle Plain Text)

    Code:
    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;
            }
    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:

    Code:
    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);
                // get tax ID or defalut to "No Tax"
            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);
                // process order
            OrderProcessor processor = new OrderProcessor(orderId); 
            processor.Process();    
                // redirect to confirmation page
            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. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: DropDownlist value returns 0

    Could it be you rebind the drop down (or reassign) on post back?

  3. #3
    Join Date
    Jun 2010
    Posts
    2

    Re: DropDownlist value returns 0

    Hi I solved the problem, somehow in this line of code where the DropDown is populated it was setting it to 0:

    shippingSelection.SelectedIndex = 0;

    I removed that and had to put where the DropDown is populated in its own method.

  4. #4
    Join Date
    Jun 2010
    Location
    Cairo, Egypt
    Posts
    17

    Re: DropDownlist value returns 0

    hay,
    if you are populating your dropdownlist in the page load with out checking the IsPostBack
    then it will lose its selection while you postback

    please don't forget to remark the thread as resolved
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    010011000111010101110110001000000100110101111001001000000101000001100011

  5. #5
    Join Date
    Jun 2010
    Location
    Cairo, Egypt
    Posts
    17

    Re: DropDownlist value returns 0

    hay,
    if you are populating your dropdownlist in the page load with out checking the IsPostBack
    then it will lose its selection while you postback

    please don't forget to remark the thread as resolved
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    010011000111010101110110001000000100110101111001001000000101000001100011

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured