locked
Class for getting information from database RRS feed

  • Question

  • Hello i need some help with some class problem iam haveing, i will post a quote from earlier post and my response to that, thanks for the help.
    Sessions are not the part of Window based application and there is nothing which you can utilize for your problem.
    The above explained method by Gnanadurai is perfect for you.
    Just create a class in your application or use any existing class, declare an static variable to store the current logged in user name. Static variable will retain its value through out the application.
    You can define a class like following:

    public static class Users
        {
            private static string _CurrentUserName;

            public static string CurrentUserName
            {
                get { return Users._CurrentUserName; }
                set { Users._CurrentUserName = value; }
            }      
                 
        }

    On Sucessful login of user you can set the username like:

    Users.CurrentUserName = "UserName";

    And Can use in other forms like:

    lblUserName.Text = Users.CurrentUserName;



    I have now done as above and i did then create a Label that i put in Window1.

    I named the Label = lblUserName, i did create a Event when the Label is loaded it should retrive the information but i have done something wrong because nothing appears
    Tuesday, August 18, 2009 11:57 PM

Answers

  • Label's event OnLoad occurs before you even have a chance to click a mouse button.
    Put label's initialization exact after Users.CurrentUserName = "UserName";
    Also mention, that you are assigning a constant value to your propertie
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 8:46 AM
  • Change
                    Window1 w2 = new Window1();
                    w2.Show();
                    Users.CurrentUserName = "UserName";
     to:

                    Users.CurrentUserName = "UserName";               
                    Window1 w2 = new Window1();
                    w2.Show();
                   
    Also, I suppose, you need to change

    Users.CurrentUserName = "UserName"

    to be

    Users.CurrentUserName = username.Text

    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 10:56 AM
  • Your existing request to the database gets only existance of user account. You should write another query to get user's name from the DB and then set its value to Users.CurrentUserName
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 11:09 AM
  •                
                    conn.Open();
    
                    myCommand.Connection = conn;
                    myCommand.CommandText = "SELECT [field_for_username] FROM users WHERE " /*Condition*/;
    
                    Users.CurrentUserName = myCommand.ExecuteScalar().ToString();
                    
                    conn.Close();


    Write the query by yourself, because only you may know DB schema.
    • Proposed as answer by Oleksandr Klymenko Thursday, August 20, 2009 10:35 AM
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 12:03 PM

