Hi everyone, I've got a shopping cart with a total amount label, this works perfect with the products, if a product quantity is changed the label also changes. I have a shipping type drop down list determined by which region the user is, I would like the total amount label to add this cost to but can't seem to get it to work. I've used a stored procedure, which I have executed and it works.

Here is my code;
Checkout.aspx.cs

public partial class Checkout : System.Web.UI.Page
{
    decimal shippingCost;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }

        // fill controls with data
    private void PopulateControls()
    {
            // get the items in the shopping cart
        DataTable dt = ShoppingCartAccess.GetItems();
            // populate the list with shopping cart content
        grid.DataSource = dt;
        grid.DataBind();
        grid.Visible = true;
            // ******* display the total amount
        decimal amount = ShoppingCartAccess.GetTotalAmount() + shippingCost;
        totalAmountLabel.Text = String.Format("{0:c}", amount);
            // check customer details
        bool addressOK = true;
        // bool cardOK = true;
        if (Profile.Address1 + Profile.Address2 == ""
            || Profile.ShippingRegion == ""
            || Profile.ShippingRegion == "Please Select"
            || Profile.Country == "")
        {
            addressOK = false;
        }

            // report/hide place order button
        if (!addressOK)
        {
            InfoLabel.Text =
                    "You must provide a valid address before placing your "
                    + "order.";
        }
        else
        {
            InfoLabel.Text =
                "Please confirm that the above details are "
                + "correct before proceeding.";
        }
        placeOrderButton.Visible = addressOK; // && cardOK;
        shippingSelection.Visible = addressOK; // && cardOK;

            // populate shipping selection
        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;
        }
    }
        
        // get shipping cost
    protected void shippingSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        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;
        }

        shippingCost = CommerceLibAccess.GetShippingCost(shippingId);
    }

Recommended Answers

All 19 Replies

you have a long code here . Can you point the line that you are having a problem with ?

Thanks for the reply.

Here is where I retrieve the ShippingId from the DropDownList, which is sent to a stored procedure that return the selected Id's ShippingCost

protected void shippingSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        int shippingId = 0;
        int.TryParse(shippingSelection.SelectedValue, out shippingId);

        shippingCost =   CommerceLibAccess.GetShippingCost(shippingId);
        PopulateControls();
    }

Here I want the ShippingCost to add to the TotalCost and display in totalAmountLabel

private void PopulateControls()
    {
            // display the total amount
        decimal amount = ShoppingCartAccess.GetTotalAmount() + shippingCost;
        totalAmountLabel.Text = String.Format("{0:c}", amount);

When you debug this , does this give you the correct answer you want

decimal amount = ShoppingCartAccess.GetTotalAmount() + shippingCost;

what i mean does the shippingCost variable carry the correct answer ?

Second Question

In the method PopulateControls() does amount bring the correct answer ?

If yes give me an example of a value and show me the wrong display that you see and tell me show me what it supposed to display.

Kind Regards

Vuyiswa Maseko

1.

decimal amount = ShoppingCartAccess.GetTotalAmount() + shippingCost;

Does not give me the correct answer, it only displays the GetTotalAmount() method not the shippingCost.

In PopulateControls() the amount variable displays and changes according to update of quantities, this works perfect. But I've got a DropDownList with ShippingCost, I want the amount variable to add this shippingCost to the amount variable and display it in the totalAmountLabe, but it does not work.

ok let us solve the problen first here

decimal amount = ShoppingCartAccess.GetTotalAmount() + shippingCost;

Debug it tell me the return value of this

ShoppingCartAccess.GetTotalAmount()

, you can move your mouse over while debuging it and tell me which one is carrying the incorrect values between shippingCost and ShoppingCartAccess.GetTotalAmount()

Thanks for the replies.
ShippingCost is not passing a value, I do not know how to fix this.

Now this means that the problem is here

CommerceLibAccess.GetShippingCost(shippingId);

Show me the code for

GetShippingCost();
public static decimal GetShippingCost(int shippingId)
    {
            // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
        comm.CommandText = "CommerceLibShippingCost";
            // create a new paramater
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@ShippingID";
        param.Value = shippingId;
        param.DbType = DbType.String;
        comm.Parameters.Add(param);
            // execute the stored procedure
        return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));
    }

The stored procedure:

ALTER PROCEDURE CommerceLibShippingCost
(@ShippingID int)
AS
SELECT ShippingCost
FROM Shipping
WHERE ShippingID = @ShippingID

when I execute the stored procedure and insert a shippingId, the correct cost is returned, so I do not think the problem lies in the stored procedure.

Thanks for all your help.

First i dont trust that this brings the correct value

return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));

i suggest you use the output variable in your storedprocedure and in your c# code accept it. understand?

No, I don't know what you mean, but I do think you are right I am calling the wrong method from GenericDataAccess.

return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));

Change your Sp to look like this

ALTER PROCEDURE CommerceLibShippingCost
 
      (@ShippingID int,
       @Results int OUTPUT
)
 
      AS
 SET @Results = ( SELECT ShippingCost
                            FROM Shipping
                           WHERE ShippingID = @ShippingID)

and in your C# add the parameter and set the direction like this

comm.Parameters["@Results"].Direction = ParameterDirection.Output;

and after you execute the

Comm.ExecuteNonQuery();

then get the value like this

int Results=   (int) comm.Parameters["@Results"].Value;

understand ?

Will all the code be in the GetShippingCost() method?

Thanks for all the help, but it still won't work.

when you say it does not work do you still receive incorrect value form the variable ?

I'm receiving no value from the variable.

Could it be that the shippingId is not correctly determined? shippingSelection is the DropDownList.

int shippingId = 0;
int.TryParse(shippingSelection.SelectedValue, out shippingId);

and must this code to do all of this be in this method?

protected void shippingSelection_SelectedIndexChanged1(object sender, EventArgs e)

let me see the Structure of the table Shipping

ShippingID int not null
ShippingType varchar(100) not null
ShippingCost money not null
ShippingRegionID int null

Are you Sure that in the ShippingCost Field there are values ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.