1

I'm a database noob so please bear with me. I'm trying to join three tables together in the SelectCommand attribute of a SqlDataSource. Here's my select command at the moment:

SelectCommand="SELECT DISTINCT a1.[CreateDate], a2.[UserName], c1.[name], c1.[organisation], c1.[address], a1.[IsApproved], a1.[UserId] FROM [aspnet_Membership], [aspnet_Users] a2, [clients] c1 INNER JOIN [aspnet_Membership] a1 ON c1.userid = a1.UserId INNER JOIN [clients] c2 ON c2.userid = a2.UserId AND a1.IsApproved = 'False'"

It's kinda mucky because I've been playing with it. I'm trying to display the registered date from aspnet_Membership (CreateDate), username from aspnet_Users (UserName) and other details from my custom clients table. But my joins aren't working. I managed to get aspnet_Membership to join fine with clients to display CreateDate, but I can't join aspnet_Users to show the username. I keep getting errors saying "aspnet_Users.UserId could not be bound".

Any ideas? I'm pretty sure the problem is in the joining, but not matter how many tweaks I make to the command I can't get any results.

Any help would be much appreciated :)

melat0nin
  • 860
  • 1
  • 16
  • 37

2 Answers2

2

Why are you using clients twice? (Maybe explain what you're trying to achieve)

Try this:

SELECT DISTINCT 
    am.[CreateDate], ru.[UserName], cl.[name], cl.[organisation], 
    cl.[address], am.[IsApproved], am.[UserId] 
FROM 
    [aspnet_Membership] am
    INNER JOIN clients cl
        ON am.userid = cl.userid
    INNER JOIN [RolesAndAllUsers] ru
        ON cl.userid = ri.userid
WHERE
    cl.IsApproved = 'False'
smirkingman
  • 6,167
  • 4
  • 34
  • 47
  • That worked after some tweaking, thanks! RolesAndAllUsers should have been aspnet_Users (I got the former from some other example) but after changing that it works great :) – melat0nin Nov 22 '10 at 13:13
0

This is what you have posted. I suspect the cross joins are where your problem lies.

SELECT DISTINCT a1.[CreateDate], a2.[UserName], c1.[name], c1.[organisation], 
    c1.[address], a1.[IsApproved], a1.[UserId] 
FROM [aspnet_Membership]
CROSS JOIN [RolesAndAllUsers] a2
CROSS JOIN [clients] c1 
INNER JOIN [aspnet_Membership] a1 ON c1.userid = a1.UserId 
INNER JOIN [clients] c2 ON c2.userid = a2.UserId 
WHERE a1.IsApproved = 'False'
Anthony Faull
  • 17,549
  • 5
  • 55
  • 73