Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > Visual Web Developer 2008
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Visual Web Developer 2008 Discuss creating ASP.NET 3.5 sites with Microsoft's Visual Web Developer 2008. If your question is more specific to a piece of code than the Visual tool, see the ASP.NET 3.5 forums instead. If your question is specific to the "Express Edition" be sure to state that in your post.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Web Developer 2008 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 25th, 2009, 10:20 AM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
Default Displaying data based on user logged in

First of all - I hope I have posted this in the correct place - if not; please bear with me as I am new to using forums. Thanks.

I am also relatively new to ASP.NET and am experiencing a steep learning curve as I work on a current assignment.

The biggest problem I am having at the moment is with user profiles / log-ins. I am using Visual Web Developer 2008 Express and have been able to get a basic login function to work using the wizards provided in the software. So I can create a new user and then log in as this user. I also have it displaying the name of the logged in user on the page when login has been successful.

I know this information is being stored in the ASPNETDB.mdf, which Visual Web Developer creates for you.

I have my own sql database which will store personal information, (address etc) and rather than try to merge the two I eventually want to link ASPNETDB.mdf with my own, based possibly on the unique user code it provides. However this is not what I am having issue with at the moment (although if you have advice on best practice I am keen to hear it!)

I want to begin to expand the site to show only information relevant to the logged in user - for example initially to use information only from ASPNETDB.mdf, so to display the user code for the logged in user.

To try and achieve this I have followed through creating a datagrid and linking it to a datasource. I realise I must somehow need to specify that the data displayed is only that of the logged in user. I follow through configuring a connection string, and including a Where clause, to try and pick up the logged in user, but this is where it falls apart on me! I don't know where the source I am picking the user name up from needs to be - for example from a session, cookie, form, profile.

I can get it to work in the test; (when I use the 'profile' value as 'username') but this is because I am typing in the user name - I need it to recognise the name of the logged in user as opposed to have them type it each time.

I realise this is a bit garbled but if you can understand what I am trying to do and could offer any help / suggestions, that would be most appreciated. I feel once I get past this hurdle I will be able to make substancial progress, but I have been stumped at this for a while.
  #2 (permalink)  
Old January 25th, 2009, 10:35 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

If you're using the built-in ASP.NET Security mechanism, then the following code gives you the user name of the currently logged on user:

string userName = Membership.GetUser().UserName;

Alternatively, you can get the user's unique ID:

Guid userId = (Guid) Membership.GetUser().ProviderUserKey;

It's in C#, but the same principle holds for a VB site of course....

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Spider (January 26th, 2009)
  #3 (permalink)  
Old January 25th, 2009, 01:02 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by Spider View Post
I don't know where the source I am picking the user name up from needs to be - for example from a session, cookie, form, profile.

I can get it to work in the test; (when I use the 'profile' value as 'username') but this is because I am typing in the user name - I need it to recognise the name of the logged in user as opposed to have them type it each time.
Keep in mind that the current user identity is always stored in the current HttpContext. Therefore, from a page, you can always use this:

string userName = Context.User.Identity.Name;

If the current user is not logged in, this value will be an empty string.

I think this is a little easier than Imar's suggestion, as it doesn't require you to use the static methods of Membership, or create a MembershipUser. It also works if you are using Windows authentication instead of the ASP.NET Forms authentication database stuff.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #4 (permalink)  
Old January 26th, 2009, 06:25 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default Wheres the sql command?

I am having the same problem and I am able to display the username, my problem is how do I query against this value? I seem to have tried everything (UserName, ID, LoginName, etc) against session, cookie, form.......Nothing. Any help on why I cant capture this?
  #5 (permalink)  
Old January 26th, 2009, 10:02 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by mashype View Post
I am having the same problem and I am able to display the username, my problem is how do I query against this value? I seem to have tried everything (UserName, ID, LoginName, etc) against session, cookie, form.......Nothing. Any help on why I cant capture this?
It's not clear what you mean here. What are you attempting to query? Or, to put it another way, what results are you attempting to obtain?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #6 (permalink)  
Old January 26th, 2009, 10:35 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default Sorry

It was a bit late when I was posting. I have user data in a sql db tied to the current user that I wanted to display. I would like to be able to query that data to return the record only based on the current user logged in.
  #7 (permalink)  
Old January 26th, 2009, 11:05 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

I'm not sure where the problem is. Can't you just write a query with the user name as a parameter?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #8 (permalink)  
Old January 26th, 2009, 01:55 PM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Imar
Thanks for your swift response. I understand what you are saying with
[string userName = Membership.GetUser().UserName;]
but I am afraid I still don't know where to put this.

I can get a page to allow a user to log in. And I can display the name of the user using the [LoginName]. I can use a datagrid to display the names of all the people in my database, but I cannot make the link between the logged in user to display only the information relevant to the logged in user in the datagrid.

I am sorry if I am being really stupid about this (I suspect I am!) but I apreciate your patience with a novice!
  #9 (permalink)  
Old January 26th, 2009, 04:23 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

It all depends on where you get your data from and in what way.

If you're using a SqlDataSource you need to create a parameter for the user and fill it in its Selecting event. If you're using ADO.NET code, you can pass it as a parameter on a Command object. If you're using an ObjectDataSource, you can get the name from the context in your code directly. I could go on for a while with all the other data sources that exist.

So, show me the code, tell me what you're doing and how, and I can recommend something a bit more concrete....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Spider (January 27th, 2009)
  #10 (permalink)  
Old January 27th, 2009, 02:17 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default Heres my issue

Here is where I am storing the User:

<asp:LoginNameID="LoginName1"runat="server"CssClass="style14"/>

This lets me see the user logged in, but....

Here is what I am trying to query:

<asp:SqlDataSourceID="cookie"runat="server"
ConnectionString="<%$ ConnectionStrings:ProdConnectionString %>"SelectCommand="SELECT [UserName], [UserID], [LastName] FROM [TblUsers] WHERE ([UserName] LIKE '%' + @UserName + '%')">
<SelectParameters>
<asp:CookieParameterCookieName="'LoginName1'"Name="UserName"Type="String"/>
</SelectParameters>
</asp:SqlDataSource>

This query does not return any values......any suggestions?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
displaying data that is related to the login user saif44 ASP.NET 2.0 Basics 0 March 9th, 2006 12:21 PM
Displaying SQLServer data to user sss22 ASP.NET 1.0 and 1.1 Basics 1 October 28th, 2005 01:05 PM
Displaying sessions logged into Win2K Server sachin1979 BOOK: Beginning ASP 3.0 0 September 5th, 2004 08:04 PM
Filter Based Upon Logged In User LandOfToz BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 August 31st, 2004 12:24 PM
Getting the name of the logged on user Grahame2003 C# 2 March 4th, 2004 04:48 AM



All times are GMT -4. The time now is 07:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.