Copied to clipboard

Flag this post as spam?

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


  • SimsIOI 2 posts 82 karma points
    May 03, 2018 @ 07:16
    SimsIOI
    0

    Error adding data to custom table from a custom form

    Hi,

    I'm new to Umbraco and really want to use it for a future project however I've hit a stumbling block which seems like a basic issue but I can't seem to get over.

    I have a form within a partial view which is loaded within my page with a ViewModel which contains a select list and all the other relevant fields. When the form is loaded, I populate the select list from a custom table I've got which is a categories table. When I enter all the information, I suave it down and I then get a SqlException with the following error...

    "Cannot insert the value NULL into column 'CategoryId', table 'UmbracoDb.dbo.cwFigure'; column does not allow nulls. INSERT fails."

    When I debug the solution I follow the post to the controller and I have a CategoryId for that property in my model so it isn't null. I can't understand why it is falling over at this. Am I missing something basic?

    Below are my models and the Save action from my controller.

    FigureCategory.cs

    namespace CW.Core.Model
    {
        [TableName("cwFigureCategory")]
        [PrimaryKey("CategoryId", autoIncrement = true)]
        [ExplicitColumns]
        public class FigureCategory
        {
            [Column("CategoryId")]
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int CategoryId { get; set; }
    
            [Column("CategoryName")]
            public string CategoryName { get; set; }
    
            [Column("DateAdded")]
            public DateTime DateAdded { get; set; }
    
            [Column("AddedBy")]
            public string AddedBy { get; set; }
    
        }
    }
    

    Figure.cs

    namespace CW.Core.Model
    {
        [TableName("cwFigure")]
        [PrimaryKey("FigureId", autoIncrement = true)]
        [ExplicitColumns]
        public class Figure
        {
            [Column("FigureId")]
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int FigureId { get; set; }
    
            [Column("FigureName")]
            public string FigureName { get; set; }
    
            [Column("ReleaseYear")]
            public string ReleaseYear { get; set; }
    
            [ForeignKey(typeof(FigureCategory), Name = "FK_CategoryId_cwFigure")]
            public int CategoryId { get; set; }
    
            [Column("UpcNumber")]
            public string UpcNumber { get; set; }
    
            [Column("FigureImage")]
            public string FigureImage { get; set; }
    
            [Column("DateAdded")]
            public DateTime DateAdded { get; set; }
    
            [Column("AddedBy")]
            public string AddedBy { get; set; }
        }
    }
    

    CreateFigureViewModel.cs

    namespace CW.Core.ViewModel
    {
        public class CreateFigureViewModel
        {
    
            public string FigureName { get; set; }
    
            public string ReleaseYear { get; set; }
    
            public int CategoryId { get; set; }
    
            public SelectList FunkoCategories { get; set; }
    
            public string UpcNumber { get; set; }
    
            public string FigureImage { get; set; }
    
            public DateTime DateAdded { get; set; }
    
            public string AddedBy { get; set; }
        }
    }
    

    FigureSurfaceController.cs

        public ActionResult Save(CreateFigureViewModel model)
        {
            if (ModelState.IsValid)
            {
    
                Figure newFigure = new Figure();
    
                newFigure.FigureName = model.FigureName;
                newFigure.ReleaseYear = model.ReleaseYear;
                newFigure.CategoryId = model.CategoryId;
                newFigure.UpcNumber = model.UpcNumber;
                newFigure.FigureImage = model.FigureImage;
                newFigure.DateAdded = model.DateAdded;
                newFigure.AddedBy = model.AddedBy;
    
                figure.Save(newFigure);
                return CurrentUmbracoPage();
            }
    
            return RedirectToCurrentUmbracoPage();
        }
    

    FigureBll.cs

        public Figure Save(Figure figure)
        {
            if (figure != null)
            {
                if (figure.FigureId > 0)
                {
                    db.Update(figure);
                }
                else
                {
                    figure.DateAdded = DateTime.Now;
                    figure.AddedBy = "system";
    
                    try
                    {
                        db.Save(figure);
                    }
                    catch (SqlException sqlEx)
                    {
                        Console.WriteLine(sqlEx);
                        throw;
                    }
    
                }
            }
            return figure;
        }
    
  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    May 03, 2018 @ 08:07
    Dave Woestenborghs
    100

    Hi,

    I see that your figure class doesn't have a ColumName attribute on the categoryId property.

    Can you try to add that and see if it fixes it ?

    Dave

  • SimsIOI 2 posts 82 karma points
    May 03, 2018 @ 08:45
    SimsIOI
    0

    Hi Dave,

    Good spot on that, I missed that when I added it in. I originally structured the table without the foreign key and then added it in.

    Knew it would be a basic mistake. This will forever serve as a reminder for me.

    Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft