Solved

ASP.NET 4 Visual Studio 2010.  Membership Website Question

Posted on 2011-03-09
14
339 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:bidgadget
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35084676
The following should give you that:
Membership.GetUser().Username

Open in new window

0
 

Author Comment

by:bidgadget
ID: 35084714
Thank you for your response.  But how would I add that to my sql query?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35084734
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

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:bidgadget
ID: 35084764
I am using inline sql and with VB
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35084782
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

0
 

Author Comment

by:bidgadget
ID: 35084863
Ok, I will give it a try.  Do I just paste the code into in the "define custum sql or stored procedure"?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35084935
Depends how you are trying to get at the data.
0
 

Author Comment

by:bidgadget
ID: 35084962
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
0
 

Author Comment

by:bidgadget
ID: 35085077
I am not sure if I add the sql statement in the vb or sql part
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35085141
Ah, ok, that sounds like you are using a SqlDataSource, which means you need to massage things a little to get it to work. In the dialog choose "Custom sql query...." then for the "select statement" paste your SQL statement as:
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 = @UserName

Open in new window

When the dialog has finished you should have a SqlDataSource component on your form. Switch to "Source View" and add a SelectParameters section to the SqlDataSource. The result should look like:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"  OnSelecting="SqlDataSource1_Selecting"
        SelectCommand="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 = @UserName">
        <SelectParameters>
            <asp:Parameter Name="UserName" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

Finally, switch to your code-behind and add the following code block to the page:
        Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs)
            SqlDataSource1.SelectParameters("UserName").DefaultValue = Membership.GetUser().UserName
        End Sub

Open in new window

0
 

Author Comment

by:bidgadget
ID: 35085336
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
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35085399
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.
0
 

Author Comment

by:bidgadget
ID: 35085839
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
0
 

Author Closing Comment

by:bidgadget
ID: 35085851
thanks again
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL - Load records to temp table through CTE 6 37
Why am I losing my session variable value 11 39
T-SQL Query 9 35
Datatable / Dates ? 4 30
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question