Create a new profile for User fails
Hi,
I'm having troubles creating a new profile for an User through code.
I created a new profile inside the Sitefinity backend and now I want to
import some users from a .csv file. At the point of creating a new
profile record for the user, I get an error while saving the profile.
This is the error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.Description:An unhandled exception occurred during the execution of the current webrequest. Please review the stack trace for more information about theerror and where it originated in the code.ExceptionDetails: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow.Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.Source Error:Line 160:Line 161: profileManager.RecompileItemUrls(sfProfile);Line 162: profileManager.SaveChanges();Line 163: userManager.SaveChanges();Line 164: Source File: d:\Projects\LightForTheWorld\WebCms\GenerateCodes.aspx.cs Line: 162Stack Trace:[SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.] Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute(Nullable`1 commandTimeout) +845 OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute(Nullable`1 commandTimeout) +151 OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOIDoid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[]fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes,BatchControlInfo batchControl) +2865[DataStoreException:Insert of '350041409-6ec6aa67-0b24-4c1c-8813-5ac313ef438c' failed:System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must bebetween 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute(Nullable`1 commandTimeout) at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute(Nullable`1 commandTimeout) atOpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOIDoid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[]fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes,BatchControlInfo batchControl)INSERTINTO [donors] ([id], [address], [allow_contact_by_phone],[bank_account_number], [birthday], [city], [donor_number], [gender],[phone_number], [postal_code], [receive_digital_newsletter],[receive_mailings], [unique_code]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?)(setevent logging to all to see parameter values)System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must bebetween 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute(Nullable`1 commandTimeout) at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute(Nullable`1 commandTimeout) atOpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOIDoid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[]fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes,BatchControlInfo batchControl)] DynamicModule.ns.Wrapped_OpenAccessProfileProvider_c913517ed523464e810c1706130a18ff.CommitTransaction() +342 Telerik.Sitefinity.Data.ManagerBase`1.SaveChanges(String eventOrigin) +274 SitefinityWebApp.GenerateCodes.importDonors_OnClick(Objectsender, EventArgs e) ind:\Projects\LightForTheWorld\WebCms\GenerateCodes.aspx.cs:162 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804 And this is the code I have:
foreach (var profile in profiles) // Create a temporary password var password = Membership.GeneratePassword(8, 0); // Create a temporary username if email is not known var username = (profile.EmailAddress.IsNullOrEmpty()) ? profile.DonorId : profile.EmailAddress; // Create a new User with a Profile MembershipCreateStatus status; // Create the user var user = userManager.CreateUser(username, password, profile.EmailAddress, "question", "answer", true, null, out status); userManager.SaveChanges(); if (status != MembershipCreateStatus.Success) continue; var sfProfile = profileManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile; if (sfProfile != null) sfProfile.FirstName = profile.Firstname; sfProfile.LastName = profile.Lastname; sfProfile.DateCreated = DateTime.Now; const string donorProfileType = "Telerik.Sitefinity.Security.Model.donors"; if (UserProfilesHelper.ProfileTypeExists(donorProfileType)) var donorProfileName = UserProfilesHelper.GetUserProfileTypeNames().SingleOrDefault(u => u.Contains("donors")); var donorProfile = profileManager.CreateProfile(user, Guid.NewGuid(), donorProfileName); donorProfile.SetValue("Birthday", profile.Birthday.GetValueOrDefault()); donorProfile.SetValue("BankAccountNumber", profile.BankAccountNumber); donorProfile.SetValue("PhoneNumber", profile.PhoneNumber); donorProfile.SetValue("Address", profile.Address); donorProfile.SetValue("PostalCode", profile.PostalCode); donorProfile.SetValue("City", profile.City); donorProfile.SetValue("ReceiveDigitalNewsletter", profile.ReceiveDigitalNewsletter); donorProfile.SetValue("AllowContactByPhone", profile.AllowContactByPhone); donorProfile.SetValue("ReceiveMailings", profile.ReceiveMailings); donorProfile.SetValue("UniqueCode", GenerateUniqueCode(user.Id)); donorProfile.DateCreated = DateTime.Now; donorProfile.LastModified = DateTime.Now; donorProfile.PublicationDate = DateTime.Now; donorProfile.ExpirationDate = DateTime.Now; profileManager.RecompileItemUrls(sfProfile); profileManager.SaveChanges(); userManager.SaveChanges(); I can't figure out which DateTime field is causing the troubles. I can't
find examples on how to to this task through code either in the
documentation.
What can I do about this?
Best,
Daniel
Hello,
Try resolving the custom profile type of the user as below and use SetValue for setting also the date.
UserManager userManager = UserManager.GetManager(); UserProfileManager profileManager = UserProfileManager.GetManager(); var myType = "Telerik.Sitefinity.Security.Model.CustomProfile"; if (UserProfilesHelper.ProfileTypeExists(myType)) //resolve the type var profType = TypeResolutionService.ResolveType(myType); var profName = UserProfilesHelper.GetUserProfileTypeNames().Where(u => u.Contains("Telerik.Sitefinity.Security.Model.CustomProfile")).SingleOrDefault(); var user = userManager.GetUsers().Where(u => u.UserName == "admin3").SingleOrDefault(); //Create the profile, it will not throw exception var userProfile = profileManager.CreateProfile(user, profName); //set value to the custom profile type field userProfile.SetValue("FieldName", "NewContents"); profileManager.SaveChanges(); userManager.SaveChanges();