How do I retrieve a userID and display in the aspx page?

I am new to C# and the syntax is killing me. I need to retrieve the users network ID from the codebehind and display it on the web page.

The codebehind is as follows:

        protected string UserIDtxt;

        public string getUserID
        {
            UserIDtxt = Environment.UserName.ToString()
            return UserIDtxt;
        }
I get the error: A get or set accessor is expected.

The web page code is <%= getUserID%>

Any ideas?
lizziesboyAsked:
Who is Participating?
 
erik_nodlandConnect With a Mentor Commented:
Hi

The second version shouldn't have a UserIDtxt part to it. You should just use the following without the protected field.

public string getUserID
        {
            get { return Environment.UserName.ToString(); }
        }

thanks
0
 
erik_nodlandCommented:
I think it is because you are calling it as a property but you want to call it as a method.

So try

<%= getUserID()%>

thanks

0
 
erik_nodlandCommented:
Forgot to give you the other bit. Note the brackets!

protected string UserIDtxt;

 public string getUserID()
        {
            UserIDtxt = Environment.UserName.ToString()
            return UserIDtxt;
        }
 In your web page code

<%= getUserID()%>

If you do want it as a property then use this instead


protected string UserIDtxt;

 public string getUserID
        {
            UserIDtxt = Environment.UserName.ToString()
            return UserIDtxt;
        }
 In your web page code

<%= getUserID()%>

If you really do want to use it as a property then use this

public string getUserID
        {
            get { return Environment.UserName.ToString(); }
        }

Web page code

<%= getUserID%>

thanks


0
 
erik_nodlandCommented:
Not sure whats going on my text is all over the place. Here you go again!

protected string UserIDtxt;

 public string getUserID()
        {
            UserIDtxt = Environment.UserName.ToString()
            return UserIDtxt;
        }
 In your web page code

<%= getUserID()%>

If you really do want to use it as a property then use this

public string getUserID
        {
            get { return Environment.UserName.ToString(); }
        }

Web page code

<%= getUserID%>

thanks

0
 
lizziesboyAuthor Commented:
I cut and pasted your second version and I still get the undline under UserIDtxt that says a get or set expected.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.