Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Rogers 139 posts 717 karma points
    Oct 17, 2018 @ 23:08
    Daniel Rogers
    0

    Creating my own database table

    I are trying to create my own data and attachit to the umbraco Member ID. Im on a development sytem under vs2012

    but the code falls over at db.Insert(MemberToAdd); in the surface controller with this error

    System.Data.SqlClient.SqlException: 'Cannot insert explicit value for identity column in table 'IBDMembership' when IDENTITY_INSERT is set to OFF.'

    Database Setup

    using System; using System.Collections.Generic; using System.Linq; using System.Web;

    //These are the main namespaces we need to use using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.DatabaseAnnotations;

    namespace IBD.IBDDatabase.pocos { [TableName("IBDMembership")] [PrimaryKey("IBDMembershipId", autoIncrement = true)] public class IBDMembership { [Column("id")] [PrimaryKeyColumn(AutoIncrement = true)] public int id { get; set; }

        [Column("MemberId")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public int? MemberId { get; set; }
    
        [Column("CompanyName")]
        public string CompanyName { get; set; }
    
        [Column("MembershipType")]
        public string MembershipType { get; set; }
    
        [Column("BuyLocalMember")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public bool? BuyLocalMember { get; set; }
    
        [Column("Position")]
        public string Position { get; set; }
    
        [Column("FName")]
        public string FName { get; set; }
    
        [Column("LName")]
        public string LName { get; set; }
    
        [Column("NumEmployees")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public int? NumEmployees { get; set; }
    
        [Column("Telephone")]
        public string Telephone { get; set; }
    
        [Column("CellPhone")]
        public string CellPhone { get; set; }
    
        [Column("Address1")]
        public string Address1 { get; set; }
    
        [Column("Address2")]
        public string Address2 { get; set; }
    
        [Column("Address3")]
        public string Address3 { get; set; }
    
        [Column("TownCity")]
        public string TownCity { get; set; }
    
        [Column("State")]
        public string State { get; set; }
    
        [Column("PostalCode")]
        public string PostalCode { get; set; }
    
        [Column("Country")]
        public string Country { get; set; }
    
        [Column("Facebook")]
        public string Facebook { get; set; }
    
        [Column("Website")]
        public string Website { get; set; }
    
        [Column("NewsletterSignup")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public bool? NewsletterSignup { get; set; }
    
        [Column("Active")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public bool? Active { get; set; }
    
        [Column("Approved")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public bool? Approved { get; set; }
    
        [Column("FirstJoinDate")]
        public DateTime FirstJoinDate { get; set; }
    
        [Column("LastUpdateDate")]
        public DateTime LastUpdateDate { get; set; }
    }
    

    }

    Database events

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using umbraco.cms.presentation; using Umbraco.Core; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; using Umbraco.Core.Persistence; using IBD.IBDDatabase.pocos;

    namespace IBD.IBDDatabase { public class IBDMembershipRegisterEvents : ApplicationEventHandler { //This happens everytime the Umbraco Application starts protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { //Get the Umbraco Database context var db = applicationContext.DatabaseContext.Database;

            //Check if the DB table does NOT exist
            if (!db.TableExist("IBDMembership"))
            {
                //Create DB table - and set overwrite to false
                db.CreateTable<IBDMembership>(false);
            }
    
            //Example of other events (such as before publish)
            Document.BeforePublish += Document_BeforePublish;
        }
    
        //Example Before Publish Event
        private void Document_BeforePublish(Document sender, PublishEventArgs e)
        {
            //Do what you need to do. In this case logging to the Umbraco log
            Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
    
            //cancel the publishing if you want.
            e.Cancel = true;
        }
    }
    

    }

    Model

    using System; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; using System.Collections.Generic;

    namespace IBD.IBDModels { public class IBDMembershipFormViewModel { [Required] public string CompanyName { get; set; }

        [Required]
        public string NumEmployees { get; set; }
        [Required]
        public string FirstName { get; set; }
    
        [Required]
        public string LastName { get; set; }
    
        [Required]
        public string Position { get; set; }
    
        public string Telephone { get; set; }
    
        public string CellPhone { get; set; }
    
        [Required]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
        public string Email { get; set; }
    
        [Required]
        public string Address1 { get; set; }
    
        public string Address2 { get; set; }
    
        public string Address3 { get; set; }
    
        [Required]
        public string TownCity { get; set; }
    
        [Required]
        public string PostalCode { get; set; }
    
        [Required]
        public string State { get; set; }
    
        [Required]
        public string Country { get; set; }
    
        public string MembershipType { get; set; }
    
        public List<SelectListItem> MembershipTypes { get; set; }
    
        public Boolean TownProudSignup { get; set; }
    
        public string Facebook { get; set; }
    
        public string Website { get; set; }
    
        [DataType(DataType.MultilineText)]
        public string Message { get; set; }
    
        public Boolean NewsletterSignup { get; set; }
    
        public int CurrentNodeID { get; set; }
    
        public int GlobalSettingsID { get; set; }
    
        // Following requiered for databace purposes
        public int IBDMembershipId { get; set; }
    
        public int MemberId { get; set; }
    
        public bool Active { get; set; }
    
        public bool Approved { get; set; }
    
        public DateTime FirstJoinDate { get; set; }
    
        public DateTime LastUpdateDate { get; set; }
    }
    
    public class MemberViewModel
    {
    
        [Required]
        public string Name { get; set; }
    
        [Required]
        [EmailAddress]
        public string Email { get; set; }
    
        [Required]
        public string Password { get; set; }
    }
    

    }

    Surface controller

    using MailChimp.Lists; using Umbraco.Web; using Umbraco.Web.Mvc; using System.Web.UI.WebControls; using USNStarterKit.USNHelpers; using USNOptions = USNStarterKit.USNEnums.Options; using Newtonsoft.Json.Linq; using System.Linq; using System.Net; using umbraco.cms.businesslogic.member; using Umbraco.Core; using IBD.IBDDatabase.pocos;

    namespace IBD.IBDControllers { public class IBDMembershipFormSurfaceController : Umbraco.Web.Mvc.SurfaceController {

    //...Build drop down box //.. Load other info for emailing

    //.. google capture

            HandleMembershipDBPost(model);
    

    // email form

        [HttpPost]
        public ActionResult HandleMembershipDBPost(IBDMembershipFormViewModel model)
        {
            //Check if the data on the model is valid
            if (!ModelState.IsValid)
            {
                //There was a validation error with the data
                return CurrentUmbracoPage();
            }
    
            //Continue processing the data...
    
            // Create New Member
    
            int MemberID = RegisterMember(model.CompanyName, model.CompanyName, model.Email, "password123", "IBD Website Member");
    
            if (MemberID > 0)
            {
                //Create new member details object
                var MemberToAdd = new IBDMembership();
    
                //Set values from view model & grab the current node ID
                //            MemberToAdd.MemberID = UmbracoContext.PageId.Value;
    
                MemberToAdd.MemberId = MemberID;
                MemberToAdd.CompanyName = " "; //model.CompanyName == null ? " " : model.CompanyName;
                MemberToAdd.MembershipType = " "; //model.MembershipType == null ? " " : model.MembershipType;
                MemberToAdd.BuyLocalMember = model.TownProudSignup;
                MemberToAdd.Position = " "; //model.Position == null ? " " : model.Position;
                MemberToAdd.FName = " "; //model.FirstName == null ? " " : model.FirstName;
                MemberToAdd.LName = " "; //model.FirstName == null ? " " : model.LastName;
                MemberToAdd.NumEmployees = Int32.Parse(model.NumEmployees);
                MemberToAdd.Telephone = " "; // model.Telephone == null ? " " : model.Telephone;
                MemberToAdd.CellPhone = " "; //model.CellPhone == null ? " " : model.CellPhone;
                MemberToAdd.Address1 = " "; //model.Address1 == null ? " " : model.Address1;
                MemberToAdd.Address2 = " "; //model.Address2 == null ? " " : model.Address2;
                MemberToAdd.Address3 = " "; //model.Address3 == null ? " " : model.Address3;
                MemberToAdd.TownCity = " "; //model.TownCity == null ? " " : model.TownCity;
                MemberToAdd.State = " "; //model.State == null ? " " : model.State;
                MemberToAdd.PostalCode = " "; //model.PostalCode == null ? " " : model.PostalCode;
                MemberToAdd.Country = " "; //model.Country == null ? " " : model.Country;
                MemberToAdd.Facebook = " "; //model.Facebook == null ? " " : model.Facebook;
                MemberToAdd.Website = " "; //model.Website == null ? " " : model.Website;
                MemberToAdd.NewsletterSignup = model.NewsletterSignup;
                MemberToAdd.Active = false;
                MemberToAdd.Approved = false;
                MemberToAdd.FirstJoinDate = DateTime.Today;
                MemberToAdd.LastUpdateDate = DateTime.Today;
    
    
                //Get the Umbraco db
                var db = ApplicationContext.DatabaseContext.Database;
    
                //Add the object to the DB
                db.Insert(MemberToAdd);
            }
    
            //All done - redirect to the page
            return RedirectToCurrentUmbracoPage();   
        }
    
        public static int RegisterMember(string username, string name, string email, string password = null, string roleName = null)
        {
            //Create the member
            var member = ApplicationContext.Current.Services.MemberService.CreateMember(username, email, name, "Member");
    
            //Set them to be approved
            member.IsApproved = true;
    
            //Save the member before adding a password or assigning them to a role.
            ApplicationContext.Current.Services.MemberService.Save(member);
    
            //if no password was provided, create a random one here, so people can't login with an empty password.
            if (string.IsNullOrEmpty(password))
            {
                password = Guid.NewGuid().ToString().Substring(0, 8);
            }
            ApplicationContext.Current.Services.MemberService.SavePassword(member, password);
    
            //If a role name was passed in, assign them to the role here.
            if (!String.IsNullOrEmpty(roleName))
            {
                ApplicationContext.Current.Services.MemberService.AssignRole(member.Id, roleName);
            }
            return member.Id;
        }
    }
    

    }

  • Daniel Rogers 139 posts 717 karma points
    Oct 18, 2018 @ 11:18
    Daniel Rogers
    100

    Solved

    Required [ExplicitColumns]

    namespace IBD.IBDDatabase.pocos { [TableName("IBDMembership")] [PrimaryKey("Id", autoIncrement = true)] [ExplicitColumns] public class IBDMembership { [Column("id")] [PrimaryKeyColumn(AutoIncrement = true)] public int id { get; set; }

Please Sign in or register to post replies

Write your reply to:

Draft