0

when i leave the page i want to capture the current Time. i have tried this below code but its not working please help me

protected void Page_Unload(object sender, EventArgs e)
        {
            SqlCommand cd = DbCon._dbConnection.CreateCommand();
            cd.CommandType = CommandType.Text;
            cd.CommandText = "update ViewTime set ViewExit= '" + DateTime.Now.ToString() + "' where ViewExit is null";
            cd.ExecuteNonQuery();
        }

and tried this below code

     protected void Page_Unload(object sender, EventArgs e)
       {
          SqlCommand cd = DbCon._dbConnection.CreateCommand();
          cd.CommandType = CommandType.Text;
          cd.CommandText = "update Timepdf set Viewstop= '" + 
          DateTime.Now.ToString() + "' where Viewstop is null";
          cd.ExecuteNonQuery();
       } 
  • 1
    The unload event fires when the page is unloaded from the processing pipeline on the server **not** when the user closes the page. – Alex K. Jul 07 '21 at 12:10
  • You can try to use [`window.onbeforeunload`](https://stackoverflow.com/questions/28627111/how-to-call-a-function-before-leaving-page-with-javascript). – Yiyi You Jul 08 '21 at 02:13

1 Answers1

0

The Unload event will be called every time in the windows queue. That is wrong approach to update DB there. Implement javascript onunload and call your ASP.Net method to update DB.

https://www.w3schools.com/jsref/event_onunload.asp