I am getting a foreign key constraint error. The error is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UserProfile_Users". The conflict occurred in database "DATABASENAME", table "dbo.Users", column 'UserID'. The statement has been terminated.
The user record gets created in the Users table, but the profile record is not added in the UserProfile table. Here is an example of the code I am using:
Public Function CreateNewUser(ByVal strUsername As String, ByVal strFirstName As String, ByVal strLastName As String, _
ByVal strEmailAddress As String) As DotNetNuke.Security.Membership.UserCreateStatus
' preliminary check to be sure that the fields are filled in
If String.IsNullOrEmpty(strUsername) Or String.IsNullOrEmpty(strFirstName) Or _
String.IsNullOrEmpty(strLastName) Or String.IsNullOrEmpty(strEmailAddress) Then
' cannot have empty values
If String.IsNullOrEmpty(strUsername) Then _
Throw New NullReferenceException("The USERNAME variable for CreateNewUser() cannot be empty.")
If String.IsNullOrEmpty(strFirstName) Then _
Throw New NullReferenceException("The FIRSTNAME variable for CreateNewUser() cannot be empty.")
If String.IsNullOrEmpty(strLastName) Then _
Throw New NullReferenceException("The LASTNAME variable for CreateNewUser() cannot be empty.")
If String.IsNullOrEmpty(strEmailAddress) Then _
Throw New NullReferenceException("The EMAILADDRESS variable for CreateNewUser() cannot be empty.")
Else
' validate the e-mail address format
If Not MyNameSpace.Common.Utilities.RegExLibrary.ValidateEmailFormat(strEmailAddress) Then _
Throw New Exception("The e-mail address was in an invalid format")
End If
' create a new user instance and populate it
Me.User = New DotNetNuke.Entities.Users.UserInfo
With Me.User
.Profile.InitialiseProfile(PortalSettings.PortalId)
.DisplayName = String.Concat(strFirstName, " ", strLastName)
.Email = strEmailAddress
.FirstName = strFirstName
.IsSuperUser = False
.Membership.CreatedDate = DateTime.Now
.Membership.Email = strEmailAddress
.Membership.Password = DotNetNuke.Entities.Users.UserController.GeneratePassword
.Membership.UpdatePassword = True
.Membership.Username = strUsername
.Username = strUsername
.Profile.FirstName = strFirstName
.Profile.LastName = strLastName
.Profile.PreferredLocale = Me.PortalSettings.DefaultLanguage
.Profile.TimeZone = Me.PortalSettings.TimeZoneOffset
End With
'Set the Approved status based on the Portal Settings
If Me.PortalSettings.UserRegistration = DotNetNuke.Common.Globals.PortalRegistrationType.PublicRegistration Then
Me.User.Membership.Approved = True
Else
Me.User.Membership.Approved = False
End If
' attempt to create the DNN user
Dim objStatus As UserCreateStatus = Users.UserController.CreateUser(Me.User)
Dim args As Modules.UserUserControlBase.UserCreatedEventArgs
If objStatus = UserCreateStatus.Success Then
args = New Modules.UserUserControlBase.UserCreatedEventArgs(Me.User)
args.Notify = True
Else ' registration error
args = New Modules.UserUserControlBase.UserCreatedEventArgs(Nothing)
args.Notify = False
End If
args.CreateStatus = objStatus
' perform some DNN logic
Me.UserCreateCompleted(args)
Select Case objStatus
Case UserCreateStatus.Success
' add to the BLL membership
Me.User = DotNetNuke.Entities.Users.UserController.GetUserByName(PortalSettings.PortalId, Me.User.Username, True)
HttpContext.Current.Session.Add("CreateNewUserID", Me.User.UserID)
If Me.User.UserID > 0 Then MyNameSpace.Events.UserLogin(Me.User)
Case Else
' something happened
End Select
Return objStatus
End Function
I thought about moving the Profile assignments lower, but I do not know how to save changes to the userinfo object yet (I haven't researched it yet, but it is probably somewhere in the ManageUsers.ascx control in the DNN source). What is causing this error and how do I get around it?