Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Move data between forms

Move data between forms

Scheduled Pinned Locked Moved C#
databasesecurityquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sea
    wrote on last edited by
    #1

    I have a good authentication form, which I use to login to my app. Now I want to be able to know which user is logged-in now, in order to fill his answers in DB. In other words: I need to save & copy username (stored in text box of the authentication form) to other parts of application. I have Tried to save it in a static variable, and the call it from another form- no use. Any idea how would I do that? TIA, sea#

    S L 2 Replies Last reply
    0
    • S sea

      I have a good authentication form, which I use to login to my app. Now I want to be able to know which user is logged-in now, in order to fill his answers in DB. In other words: I need to save & copy username (stored in text box of the authentication form) to other parts of application. I have Tried to save it in a static variable, and the call it from another form- no use. Any idea how would I do that? TIA, sea#

      S Offline
      S Offline
      Snowblind37
      wrote on last edited by
      #2

      You would not be able to call a static variable from another form as it would have been unique to the object form that you had running earlier. If you need to save information accross instances of your program try saving the information to a file. You can set and get information as you need and it will last until you decide to erase it. Of course, you need to write methods to write and retrieve info, no easy way out. But it will be worth it. CodeBlind

      1 Reply Last reply
      0
      • S sea

        I have a good authentication form, which I use to login to my app. Now I want to be able to know which user is logged-in now, in order to fill his answers in DB. In other words: I need to save & copy username (stored in text box of the authentication form) to other parts of application. I have Tried to save it in a static variable, and the call it from another form- no use. Any idea how would I do that? TIA, sea#

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        It depends, are you writing a Windows Forms or an ASP.NET application? I will assume Windows Forms. What I do is create a User class that has properties like name, database ID, and anything else you would like to know about a user. It also has a Current static property, which is initializes by a static method taking user name and password. Something like this:

        class User
        {
            // Private fields, and properties to access user information.
            // The properties could also have setters if you have a Save method
            // that will persist the changes to the database.
            private int id;
            private string name;
         
            public int Id
            {
                get { id; }
            }
         
            public string Name
            {
                get { return name; }
            }
         
            // Constructor that gets the info for one customer
            public User(int id)
            {
                // Load id and name from database
                ....
            }
         
            // Static methods and properties for current logged in user
            static private User current;
         
            // Static property to return currently-logged in user. Since the field
            // is private, it can't be created from the outside.
            public static User Current
            {
                get { return current; }
            }
         
            // Call this method to try to login a user.
            public static bool LogIn(string userName, string password)
            {
                // Try to find a record in the database with the specific user name and
                // password
        
                if(found)
                {
                    current = new User(foundId);
                    return true;  // User logged in
                }
         
                return false;  // No user logged in
            }
        
            public static void Logout()
            {
                current =  null;
            }
        }
        

        Then I always have a global object with info about the current user, so I can do things like this:

        if(User.Current != null)
            sbpUserName.Text = User.Current.Name;  // Display in status bar
        else
            sbpUserName.Text = "(please log in)";
        

        I hope this helps! Good luck! -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups