Note that there are some explanatory texts on larger screens.

plurals
  1. POCookieHelper Error, creating cookie again and again
    primarykey
    data
    text
    <p>Created this simple class to create cookie. But creating cookie again and again.</p> <pre><code>using System; using System.IO; using System.Web; using System.Web.UI; /// &lt;summary&gt; /// Summary description for CookieHelper /// &lt;/summary&gt; public class CookieHelper { private Page _page = null; public bool success { get; private set; } public bool CreateSuccess { get; private set; } protected bool SaveSuccess { get; private set; } protected bool DeleteSuccess { get; private set; } public bool IsExisting { get;private set; } public string cookieName { get; set; } public CookieHelper(Page page) { _page = page; CreateSuccess = SaveSuccess = IsExisting = DeleteSuccess = false; } public bool Exist(string cookieName) { if (string.IsNullOrEmpty(cookieName)) throw new ArgumentNullException("cookieName can't be null"); else { try { if (_page.Request.Cookies[cookieName] == null) { IsExisting = false; return false; } else { IsExisting = true; return true; } } catch (ArgumentException) { throw new ArgumentException("cookieName given to Exist check function is invalid or not string"); } } } public void CreateCookie(string cookieName, int expiryTimeInMinutes=1, bool secure = true) { if (string.IsNullOrEmpty(cookieName)) throw new ArgumentNullException("cookieName can't be null"); else { try { if (Exist(cookieName)) { IsExisting = true; return; } else { HttpCookie cookie=new HttpCookie(cookieName); cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes); cookie.Secure = secure; CreateSuccess = true; IsExisting = true; } } catch (ArgumentException) { throw new ArgumentException(string.Format("CookieName type mismatch. The parameter is of type '{0}', was expecting '{1}'.", cookieName.GetType(), "a".GetType())); } } } public bool SaveInCookie(string cookieName, string valueKey, string valueToBeStored, int expiryTimeInMinutes, bool secure = true) { SaveSuccess = false; if (typeof(string) != cookieName.GetType() || typeof(string) != valueKey.GetType() || typeof(string) != valueToBeStored.GetType() || typeof(bool) != secure.GetType() || typeof(int) != expiryTimeInMinutes.GetType()) throw new ArgumentException("Parameter type mismatch"); else { try { HttpCookie cookie = null; if (_page.Request.Cookies[cookieName] == null) { cookie = new HttpCookie(cookieName); CreateSuccess = true; IsExisting = true; } else { cookie = _page.Request.Cookies[cookieName]; IsExisting = true; CreateSuccess = false; } cookie.Values.Add(valueKey, valueToBeStored); cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes); cookie.Secure = secure; _page.Response.Cookies.Add(cookie); } catch (Exception) { SaveSuccess = false; throw new Exception("Cookie Save Failed"); } return SaveSuccess; } } public string GetCookieValue(string cookieName, string valueKey = null) { if (typeof(string) != cookieName.GetType() || typeof(string) != valueKey.GetType()) throw new ArgumentException("Parameter type mismatch"); else { string keyExist = string.Empty; try { if (Exist(cookieName) == IsExisting) { keyExist = (string)_page.Request.Cookies[cookieName][valueKey]; return ((string.IsNullOrEmpty(keyExist)) ? String.Empty : _page.Request.Cookies[cookieName].Values[valueKey] ); } else throw new FileNotFoundException("Cookie with the name provided doesnot exist"); } catch (Exception ex) { throw ex; } } } public bool DeleteCookie(string cookieName) { if (typeof(string) != cookieName.GetType()) throw new ArgumentException("Parameter type mismatch"); else { try { if (_page.Request.Cookies[cookieName] != null) { HttpCookie myCookie = new HttpCookie(cookieName); myCookie.Expires = DateTime.Now.AddDays(-1d); _page.Response.Cookies.Add(myCookie); IsExisting = false; } return true; } catch (Exception ex) { throw ex; } } } } </code></pre> <p>Called this class in my web page as-</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { CookieHelper cook = new CookieHelper(this.Page); if (!cook.Exist("Preferences")) { cook.CreateCookie("Preferences",2); Response.Write("Created"); } else Response.Write("Not"); } </code></pre> <p>Giving me always Created. I added breakpoint and checked in debug and found that compiler always goes to creating section. Please suggest me to create this helper class.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload