Question Store user login data in to a Class and reuse within the application

andrewmanuja

Well-known member
Joined
May 30, 2019
Messages
75
Programming Experience
Beginner
Hi All,

I got a small application and need to store the user's login details (ex - user name) into a static variable.

I created a class named, "UserData" and it looks like below;

public class UserData
{
public static string userName;
}

I need to access the "userName" variable from other window forms.

Using the below command to check the variable value of the "UserName", gave a blank answer.

MessageBox.Show(UserData.userName);

I am not so sure about the reason for the said issue, however, I am checking for a valid username and password combination and need to get the value for the "userName" variable upon the database validation.

Assume the question is clear and appreciate a lot if you could help me to resolve this issue.

Thank you.

Kind regards,

Andrew
 
Either you're not setting that field or you're setting it after you try to get it. Place breakpoints on all the places it is used and see if and when they are hit.

By the way, if you were to use a property rather than a field, you could just put breakpoints on the getter and setter and check the call stack to see where it came from.
 
Hi,

You are correct. The issue was not not setting the field before accessing it.

Thank you very much for your guidance.

Kind Regards,

Andrew
 
Hi,

Thanks for the above respond.

Now, my requirement is to retrieve a single string value from the database based on the variable value I collected at the user login point.

Basically, I want to get the "costCentreCode" which relates to individual user and pass it to a textbox.

My class looks as below;
C#:
public class UserData
{
    public static string userName;
    public static string costCentreCode;
}

I have written a function to get the "costCentreCode" as below;
C#:
private void CallCostCentre()
{           
    SqlCommand cmd = new SqlCommand("SELECT costCentreCode FROM tbl_UserData WHERE userName=  UserData.userName  ", con);

    con.Open();
    
    SqlDataReader dr = cmd.ExecuteReader();
    string costCentreCode = null;
    
    if (dr.Read())
    {
        UserData.costCentreCode = (string)dr[0];
    }
    
    dr.Close();
    con.Close();         
}

Note
con refers to connection string

I need two favors;
1. Whether the function I have written on the respective window form is correct?
2. How to call the "costCentreCode" value using the function elsewhere of the program/ or at least on the same window form?

Thank you very much in advance for your favorable comments.

Look forward to hearing from you.

Kind regards,

Andrew
 
Last edited by a moderator:
Take a look at this code:
C#:
var myName = "andrewmanuja";

Console.WriteLine("Hello myName");
What would you expect the result of that code to be? If you run that code, do you see that result? Now ask yourself whether this code is actually doing what you want:
C#:
SqlCommand cmd = new SqlCommand("SELECT costCentreCode FROM tbl_UserData WHERE userName=  UserData.userName  ", con);
 
Take a look at this code:
C#:
var myName = "andrewmanuja";

Console.WriteLine("Hello myName");
What would you expect the result of that code to be? If you run that code, do you see that result? Now ask yourself whether this code is actually doing what you want:
C#:
SqlCommand cmd = new SqlCommand("SELECT costCentreCode FROM tbl_UserData WHERE userName=  UserData.userName  ", con);

Thanks for the reply.

Yes, need to use the ReadLine to get the output.

Also the command should be
Console.WriteLine("Hello" +myName);


Thank you.

Regards,

Andrew
 
Last edited:
Back
Top Bottom