Click here to Skip to main content
12,886,913 members (31,349 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
i want show my current username inside the other user name every forms using label
Posted 31-Dec-12 21:22pm
Zoltán Zörgő 1-Jan-13 3:35am
And what is the question?
Windows Forms, Web Forms?
Current "username" - what user, Windows user running the application, or you have some authentication and authorization mechanism implemented?
This is not a question, this is yet another "I want...". If you want something, make it. If you have a problem, ask a question.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are a number of ways to do this:

1) You could have a property in each form which is set when it is created (or part of the constructor)
2) You could create a static method in your user class which returns the current logged in user name (or the current logged in user).
3) You could make your user class a singleton, which returns the one-and-only user instance and get the required information from there.

Probably, I would use the second or third method, depending on how the rest of your application works. If you have an Admin mode which accesses multiple users for example, you can't use a singleton pattern for your user.

The first method works well, but it requires you to make sure you pass the into through to each form, and can make it difficult if one form logs out the user - all the other forms may need to be notified.
prashant patil 4987 1-Jan-13 3:39am
Turbo_23 1-Jan-13 8:01am
Great, *****
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If It is a WinForm
then use MDI Container and show there in a control no need to show in each page.

If It is a WebForm
then use Master page and show there in a control no need to show in each page just inherit master template in your form.


create a form and write code on load event to add label control runtime and show username inside
and now, just inherit that form in all forms

Happy Coding!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

If WebForm ,

a) Inherit master page, in master page use following controls.

In master page
<asp:LoginName ID="LoginName1" runat="server" />
                <asp:LoginStatus ID="LoginStatus1" runat="server" />

b) If you are not inheriting master page then set user name in session & in each page get user name from session.

If Windows Application, Check solution posted by "OriginalGriff."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100