hi, i'm using forms based authentication within my aspnet (c#) website. At the log in page i'm capturing username, password and email. the email addressed is stored within the membership table. now on a new page, i'd just like to display the users' email address so they update it as well as another boolean field. i've tried using a gridview but i can't figure out how to modify the query so it only reveals the current logged in user. thanks!
A:
Hi,
Since you're using Membership controls, you could to this.
string userEmail = Membership.GetUser().Email;
If you don't pass a parameter to GetUser()
, it will find the details of the currently logged in user.
keyboardP
2010-09-30 15:14:57
ok thanks, so i placed that code in my code behind file. then to call the data in my aspx page, i just place `<form id="form1" runat="server">Welcome <b><%=userEmail%></b>. </form>` correct?
PW2
2010-09-30 15:59:17
Yes, that should work. You might want to look into caching the email string instead of polling the database on every call. A few methods http://www.4guysfromrolla.com/articles/022802-1.aspx
keyboardP
2010-09-30 16:09:26
i'm getting the error, "useremail" does not exist in current context.
PW2
2010-09-30 16:34:11
Did you copy and paste my code above? The code is case-sensitive, so it should be `userEmail`. If that's not it, then make sure you rebuild your project before running it.
keyboardP
2010-09-30 16:41:48
i could not get this to work. sorry, i'm a newbie :/
PW2
2010-09-30 22:39:34
Add a label control, which you can then set from the codebehind using myLabel.Text = userEmail. For an example, check the Email property link I gave in the answer. Look at how they've coded it.
keyboardP
2010-09-30 23:01:57
hi, i got the code running from the "msdn email property" is there a way to set that code to display on the current user?
PW2
2010-10-01 10:22:46
hey, thanks a lot for yout help. i'm posting another question to expand on my question.
PW2
2010-10-01 10:54:50