All replies

  • Hi,

    It is a bit unclear what you are doing.  If you want to get the currently logged on windows user you can try one of these two ways

    Label label1 = new Label();
    label1.Text = System.Environment.UserDomainName + @"\" + System.Environment.UserName;
    label1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    Controls.Add(label1);
    

    If you have your own user database and login procedure then you need to set CurrentUserName at some point, and update the label afterwards either by setting the Text property manually or using databinding.

    Why lblUserName does not display anything is a bit hard to say without seeing the code.
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Wednesday, August 19, 2009 5:14 AM
  • Hi Alcstudio,

    here you have a useful article on windows identity

    http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx

    Yes, you will not be able to use the Session variables to store the logged in user information.

    Regards,
    Vishal Mohan

    Wednesday, August 19, 2009 7:28 AM
  • Hi all. I will now post some code so you better can see.

    Here is the CLASS:

      public static class Users
        {
            private static string _CurrentUserName;
    
            public static string CurrentUserName
            {
                get { return Users._CurrentUserName; }
                set { Users._CurrentUserName = value; }
            }
    
        }
    And here is the part when the program is suposed to get the username from the database.

            private void Cmd_Login_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                MySqlConnection conn = new MySqlConnection();
                MySqlCommand myCommand = new MySqlCommand();
    
                string myConnString = null;
                myConnString = "server=; user id=; password=; database=; pooling=; port=;";
                conn.ConnectionString = myConnString;
    
                int i;
    
                try
                {
                    conn.Open();
    
                    myCommand.Connection = conn;
                    myCommand.CommandText = "SELECT COUNT(*) FROM users WHERE account='" + username.Text + "' AND password='" + password.Text + "'";
    
                    i = Convert.ToUInt16(myCommand.ExecuteScalar());
                    
                    conn.Close();
    
                }
                catch
                {
                    i = 0;
                    connectionstatus.Content = "Error connecting to System";
                    ErrorBoxFade_BeginStoryboard.Storyboard.Begin();
                    ErrorBox.Visibility = Visibility.Visible;
                }
                if (i > 0)
                {
                    Window1 w2 = new Window1();
                    w2.Show();
                    Users.CurrentUserName = "UserName";
                    this.Login.Close();
                                    
                }
                else
                    connectionstatus.Content = "Password or username is invalid";
                    ErrorBoxFade_BeginStoryboard.Storyboard.Begin();
                    ErrorBox.Visibility = Visibility.Visible;
                
    
                
           }
    And here is the code for the LABEL when its loaded

            private void lblUserName_Loaded(object sender, RoutedEventArgs e)
            {
                lblUserName.Content = Users.CurrentUserName;
            }

    Wednesday, August 19, 2009 8:20 AM
  • Label's event OnLoad occurs before you even have a chance to click a mouse button.
    Put label's initialization exact after Users.CurrentUserName = "UserName";
    Also mention, that you are assigning a constant value to your propertie
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 8:46 AM
  • Whit this code Users.CurrentUserName = "UserName"; it sets the value in the label to "UserName" i want it to get the username from the database.

    Anyway, i cant set the label's initialization exact after Users.CurrentUserName = "UserName"; because the OnLoad for the label is in another window.

    The CLASS and the MySQL connection is in LoginWindow

    And the LABEL is in Window1 
    Wednesday, August 19, 2009 10:24 AM
  • Change
                    Window1 w2 = new Window1();
                    w2.Show();
                    Users.CurrentUserName = "UserName";
     to:

                    Users.CurrentUserName = "UserName";               
                    Window1 w2 = new Window1();
                    w2.Show();
                   
    Also, I suppose, you need to change

    Users.CurrentUserName = "UserName"

    to be

    Users.CurrentUserName = username.Text

    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 10:56 AM
  • Now it works. But by doing this iam not getting the information from the database it just getting the valu of what you put in the username.text field.
    Wednesday, August 19, 2009 11:01 AM
  • Your existing request to the database gets only existance of user account. You should write another query to get user's name from the DB and then set its value to Users.CurrentUserName
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 11:09 AM
  • Have any example how this chould look
    Wednesday, August 19, 2009 11:14 AM
  •                
                    conn.Open();
    
                    myCommand.Connection = conn;
                    myCommand.CommandText = "SELECT [field_for_username] FROM users WHERE " /*Condition*/;
    
                    Users.CurrentUserName = myCommand.ExecuteScalar().ToString();
                    
                    conn.Close();


    Write the query by yourself, because only you may know DB schema.
    • Proposed as answer by Oleksandr Klymenko Thursday, August 20, 2009 10:35 AM
    • Marked as answer by Harry Zhu Tuesday, August 25, 2009 3:48 AM
    Wednesday, August 19, 2009 12:03 PM
  • should i put this query, in the login part?
    Thursday, August 20, 2009 5:20 PM
  • you may want to do it exactly after you check existance or user in DB

    Thursday, August 20, 2009 5:25 PM
  • I did solve it.

    How do i do if i want to insert two values in one label seperated by a ",".

    Tabel Users and Tabel company is connected. So i want it to get the companyname from the company tabel depending on the user.
    i know how the query gonna look like but dont know how we do this in C#.

    if i do like this.

                    conn.Open();

                    myCommand.Connection = conn;
                    myCommand.CommandText = "SELECT firstname,lastname from users WHERE account='"+username.Text+"'";
                    
                    Users.CurrentUserName = myCommand.ExecuteScalar().ToString();

                    conn.Close();

    its only selecting the firstname and not the last name.
    • Edited by Alcstudio Thursday, August 20, 2009 5:56 PM
    Thursday, August 20, 2009 5:30 PM
  • >insert two values in one label seperated by a ",".

    Use this:

    myCommand.CommandText = "SELECT firstname+', '+lastname from users WHERE account='"+username.Text+"'";
    Thursday, August 20, 2009 6:21 PM
  • Now its only a zero instead. if i insert some more "," in the query it will get the firstname but not the last name.


    Thursday, August 20, 2009 8:55 PM
  • Some more help? =)
    Saturday, August 22, 2009 11:46 AM
  • Hi,

    You created a new thread for this question :
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/52402c89-4570-45af-aea4-6b12bd2dc585

    I'm going to mark this thread for it's relating to other questions.

    Please create new threads for new questions.

    Thanks,
    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, August 25, 2009 3:48 AM