locked
if (Session[" currentOrderId"] != string.Empty) RRS feed

  • Question

  • User-605499000 posted

    Possible unintended reference comparison to get a value comparison  Cast the left hands id to type "string"  error.

    Below is the code.  I see that a lot of people are getting this error but still could not fix.

    bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
    if (ret)
    {
    // Retrieve PayPal confirmation value.
    string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();
    TransactionId.Text = PaymentConfirmation;

    ProductContext _db = new ProductContext();
    // Get the current order id.
    int currentOrderId = -1;
    if (Session[" currentOrderId"] != string.Empty)
    {
    currentOrderId = Convert.ToInt32(Session["currentOrderID"]);   ( here is where the error is)
    }
    Order myCurrentOrder;
    if (currentOrderId >= 0)
    {

    thanks

    Jen

    Friday, October 19, 2018 5:10 PM

Answers

  • User475983607 posted

    This syntax is incorrect.  Session returns an object and therefore you must cast the Session variable to the correct type.

    if (Session[" currentOrderId"] != string.Empty)

    For example...

    if (Session["currentOrderId"] != null && (string)Session["currentOrderId"] != string.Empty)

    The ASP Session support documents explain the how to use Session.

    https://msdn.microsoft.com/en-us/library/ms178581.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 19, 2018 5:47 PM

All replies

  • User475983607 posted

    This syntax is incorrect.  Session returns an object and therefore you must cast the Session variable to the correct type.

    if (Session[" currentOrderId"] != string.Empty)

    For example...

    if (Session["currentOrderId"] != null && (string)Session["currentOrderId"] != string.Empty)

    The ASP Session support documents explain the how to use Session.

    https://msdn.microsoft.com/en-us/library/ms178581.aspx?f=255&MSPPError=-2147217396

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 19, 2018 5:47 PM
  • User-605499000 posted

    Thank you so much that worked beautifully.  I was close thinking that I needed null but nothing I did worked. I am not a programmer and trying to redo my business site that was built in 2009.  I chose Wingtops as a way to do it but have run into many problems.

    Thank you so much for your help.  Everyone on this forum is excellent and has helped me so much.

    Jen

    Friday, October 19, 2018 5:58 PM