I have a stored procedure as following:
ALTER PROCEDURE [dbo].[Addworkshop]
@title varchar(100),
@time varchar(10),
@date date,
@mandatory int,
@sid int
AS
BEGIN
SET NOCOUNT ON;
insert into Workshop(title, date, time, mandatory, sid)
values(@title, @date, @time, @mandatory, @sid)
declare @wid int
set @wid = scope_identity()
return @wid
END
And I call it from my action method in ASP.NET MVC5 C#:
public JsonResult addWorkshop(Workshop workshop)
{
TLCWREntities context = new TLCWREntities();
var wid = db.Addworkshop(workshop.title, workshop.time, workshop.date, workshop.mandatory, workshop.sid);
return Json(wid);
}
This method is called using JSON ajax:
$.ajax({
type: 'POST',
url: '@Url.Action("addWorkshop")', // we are calling json method
dataType: 'json',
data: $.toDictionary(wsData),
success: function (wid) {
console.log("success add workshop wid: "+wid);
},
error: function (ex) {
console.log("err add workshop");
}
});
The data is successfully inserted to the table and when I execute the stored procedure within SQL Server Management Studio it returns the correct newly inserted row id. However, when I call the stored procedure using JSON ajax and action method it always returns -1.
I am working on Visual Studio 2013 MVC5 C# and SQL Server Studio 2008