0
votes

So i made 2 pages, one for settings and one for label and images. the thing that i want is when there is no cookies the img_gm.BorderWidth and img_gm.Bordercolor will be 1 and Black by default, but it seems error always occur in libe "img_gm.BorderWidth = Convert.ToInt32(cookie["width"]);" any help will be appreciated.

*i put text box, dropdown list and radio button in settings page to get an input of cookies["width"], cookies["color"] and cookies["font"]

protected void Page_Load(object sender, EventArgs e) {

    HttpCookie cookie = Request.Cookies["settings"];
    if (Request.Cookies.AllKeys.Contains("settings") == null)
    {
        cookie["width"] = "1";
        cookie["color"] = "Black";
        cookie.Expires = DateTime.Now.AddDays(14);
        Response.Cookies.Add(cookie);
    }
    else
    {

        img_gm.BorderWidth = Convert.ToInt32(cookie["width"]);
        img_gm.BorderColor = System.Drawing.Color.FromName(cookie["color"]);
        switch (cookie["font"])
        {
            case "Bold":
                lbl_desc.Font.Bold = true;
                break;
            case "Italic":
                lbl_desc.Font.Italic = true;
                break;
            case "Overline":
                lbl_desc.Font.Overline = true;
                break;
            case "Underline":
                lbl_desc.Font.Underline = true;
                break;
        }

    }
1
Why do you checking Response.Cookes.AllKeys in your if block? Maybe it should be Request?Sergey Litvinov
oh yeah i mixed up between those, i wrote request in my code but it still gives me an error in " img_gm.BorderWidth = Convert.ToInt32(cookie["width"]);"AHA27
If you set breakpoint and check cookie["width"] value what value does it have? maybe it has some old testing value that isn't integer, so it fails during parsing? and cookie just needs to be cleared in the browseR?Sergey Litvinov

1 Answers

0
votes

A neat trick is to change them to Properties in your class:

public int CookieWidth {
  get {
    int width;
    var str = String.Format("{0}", cookie["width"]);
    if (!Integer.TryParse(str, width)) {
      width = 1;
    }
    return width;
  }
  set {
    cookie["width"] = value;
  }
}

You can do that with Color, also.