Hey Ahmed,
Based on the provided code, I can see a few potential issues that might be causing the data not to be inserted into the SQL Server database.
Database Context (_context): Ensure that the _context variable in your Web API (AddOwnerFile) is properly initialized and configured to connect to the correct database. Check that the _context is not null and that it's properly configured to perform the database operation.
JSON Serialization: In the Web API AddOwnerFile method, you are deserializing the owner object using JsonConvert.DeserializeObject, but you are not specifying the correct type for deserialization. Instead of using Dictionary<string, object>, create a class representing the data structure for the owner object:
//csharp
public class OwnerData { public string EmployeeID { get; set; } public string DepartementCode { get; set; } } Then, use this class for deserialization:
//csharp
[HttpPost] public ActionResult AddOwnerFile([FromBody] OwnerData owner) { // Access owner.EmployeeID and owner.DepartementCode // ... rest of the code } JSON Property Names: Ensure that the property names in the OwnerData class match the property names being sent from the client-side JavaScript. For example, in the OwnerData class, the property name is EmployeeID, but in the JavaScript, you are using employeeID.
Passing Data from JavaScript: In the JavaScript function insertData, make sure that the owner object passed to the AJAX request has the correct structure, corresponding to the OwnerData class:
//javascript
function insertData(owner) { $.ajax({ type: 'POST', url: '/api/Owner/AddOwnerFile', data: JSON.stringify({ employeeID: owner.employeeID, departementCode: owner.departementCode }), contentType: 'application/json', success: function () { alert('Data inserted successfully!'); }, error: function () { alert('Error inserting data!'); } }); } Database Permissions: Ensure that the user accessing the SQL Server database has the necessary permissions to perform the insert operation on the Owner table. Please make sure to address these points and see if it resolves the issue. Additionally, it's a good idea to check the browser console and server-side logs for any error messages that might provide further insight into the problem. If the issue persists, you can use console.log or debugging techniques to inspect the data being passed between the client and server-side to identify any discrepancies. I have implemented a bit of the same logic in one of client's site which is Keralajackpot.in