cannot retrieve inserted id

cannot retrieve inserted id

montoyammontoyam Posts: 568Questions: 136Answers: 5

I am getting an error when trying to add a new record. the primary key is set as identity with a seed/increment. All the posts I see in the forum are talking about schemas but this one is just dbo. The primary key is not called "ID"

model:

        public class LineItemCategoryModel 
        {
            public int CategoryID { get; set; }
            public string category { get; set; }
            public string sortby { get; set; }
        }

controller:

                public class LineItemCategoriesController : ApiController
                {
                    [Route("api/LineItemCategories")]
                    [HttpGet]
                    [HttpPost]
                    public IHttpActionResult Categories()
                    {
                        var request = HttpContext.Current.Request;
                        var settings = Properties.Settings.Default;
            
                        using (var db = new Database(settings.DbType, settings.DbConnection))
                        {
                            var response = new Editor(db, "InvoiceStructure_Categories", "CategoryID")
                                .Model<LineItemCategoryModel>()
                                .Process(request)
                                .Data();

                            return Json(response);
                        }
                    }
                }

js

    /**************************************************/
    var categoryEditor = new $.fn.dataTable.Editor( {
        ajax: '/api/LineItemCategories',
        table: '#Categories',
        fields: [
            //{"label": "CategoryID:", "name": "CategoryID"},
            {"label": "Category:", "name": "category"},
            {"label": "SortBy:", "name": "sortby"}
        ]
    } );

    var categoryTable = $('#Categories').DataTable( {
        dom: 'Bfrtip',
        ajax: '/api/LineItemCategories',
        columns: [
            //{"data": "CategoryID"},
            {"data": "category"},
            {"data": "sortby"}
        ],
        select: {style: 'single'},
        lengthChange: false,
        buttons: [
            { extend: 'create', editor: categoryEditor },
            { extend: 'edit', editor: categoryEditor },
            { extend: 'remove', editor: categoryEditor }
        ]
    });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    You can rename the primary key with idSrc, see example here. Hopefully that'll do the trick,

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    still getting same error.

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    is there something that I need to define in the controller or model telling it that sql is going to assign an autonumber?

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    oh my goodness!! So when the error says "No primary key found", go to your table and make sure you have your field set as Primary!!! Wow... I can't believe I missed that. I had identity seed and everything...except primary key!!

    Sorry, thank you for your time.

  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    Answer ✓

    Fantastic - great to hear you've got it working and the error messages actually helped rather than just obfuscating the problem (which I've seen all to often in error messages)!

    Allan

This discussion has been closed.