-1

I'm trying to get the current logged (in my app) user in every winform of my app, but I don't know how to differentiate between multiple logged users in the app.

I have an ActiveUsers SQL Table with SessionID and UserID to identify a single user.

Also how can I close the session (delete the userid from the ActiveUsers table) if the app is terminated abnormally?

Any help?

Edit: The app is composed of several winforms and is intended to run in multiple Pcs at the same time. Users are created in the application and stored in SC_User table with UserID as PK. The ActiveUsers table has UserID as FK and SessionID as PK. What i want is to get the UserID of the user using the applicattion in any winform and use it to for example change the app language preference of the that user. For a single user i insert the UserID in the ActiveUsers when login and delete when logoff.

M.Gaudio
  • 21
  • 4
  • your question is not clear. Are you saying you have ONE desktop application and each form in your app is using a different SQL USER?BTW, if you record user entrance and exit in your own table, I don't see any elegant way to handle abnormal termination. Please make your question more clear – FLICKER May 04 '16 at 00:20
  • You need to edit this question so that it makes more sense to someone who knows nothing about your application. – pmcilreavy May 04 '16 at 03:26
  • looks like you need use `SessionID` in the `ActiveUsers` table to track rather than `UserID` – Squirrel May 04 '16 at 04:34
  • Let's see if I understand correctly: Your application runs multiple copies on multiple computers, each by a different user. All users login to the application and that inserts a record to the ActiveUsers table with the user id and SessionId. Now, your question is how to delete that record when the application is closed without proper logging out (i,e, a computer shut down because of power failure)? Or is your question how to keep the user id and session id in the application while the user opens a new form and closes the old one? – Zohar Peled May 04 '16 at 08:23
  • Thats right. The winform problem is the main question. – M.Gaudio May 04 '16 at 15:42

1 Answers1

0

Keeping application-wide data is quite easy. All you have to do is add either a static (shared in vb.net) class or a singleton class to your project and store the application-wide data there. (Extra read: differences between singleton and static)

As for the second question, You don't really have a way to know if the client is closed without proper logout. What you do in these cases is implement a keep-alive mechanism.

A simple implementation would be to add another column to your session table to keep track off the time stamp when the logged in user was last active (usually a timer inside that static/singleton class will be in charge of updating this column every x time, and once the difference between this column and the current datetime is big enough, you can safely assume that the application is closed without proper logout.

You can even run a scheduled job on your sql server to delete the records on the session table where the application is closed without proper logout if you want to.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121