Cookie or Session Variable not updating


  • 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 =;
        //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 !=
        <audio src = "/sounds/alert.wav" autoplay ></audio >
        Request.Cookies["Time"].Value = @item2.time.ToString("HH:mm");
        Request.Cookies["No"].Value =;
       <td width="30%" style="padding-left:10px; color:red"></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"></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

    • Diedit oleh G-Oker Kamis, 07 Juni 2018 08.49
    • Dipindahkan oleh CoolDadTx Kamis, 07 Juni 2018 18.26 ASP.NET related
    Kamis, 07 Juni 2018 08.46

Semua Balasan