title: How to read a cookie
body: <p>I'm trying to save a cookie and then read it. Unable to compile as there is an error in code for the lines (readcookie[....] )- </p>
  
  <pre><code>Response.Write("USERNAME: " + readCookie["userInfo"]["userName"].ToString());
  </code></pre>
  
  <p>which says - "best overloaded method match for string.this[int] has invalid arguments.</p>
  
  <p>If I write, </p>
  
  <pre><code> Response.Write("USERNAME: " + readCookie["userInfo"][0].ToString());
  </code></pre>
  
  <p>then I can compile. It gives the 'data saved' message on save_click but on read_click, it shows an error - object reference invalid. Is it correctly saving a cookie in the first place. How can I read from it. Here's my code:</p>
  
  <pre><code>protected void btnSaveCookie_Click(object sender, EventArgs e)
      {
          HttpCookie myCookie = new HttpCookie("userInfo");
          myCookie.Values["userName"] = "Patrick";
          myCookie.Values["lastVisit"] = DateTime.Now.ToString();
          Response.Cookies.Add(myCookie);
          Response.Write("Data Stored Into Cookie....");
      }
  
  protected void btnReadCookie_Click(object sender, EventArgs e)
      {
          HttpCookie readCookie = Request.Cookies.Get("userInfo");
          Response.Write("USERNAME: " + readCookie["userInfo"]["userName"].ToString());
          Response.Write("LAST VISIT: " + readCookie["userInfo"]["lastVisit"].ToString());
      }
  </code></pre>