Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 2nd, 2007, 09:46 AM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default Help with ASP.NET Membership

Hi everyone!
I'm having trouble with my SQL query.
The database scheme is http://img237.imageshack.us/img237/2484/schemahx1.gif.

The query I am trying to fix is
Code:
SELECT m.email
FROM aspnet_users u
INNER JOIN aspnet_usersInRoles uir ON u.userId = uir.userID
RIGHT OUTER JOIN aspnet_membership m ON u.userID = m.userID
INNER JOIN aspnet_custom_registrants cr ON m.userID = cr.userID
LEFT OUTER JOIN aspnet_roles r ON r.roleID = uir.roleID
WHERE cr.receiveNewsletter = 1
/* if I add this line of query, I get no results at all
AND r.roleName = 'user' */
I believe you all noticed what I'm trying to do. I need a list of emails of users in a specific role who are subscribed for newsletter.

Thank you in advance!
Aleksandar

__________________
Aleksandar Dragosavac
Belgrade, Serbia
  #2 (permalink)  
Old May 2nd, 2007, 09:56 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Try this instead:

SELECT m.email
FROM aspnet_users u
INNER JOIN aspnet_usersInRoles uir ON u.userId = uir.userID
RIGHT OUTER JOIN aspnet_membership m ON u.userID = m.userID
INNER JOIN aspnet_custom_registrants cr ON m.userID = cr.userID
LEFT OUTER JOIN aspnet_roles r ON r.roleID = uir.roleID AND r.roleName = 'user'
WHERE cr.receiveNewsletter = 1


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
  #3 (permalink)  
Old May 2nd, 2007, 10:04 AM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

That didn't help.
Why did you put "AND r.roleName = 'user'" near the "LEFT OUTER JOIN aspnet_roles r ON r.roleID = uir.roleID"?
Shouldn't this be a simple INNER JOIN query?

  #4 (permalink)  
Old May 2nd, 2007, 10:20 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I have had problems with querys where I place a field from a joined table in my WHERE clause and fixed the problem by making it one of my join parameters, was not sure if you were having the same problem.

In any case if the statement returns a result set without: AND r.roleName = 'user' then i am going to bet that the column does not contain just 'user'

What happens if you query that table with a simple select:

SELECT * from aspnet_roles where roleName = 'user'

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
  #5 (permalink)  
Old May 2nd, 2007, 11:14 AM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

Code:
SELECT * from aspnet_roles where roleName = 'user'
returns expected results of 1 row.
I don't know if you have some experience with asp.net membership tables, but here's how it works: when you create a membership user, the aspnet_users table contains 2 rows for each user. One record is related to the record from the aspnet_membership table, and the other one is related to aspnet_usersInRoles.
I suppose this is where the problem begins.

  #6 (permalink)  
Old May 2nd, 2007, 01:16 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:Originally posted by VerbatimBOT
but here's how it works: when you create a membership user, the aspnet_users table contains 2 rows for each user. One record is related to the record from the aspnet_membership table, and the other one is related to aspnet_usersInRoles.
I hate to spoil the fun, but this is *not* how it works.

When you create a user and add it to a role, you get the following records:

aspnet_Users
1 record that contains the base user data used by all providers. User ID for example: 27fea12d-a12d-4463-a3f6-d0491aca1316

aspnet_Membership
1 record, with all the Membership data for a user.
User ID is the same as in the _Users table as it is in fact a reference: 27fea12d-a12d-4463-a3f6-d0491aca1316

aspnet_UsersInRoles
1 record: a link between a RoleID (for example 43861e10-4666-474d-ab11-700c95e536bf) and the User's ID from the aspnet_Users table (27fea12d-a12d-4463-a3f6-d0491aca1316)

If you're seeing something else, something is seriously wrong.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #7 (permalink)  
Old May 2nd, 2007, 01:58 PM
Authorized User
 
Join Date: Sep 2006
Location: Belgrade, Serbia, Yugoslavia.
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

Yes, I just found out that my membership and role provider definitions were wrong. I made a small roleProvider correction in web.config and everything works perfect now.
The only strange thing is that when I started implementing custom membership and role providers, I've noticed from the start that I always get 2 records in my aspnet_users table. Normally, I asked around about the issue, and never got the answer about it. So, I started to believe that's normal! :)
I wanna thank YOU and dparsons for helping me.

Cheers! :)



Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET Membership and ActiveDirectory Quick209 ASP.NET 2.0 Professional 0 June 13th, 2007 12:41 PM
ASP.NET SQL Membership Provider bignermo ASP.NET 2.0 Professional 2 March 20th, 2007 05:41 AM
Roles and membership asp.net 2 zeeshannasir ASP.NET 1.x and 2.0 Application Design 1 September 22nd, 2006 03:34 AM
Roles and membership asp.net 2 zeeshannasir ASP.NET 2.0 Basics 2 September 15th, 2006 07:24 AM
asp.net 2 custom membership provider andyw ASP.NET 2.0 Professional 24 September 6th, 2006 08:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.