title: Setting and retrieving cookies in c# code behind
body: <p>I attempt to save the selection of a dropdown to a cookie in the SelectedIndexChanged event.</p>
  
  <pre><code>protected void BranchNumberList_SelectedIndexchanged(object sender, EventArgs e)
  {
     HttpCookie myCookie = new HttpCookie("default_Loc", BranchNumberList.SelectedValue);
     myCookie.Expires = DateTime.Now.AddDays(365);
     Response.Cookies.Add(myCookie);
     ViewDate.Enabled = true;
     SelectEverything();
  }
  </code></pre>
  
  <p>myCookie looks fine and I can see it in the response object with a quickwatch.</p>
  
  <p>I try to retrieve it on the next login when this method is called from Page_Load.</p>
  
  <pre><code>private void BranchName()
  {
     DatabaseHelpers dh = new DatabaseHelpers();
     DataSet DrpDownSrc = dh.FillBranchSelection(objConn);
     BranchNumberList.DataSource = DrpDownSrc;
     BranchNumberList.DataTextField = "BranchName";
     BranchNumberList.DataValueField = "LocationID";
     BranchNumberList.DataBind();
     BranchNumberList.Items.Insert(0, "Select a branch");
     try
     {
        BranchNumberList.SelectedValue = this.Request.Cookies["default_Loc"].Value;
     }
     catch (Exception)
     {
        BranchNumberList.SelectedIndex = 0;
     }
  }
  </code></pre>
  
  <p>I always get 'this.Request.Cookies["default_Loc"]' is null.</p>
  
  <p>Can anyone see where I am going wrong?</p>