You can run your executable either at system shutdown or at user logoff using group policy editor. It works smoothly i use it all the time.
Type gpedit.msc at a command line prompt, and navigate either to :
computer settings-> windows settings -> scripts (startup / shutdown)
user settings-> windows settings -> scripts - >(logon / logoff)
There you can add a batch script or vbscript that simply executes your application.
If you application runs on the background and you want it to get notified when system is being terminated, then probably the only role of the script could be to notify your application (by p.e. writing something to a file).
Dim Adapter As New OleDbDataAdapter
Dim Command As New OleDbCommand
Dim xFrom As String = dtpFrom.Value.ToString.Substring(0, dtpFrom.Value.ToString.IndexOf(" "))
Dim xTo As String = dtpTo.Value.ToString.Substring(0, dtpTo.Value.ToString.IndexOf(" "))
sqlQuery = MainQuery ...