Create roles programmatically for aspnetdb - role already exists error

DB1949
DB1949 used Ask the Experts™
on
I have a windows application and a web site app that will use the same database so I combined the aspnetdb.mdf with the data db. The windows app creates the db(for deployment or in case it gets corrupted and needs to be deleted and replaced) so there is no web.config file initially.  The db is created in the App_Data directory, used aspnet_regsql.exe to add aspnetdb functions and creates users with Membership.CreateUser and this all works fine.  

The problem is I want to add 2 roles to the database - user and admin.  When I use Roles.CreateRole I get a "role already exists" error, but the roles don't show up in the database.
If I change the role name it will create it the first time but then the second time I get the "already exists" error.  But neither time does it get added to the database I want, but obviously it's adding it somewhere else.  

Any ideas for a newbie?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
1 . First verify that which database currently used by membership provider ? As it generating error that role already exists it means it has something added in database.


2.  Roles.RoleExists("rolename")  to check that role is already exists or not.

3  IF both application sharing data (like commong users ) then  both should have application name same in membership and role provider setting.


Top Expert 2013

Commented:
The roles will be added to the database that is used by the role provider ..i.e.
Check the role provider ConnectionStringName attribute...whatever DB that connectionString is pointing to....roles are added to that DB

Author

Commented:
I don't explicitly create a role provider and there is no web.config file to set up roles because this is just a form app initially-I may be making a mistake here.  

I should be pointing to the correct database because I can add tables with no problem.  Then I can create users using the code below.  But when I try to add the roles I get an error, and for some reason it is no longer pointing to the correct db.


'add membership and role functions to db
Shell("C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S DESKTOP\SQLEXPRESS -U pcuser -P pcpassword -d testdb -A mr", , True, 50000)
 
'these 2 lines ok
Membership.CreateUser("user1", "password+")
Membership.CreateUser("admin1", "password+")
 
'these create error - role exists somewhere but not in the database I want
Roles.CreateRole("user")
Roles.CreateRole("admin")
Roles.AddUserToRole("user1", "user")
Roles.AddUserToRole("admin1", "admin")

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
<membership> has connectionString parameter in it. That connectionString should contain testdb name. by default it is point to another database called aspnetdb so..


Author

Commented:
But MemberShip.CreateUser puts the user1 and admin1 in testdb correctly.
I don't see how to access membership connectionstring parameter.  I've been using vb for many years but I'm new to asp.net and databases and don't have a solid background in it.
Top Expert 2013
Commented:
Right now the membership and roles settings are taken default from machine.config.
They look like below:
----Membership provider-----
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>
-----Role Provider------
<roleManager>
<providers>
<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>

Note in the providers' settings...-connectionStringName="LocalSqlServer"
----Default in LocalSqlServer machine.config----
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

So it creates an aspnetdb database under app_data folder...but you are using membership/roles tables from testdb Database....for these providers to use your testDB add this following to your app.config ---Configuration section...

<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="data source=DESKTOP\SQLEXPRESS;Integrated Security=True;Initial Catalog =testdb;"
providerName="System.Data.SqlClient"/>
</connectionStrings>

<system.web>
<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear />
<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
</system.web>

Note...Adjust LocalSqlServer accordingly to your testdb....I just estimated it from the info you provided earlier..
Check this as well:
http://www.theproblemsolver.nl/usingthemembershipproviderinwinforms.htm

Author

Commented:
Thanks guru_sami.  I had the  info in the  section of the app.config, but not in the roleManager section.  That's why it worked with CreateUser but not CreateRole.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial