tags:

views:

715

answers:

8

Hi can I'm very new to windows forms. Here I want to maintain state (like session in web applications) in windows forms.

Actually i want to store user login details in session. But i think there is no concept of session in winforms. So what is the alternative method to handle this type of situation.

Regards, Nagu

+1  A: 

In winforms you can use variables that are exposed to other forms through methods or properties.

You can also use static variables.

rahul
thank you but here how to i set the startup page? I want to set login.aspx as starup page after successfull authentication I want to redirect to home page
Nagu
woa! @Nagu: I wonder how are you using aspx in winform application?
Hemant
login.aspx? Are you creating a web application?
rahul
sorry sorry just its login.cs only
Nagu
+2  A: 
public class MyForm : Form
{
     private string userName;
     private string password;
}

Since windows forms are statefull (opposed to stateless for web forms), you can just use a field in your Form class.

pb
can i use this values in all other pages in the application? if so how can i get username and password in home.cs page?
Nagu
Then you would need to make the fields internal or add properties to expose them.
pb
A: 

It's unclear to me whether you are talking about a web application or a stand along application based upon one of your responses. If you are talking about a web application, you can use the Session properties on the Page object.

It would set the variables like this:

Session["username"] = "Username";
Session["fullname"] = "User's full name";

You could then access like:

lblGreetings.Text = "Hi " + Session["fullname"];

Is that what you were after?

Travis
The post is about windows form, and not web apps.
Bhaskar
+1  A: 

There is no concept of Session variables in windows forms. What you can do is:

  1. Create a internal class that holds the User name and password and any other variables and enumerations needed across the application (Something like Common.cs). These can be accessed through public properties across the application.

  2. Have a parameterized constructor for all the forms and send the user name and the password whenever you are showing the form.

danish
Still I'm facing the same problem.. i declared two variables in login.cs like public string loginid = "123456";after that i try to access in home.cs like login lg = new login()MessageBox.Show(lg.loginid);It is showing null value.. What is the problem
Nagu
A: 

In reply to your comment to my first reply:

You are creating the new instance of the Login form. How is that supposed to have values. It is a Login form and hence I believe you will be closing it as the user enters user name and password and clicks OK or whatever.

Then, there is no way you can get the values from the Login form as it is closed. If you need to stick to this approach, this could be a way:

  1. Do not close the Login form, just hide it.
  2. Pass the current instance to the next form. Like this: In Login form:

    NextForm nxt = new NextForm(this);

The constructor of NextForm will look like:

public NextForm(LoginForm frm){
// Code here
}

Now in NextForm, you can access the properties through "frm".

danish
A: 

Use MVC. Have a Controller that controls your View (windows). Store your application state in the controllers, not in the windows. Have a MainController that instantiates LoginWindowController, etc. passing in the required state in the constructor.

jeyoung
hi where can i get a simple example to do this?
Nagu
A: 

In the following example, you would have a controller for each window or group of windows. The controllers would be passed to one another depending on how they need to collaborate (what knowledge they need to share, etc). The important thing is to keep your application state in the controllers and limit the windows to handling user input and events.

// pseudocode, because I do not know WinForms that much
class MainController
{
    private Guid securityToken;

    public Guid SecurityToken
    {
        get { return securityToken; }

        set { securityToken = value; }
    }
}

class LoginWindowController
{
    MainController mainController;
    LoginWindow    loginWindow;

    public LoginWindowController(MainController mainController)
    {
        this.loginWindow    = new LoginWindow(this);
        this.mainController = mainController;
    }

    public void Show()
    {
        loginWindow.IsVisible = true;
    }

    public void HandleLogin()
    {
        Guid token = 
            myobject.Authenticate(loginWindow.Username, loginWindow.Password);

        if (token != Guid.Empty)
        {
            mainController.SecurityToken = token;
        }   
    }
}
jeyoung
A: 

You need to think more in terms of scope than session; as long as an object remains in scope you will be able to pull values from its public properties/fields.

In your case it would make sense to store the user details in a static class:

public static class LoginInfo
{
    public static string UserID;
}

Now you can access the UserID simply from anywhere in your code:

MessageBox.Show(LogInfo.UserID);
Phil