none
CookieID is always returning null. Getting error: "Nullable object must have a value." RRS feed

  • Question

  • using this code... (but its not code related error.. because same thing is working in other's system) i am guessing its because of some IE settings(using IE 8. ) or something else.. spent 2 days on this.. but cant.. please anyone help me.. 
     public long GetCookieId()
            {
                long? cookieId;
                string cookieIdString = CookieManager.GetClientCookie("shop");
                if (string.IsNullOrWhiteSpace(cookieIdString))
                {
                    cookieId = MiniPSSHelper.CreateNewCookieId();
                    CookieManager.SetClientCookie(Constants.Cookies.ShopCookieName, cookieId.ToString());
                }
                else
                    cookieId = long.Parse(cookieIdString);
                return (long)cookieId;
            }
    Monday, December 26, 2011 10:15 AM

All replies

  • Update the second statement in 'if' block as,

    CookieManager.SetClientCookie(Constants.Cookies.ShopCookieName, cookieId.Value.ToString());

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Edited by Adavesh Monday, December 26, 2011 10:26 AM
    Monday, December 26, 2011 10:26 AM
  • first statement in if block itself returning null value... 

    cookieId = MiniPSSHelper.CreateNewCookieId(); //returning null

    Monday, December 26, 2011 10:30 AM
  • Ok. Did you debug CreateNewCookieId method in your MiniPSSHelper class? since it is your code, we can't tell what is going on there. Just check whether Cookies are enabled in the problematic machine. If not, enable cookies and try.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, December 26, 2011 11:02 AM
  • Yes. under internet options,  i changed the cookie setting by dragging it to bottom (accept all cookies)under  privacy tag . 

    Still its not working. if its not the correct way.. can you please tell me the correct way?

    Monday, December 26, 2011 11:15 AM
  • Yeah. That is correct. Then only option is to debug your MiniPSSHelper.CreateNewCookieId() method to know what is going wrong. Can you post the code of that method? Or you yourself can debug and check for problem?
    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, December 26, 2011 11:19 AM
  • i tried.. its not working.. (same code is working in other's system)

    this is the code.. 

    public class MiniPSSHelper

        {

            public static Int64? CreateNewCookieId()

            {

                CookieIdInfo cookieIdInfo = null;

                PSSServiceBroker pssBroker = new PSSServiceBroker();

                try

                {

                    cookieIdInfo = pssBroker.CreateNewCookieIdRESTful();

                }

                catch

                { return null; }

                if (cookieIdInfo == null)

                { return null; }

                if (!cookieIdInfo.Id.HasValue)

                { return null; }

                if (cookieIdInfo.Errors != null)

                {

                    if (cookieIdInfo.Errors.Count > 0)

                    { return null; }

                }

                return cookieIdInfo.Id;

            }

    }

     

     

     

     

    //CreateNewCookieIdRESTful

     public Contract.CookieIdInfo CreateNewCookieIdRESTful()

            {

                Contract.CookieIdInfo cookieIdInfo = null;

                try

                {

                    stopwatch.Start();

                    if (isDirectAccess())

                    {

                        cookieIdInfo = serviceDirect.CreateNewCookieIdRESTful();

                    }

                    else

                    {

                        cookieIdInfo = PersistentSessionManager.CreateNewCookieIdRESTful();

                    }

                }

                catch (Exception ex)

                {

                    Log.Write(ex, LogType.Error); 

                }

                finally

                {

                    if (stopwatch.IsRunning)

                        stopwatch.Stop();

                    Log.Write(string.Format("PSS:{0} - {1}ms for Entities: {2}", "CreateNewCookieIdRESTful", 

                                            stopwatch.ElapsedMilliseconds, 

                                            getEntityInfo(cookieIdInfo,isDirectAccess())), 

                                            LogType.Info);

                }

                return cookieIdInfo;

            }

    Monday, December 26, 2011 11:25 AM
  • CreateNewCookieIdRESTful swallows all exceptions and returns null instead which in turn will cause CreateNewCookieId to return null. Have you checked that no exception are thrown?

    Monday, December 26, 2011 3:10 PM
    Moderator