Link to home
Start Free TrialLog in
Avatar of bidgadget
bidgadgetFlag for United States of America

asked on

ASP.NET 4 Visual Studio 2010. Membership Website Question

I have a asp.net 4 membership web with roles.  When a user goes to change his password, I want to display that particular users Password Questiion.  I added a dataset form view to the membership database and am able to achieve this by a sql query when I specify the users actual name in the query.  I want to change my code to query the databased based on the user that is logged in.

Basically I want to change the part in my code that says
WHERE (aspnet_Users.UserName = 'RICK')
 to  something like

Where aspnet_users.username = (the currently logged in user)

I am not sure what to replace "the logged in user" with to make it equal the currently logged in user

any help would be appreciated.
SELECT aspnet_Membership.PasswordQuestion, aspnet_Membership.PasswordAnswer FROM aspnet_Membership INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = 'RICK')

Open in new window

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

The following should give you that:
Membership.GetUser().Username

Open in new window

Avatar of bidgadget

ASKER

Thank you for your response.  But how would I add that to my sql query?
Are you using inline SQL or calling a stored procedure? If it is inline you would do something like (assuming C#):
string username = Membership.GetUser().UserName;
string sql = string.Format("SELECT aspnet_Membership.PasswordQuestion, aspnet_Membership.PasswordAnswer FROM aspnet_Membership INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = '{0}')
", username);

Open in new window

I am using inline sql and with VB
In that case:
Dim username As String = Membership.GetUser().UserName
Dim sql As String = String.Format("SELECT aspnet_Membership.PasswordQuestion, aspnet_Membership.PasswordAnswer FROM aspnet_Membership INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = '{0}')
", username)

Open in new window

Ok, I will give it a try.  Do I just paste the code into in the "define custum sql or stored procedure"?
Depends how you are trying to get at the data.
I added a list view, then made a connection string to the membership database.  Then I am at the part where I am creating the custom sql query to return the question.  Thank you so much
I am not sure if I add the sql statement in the vb or sql part
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.  When I put the first code in, it takes me to another screen that says it recognizes I have a parameter "username"  It is asking me for a parameter source, a query string fiels and a default value
Ah, it's a little smarter than I gave it credit for :)   Leave the source set to "None" and the default value empty, unless you want to give it a default.
Still having a little trouble, page coming up blank, but i will play around with it.  Most likely I am not implementng correctly.  Thaks again for all your help
thanks again