How can I call stored procedure returning both a table and a return statement in a controller in mvc entity framework 4?

StackOverflow https://stackoverflow.com/questions/16645503

سؤال

I have a stored procedure which returns a table as well as a return errorstate which is an int. How can I call this stored procedure from a controller in MVC.
(I'm using Entity Framework 4)


Stored Procedure :

CREATE PROC [dbo].[sp_list24](@emp dbo.list READONLY ,@error int output) AS BEGIN BEGIN TRY DECLARE @Error_State INT

SET @Error_State = 1

UPDATE dbo.employee SET mobile=mobile+11 WHERE eid=2 SELECT * FROM @emp set @error=@Error_State; select @error

END TRY BEGIN CATCH SET @error = ERROR_STATE() select @error

END CATCH END GO


Here 'list' is a userdefined table type for passing table valued parameter.

CREATE TYPE [dbo].[list] AS TABLE(
    [eid] [int] NULL,
    [name] [nvarchar](50) NULL,
    [age] [int] NULL
)

In controller:

 [HttpPost]
        public JsonResult Onclick(int id)
        {

            using (examemployeeEntities1 eee = new examemployeeEntities1())
            {
                //Create table value parameter
                DataTable dt = new DataTable();
                DataRow dr = dt.NewRow();
                dt.Columns.Add("eid");
                dt.Columns.Add("name");
                dt.Columns.Add("age");
                dt.Rows.Add(1, "john", 21);
                dt.Rows.Add(2, "albert", 22);
                dt.Rows.Add(3, "martin", 33);

                SqlParameter emp1 = new SqlParameter("@emp", SqlDbType.Structured);
                emp1.Value = dt;
                emp1.TypeName = "list";

                //eee.Database.ExecuteSqlCommand("EXEC sp_list4 @emp",emp1);
                var resp = eee.Database.SqlQuery<Item>("exec sp_list20 @emp", emp1);

                return Json(resp.ToList());  
            }

}

In view:

paragraph id is "sdf" and button id is "asd"!!!!!

script:

    $("#asd").click(function () {

        var a = 1;
        var content = "<table><th>Id</th><th>Name </th><th>Age</th></tr>";
        $.ajax({
            type: 'POST',
            url: '/Home/Onclick/',
            data: { 'id': a },
            datatype: 'json',
            success: function (data) {
                $.each(data, function (i, item) {
                    content += "<tr>";
                    content += "<td style=\"background-color:White\">" + data[i].eid + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].name + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].age + "</td>";
                    content += "</tr>";
                });
                content += "</table>";
                $('#sdf').html(content);
                alert("success");
            },
            error: function () {
            }
        });

    });

Result displays:

Id Name Age

1 john 21

2 albert 22

3 martin 23


It doesn't return @Error_State. I tried using select @Error_State also. But it also don't work. How can I call stored procedure returning both a table and a return statement?

هل كانت مفيدة؟

المحلول

An output parameter is enough to solve it..try including it like

 var outParam = new SqlParameter();
       outParam.ParameterName = "error";
                    outParam.SqlDbType = SqlDbType.Int;
                    outParam.Direction = ParameterDirection.Output;

                    var resp = eee.Database.SqlQuery<Item>("exec sp_list23 @emp,@error OUT", emp1,outParam);
                    var error = (int)outParam.Value;

نصائح أخرى

You have to use OUTPUT parameter of stored procedure. There is a tutorial on MSDN: Using a Stored Procedure with Output Parameters

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top