Cookie or Session Variable not updating RRS feed

  • Question

  • Hello,
    I have a page that is running an SQL query and looking for a certain keyword in an items name. If the keyword is found, It then looks for a cookie. If the cookie doesn't exist, it then creates one.
    On the next iteration (20 secs) the cookie will be located, and, if the values don't match, then it plays a sound, and then overwrites the cookie with the new values.
    On the next iteration, the values match, so the cookie won't be overridden, and no sound is played... or .. that's what I am trying to do.
    However, the values do not seem to be updated.
    My code is
    string alertWord2 = "NOW";
    @if (item2.item_name.StartsWith(alertWord2))
      if ((Request.Cookies["Time"] == null) && (Request.Cookies["No"] == null))
        Response.Cookies["Time"].Value = @item2.time.ToString("HH:mm");
        Response.Cookies["No"].Value = @item.no.ToString();
        //Response.Cookies["Time"].Expires = DateTime.Now.AddSeconds(25);
        //Response.Cookies["No"].Expires = DateTime.Now.AddSeconds(25);
       if (Request.Cookies["Time"].Value != @item2.time.ToString("HH:mm") || Request.Cookies["No"].Value != @item.no.ToString())
        <audio src = "/sounds/alert.wav" autoplay ></audio >
        Request.Cookies["Time"].Value = @item2.time.ToString("HH:mm");
        Request.Cookies["No"].Value = @item.no.ToString();
       <td width="30%" style="padding-left:10px; color:red">@item2.name</td>
       <td width="30%" style="padding-left:10px; color:red; font-weight:bolder">@item2.time.ToString("HH:mm")</td>
       <td width="20%" style="padding-left:10px; color:lawngreen; font-weight:bold">TABLE: @item.table</td>
       <td width="10%" style="padding-left:10px; color:red">@item.no</td>
    What I am trying to do  it for it to look for through the items for any that start with NOW, and if that items time and Number (No) are different (No may be greater or lesser, Time should always be greater) to that in the cookie, play a sound, and update the cookie with the latest values (so next time around, if no extra item starting with NOW has been added/found, the cookie will not be written to and the sound will not play).
    I understand that cookies may not be the way to go here, and Session variable(s) might be a better option, so I have tried this in HomeController.cs
        public class HomeController : Controller
            public ActionResult Index()
                Session["awayTime"] = "";
                return View();
    ..and then this code snippet in on of the subpages
       if (Session["awayTime"] == null)
         Session["awayTime"] = @item2.time.ToString("HH:mm");
       if ((Session["awayTime"].ToString() != @item2.time.ToString("HH:mm"))
        <audio src = "/sounds/alert.wav" autoplay ></audio >
        Session["awayTime"] = @item2.time.ToString("HH:mm");

    So, the Index pages loads up first (and sets the session variable to a null value, and the user (me) picks the subpage. Becuase on the first run, the variable is null, it assigns a value, then when the page is reloaded, the variable value doesn't match the existing one, so plays a sound, then reassigns the value.

    However.... neither the cookie option nor the Session variable seems to work. the cookie does not update with the new details, and I am not sure I am using the session variable correctly, as both options play the sound on reload (plus, if the user bookmarks the subpage instead of the index, the session variable solution (as it is) will not work).

    Can someone help me, please ?

    Thank You

    • Edited by G-Oker Thursday, June 7, 2018 8:49 AM
    • Moved by CoolDadTx Thursday, June 7, 2018 6:26 PM ASP.NET related
    Thursday, June 7, 2018 8:46 AM

All replies

  • ASP.NET issues are addressed at the ASP.NET forum.


    Thursday, June 7, 2018 8:50 AM
  • Sorry about that. I have moved to the ASP forum. thank you
    Thursday, June 7, 2018 9:00 AM