Solved

ASP.NET SQL Select a record based on Membership UserID

Posted on 2009-04-01
1
664 Views
Last Modified: 2013-11-26
I have the code shown below in an ASP.NET Web site.  I am trying to select a record from a table in an SQL database (Membership with added tables) where two fields match my selection criteria.  I can select on only the Fair_ID (an integer) but can not figure out how to set up the types to get the selection on the Master_UserId (SQL type uniqueidentifier - the same as Membership 'UserId'). I get casting errors.'
How do I set this up so that I can select the record(s) where the UserId is the same as the SQL Membership UserId?  
ASPX Page Code:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

    ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" 

    

        SelectCommand="SELECT * FROM [Fairs] WHERE (([Fair_ID] = @Fair_ID) AND   ([Master_UserId] = @Master_UserId))">

    <SelectParameters>

        <asp:Parameter Name="Fair_ID" Type="Int32" />

        <asp:Parameter Name="Master_UserId" Type="Object" />

    </SelectParameters>

 </asp:SqlDataSource>
 

VB Code

  Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting

        Dim myObject As MembershipUser

        myObject = Membership.GetUser()

        Dim userid As String  ' this just to test my code

        userid = myObject.ProviderUserKey.ToString()   

        TextBox1.Text = userid   ' This shows the correct user ID

        e.Command.Parameters("@Fair_ID").Value = 100  ' This line works

' next line fails in debug with error:

 ' Unable to cast object of type 'System.Guid' to type 'System.Data.Common.DbParameter'.

        e.Command.Parameters("@Master_UserId") = myObject.ProviderUserKey()

        ' the next line will not compile

        ' e.Command.Parameters("@Master_UserId") = myObject.ProviderUserKey.ToString()

    End Sub

Open in new window

0
Comment
Question by:newtovista
1 Comment
 

Accepted Solution

by:
newtovista earned 0 total points
ID: 24045915
http://www.asp.net/learn/security/tutorial-08-vb.aspx has the answer which is:
        ' Get a reference to the currently logged on user
        Dim currentUser As MembershipUser = Membership.GetUser()
        ' Determine the currently logged on user's UserId value
        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
        ' Assign the currently logged on user's UserId to the @Master_UserId parameter
        e.Command.Parameters("@Master_UserId").Value = currentUserId
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

947 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now