Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am creating an application where the user will login and then start working. What I want is that once the user has logged in, it will create a session where the data saved by the user stays in that particular session.

I have a login form created that fetches its data from SQL Server but now I want to connect the two forms and create a session. So after logging in, it should show "Welcome *Username*"

Please help.

What I have tried:

I do not know how to create a session in C# WinForms.
Posted
Updated 31-Jan-21 19:53pm

There is no such thing as a "session" in WinForms: it's a construct that is needed in websites because you may have multiple users logged in at the same time, and a single application (IIS) takes care of them all. Each user gets a Session so that his data can be kept distinct from all the others, without needing the overhead of a complete application instance running for each separate user.

With Winforms, it's different: normally there is only one user per machine, and the application runs for just that one user: if you need two users on the same machine at the same time, you run a second instance of the application. You don't need a "session" because there is no other user in the application to confuse things with!

So your process is:

Start app.
Open login form.
Verify user.
Hide or close login form, open main form, pass user details to main form.
The main form then stores the user info as part of it's form instance, and displays your "welcome" message.
 
Share this answer
 
If I understand the question correctly, one simple way is to use a static class to store application wide information. For example

C#
public static class MyAppData {
   public bool LoggedIn { get; set; }
   public string UserName { get; set; }
}

You can access the data simply using for example
C#
if (!MyAppData.LoggedIn) {
   ...
}
...
MessageBox.Show("$"Welcome {MyAppData.UserName}");
 
Share this answer
 
Comments
Primo Chalice 10-May-18 5:19am    
Yes, something like this only. So how shall I proceed?
Wendelius 10-May-18 6:05am    
Not sure I understand your question. Create the static class and store the information needed in the class properties you define in the class.
Primo Chalice 10-May-18 6:11am    
Alright. I will try. But I think you understood. I will explain in brief.

Say I have a login form and I login with user "X". As soon as the login is successful, a MessageBox will appear saying "Hello X". The Login form will close and MainForm will open. (I have done this without the message). After the MainForm is loaded, I will run a condition if Username==X then save all data in a table in SQL.

So somewhat every user will have a separate table for their stored work and progress.

I have the tables created.
Wendelius 10-May-18 6:26am    
Okay, so after successful login, store the username in the static class and when needed in another form, fetch the username from there. Note that the static class is not seen by any other application so in this sense this differs from session variables in ASP.Net

What comes to the tables, having separate tables for different users sounds like a bad idea. It is a common practice to share the table among users so I recommend reconsidering that approach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900