Create a new profile for User fails

Posted by Community Admin on 04-Aug-2018 15:12

Create a new profile for User fails

All Replies

Posted by Community Admin on 23-Mar-2015 00:00

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 web
request. Please review the stack trace for more information about the
error and where it originated in the code.
 
Exception
Details: 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: 162
 
Stack 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(NewObjectOID
oid, 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 be
between 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)
   at
 
OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID
oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[]
fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes,
BatchControlInfo batchControl)
INSERT
INTO [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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?)
(set
event logging to all to see parameter values)
System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be
between 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)
   at
 
OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID
oid, 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(Object
sender, EventArgs e) in
d:\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

Posted by Community Admin on 26-Mar-2015 00:00

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();
   
               



Regards,
Stanislav Velikov
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed