Answered by:
Using two databases

Question
-
User-1573875253 posted
Hi,
I am hoping someone could offer some advice.
I have two databases; database A which handles all membership (users, roles etc) requests and database B which holds data in relation to products.
I want to create a related record in database B once a user has been created in database A using the standard asp.net create user wizard i.e. a relation between DatabaseA.aspnet_Users.UserId and DatabaseB.Users.UserId
Would the best way be to use some kind of override once the create user wizard has finished?
Also once a user has logged in I need to be able pull record back from database B so would probably need to override the LoggedIn event?
Any help, tips on good practice or examples would be appreciated.
Regards
K
Monday, March 29, 2010 12:03 PM
Answers
-
User-925904572 posted
Thanks dzieba that helps. Now that I have control of the CreatedUser step I can add to a role as per your example.
Next step, I want users to be assigned to an account when they are created. I could therefore add a record into a table after I have assigned them a role. i.e.
User creates account, user added to role, user id inserted into DatabaseB.UserAccounts (fields: userId, accountId) table, once logged in users see records in relation to their account.
Does that make sense? would you have a second table which holds the userid, accountid as above?
Thanks again
This depends on your needs. In my app, I insert a record into the UserOptions table (see my code example) because my application needs that. But if your need is to save information for reference in another DB or account than you can do that. Nothing wrong with it.
Like you can see in my example it's only two lines of code. Setting the paramerter (UserID) and calling Insert on the datasource. You just need a SqlDataSource object in your ASPX file. Fill in the InsertCommand property and add the userID parameter and you are done.
good luck.. don't forget to mark Answer...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 30, 2010 11:10 AM
All replies
-
User-925904572 posted
Here is the event to use when the user is created.
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser Dim w As CreateUserWizard = CType(sender, CreateUserWizard) Dim usr As MembershipUser = Membership.GetUser(w.UserName) If Not usr Is Nothing Then Roles.AddUserToRole(usr.UserName, "Member") SqlDataSource1.InsertParameters("userID").DefaultValue = usr.ProviderUserKey().ToString() SqlDataSource1.Insert() End If CreateUserWizard1.Visible = True End Sub
The wizard adds it to the Membership data.In this function, I inserts the user into the Member Role.
SqlDataSource1 inserts a record into my website data, into the UserOptions table so they can have options on their profile page.
This is where your datasource would insert into your other database.
Let me know if this helped.
Monday, March 29, 2010 3:59 PM -
User-925904572 posted
you are correct about the second half of your question. here is mine. not much.
Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn Response.Redirect("~/MyXGames.aspx") End Sub
Monday, March 29, 2010 4:05 PM -
User-1573875253 posted
Thanks dzieba that helps. Now that I have control of the CreatedUser step I can add to a role as per your example.
Next step, I want users to be assigned to an account when they are created. I could therefore add a record into a table after I have assigned them a role. i.e.
User creates account, user added to role, user id inserted into DatabaseB.UserAccounts (fields: userId, accountId) table, once logged in users see records in relation to their account.
Does that make sense? would you have a second table which holds the userid, accountid as above?
Thanks again
Tuesday, March 30, 2010 5:10 AM -
User-925904572 posted
Thanks dzieba that helps. Now that I have control of the CreatedUser step I can add to a role as per your example.
Next step, I want users to be assigned to an account when they are created. I could therefore add a record into a table after I have assigned them a role. i.e.
User creates account, user added to role, user id inserted into DatabaseB.UserAccounts (fields: userId, accountId) table, once logged in users see records in relation to their account.
Does that make sense? would you have a second table which holds the userid, accountid as above?
Thanks again
This depends on your needs. In my app, I insert a record into the UserOptions table (see my code example) because my application needs that. But if your need is to save information for reference in another DB or account than you can do that. Nothing wrong with it.
Like you can see in my example it's only two lines of code. Setting the paramerter (UserID) and calling Insert on the datasource. You just need a SqlDataSource object in your ASPX file. Fill in the InsertCommand property and add the userID parameter and you are done.
good luck.. don't forget to mark Answer...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 30, 2010 11:10 AM -
User-1573875253 posted
Thanks
Wednesday, March 31, 2010 3:25 AM