Question

I am trying to add a record to an SQL table using Knockout Js and OData using the following script.

Knockout Js

self.Grade = ko.observable();
self.selectedProduct = ko.observable();

    self.add = function (grade) {
        var payload = { ProductName: this.selectedProduct(), GradeName:this.Grade(), Locked:false };
        $.ajax({
            url: '/odata/Grades',
            type: 'POST',
            data: JSON.stringify(payload),
            contentType: 'application/json',
            dataType: 'json'

        });

    }

HTML

<td >
     <select data-bind="options: $root.productNames, optionsText: 'ProductName', optionsValue: 'ProductName', value: selectedProduct, optionsCaption: 'Product'">
                </select></td>
          <td>
    <input type="text" id="txbInput" placeholder="Grade Name" data-bind="value: Grade"/> </td>
          <td>
    <input type="button", onclick="ClearFields();" class="btn btn-success" data-bind="    click: add, visible: !loading()", value="Add Grade"/></td>
      </tr>

But for some reason when I click on Add Grade button to add a record it is throwing me an exception as follows

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:57044/odata/Grades

May I know the reason and best solution to this.

Was it helpful?

Solution

Figured out myself. A simple mistake is that I am not inserting a value Id column and it can't be NULL as it is a key. I changed the Identity specification property of the Id column in SQL table to Yes and it worked.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top