Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call stored procedure returning both a table and a return statement in a controller in mvc entity framework 4?
    primarykey
    data
    text
    <p>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.<br> (I'm using Entity Framework 4)</p> <hr> <pre><code>Stored Procedure : </code></pre> <p>CREATE PROC [dbo].[sp_list24](@emp dbo.list READONLY ,@error int output) AS BEGIN BEGIN TRY DECLARE @Error_State INT</p> <p>SET @Error_State = 1</p> <p>UPDATE dbo.employee SET mobile=mobile+11 WHERE eid=2 SELECT * FROM @emp set @error=@Error_State; select @error </p> <p>END TRY BEGIN CATCH SET @error = ERROR_STATE() select @error </p> <p>END CATCH END GO</p> <hr> <p>Here 'list' is a userdefined table type for passing table valued parameter.</p> <pre><code>CREATE TYPE [dbo].[list] AS TABLE( [eid] [int] NULL, [name] [nvarchar](50) NULL, [age] [int] NULL ) </code></pre> <hr> <h2> In controller:</h2> <pre><code> [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&lt;Item&gt;("exec sp_list20 @emp", emp1); return Json(resp.ToList()); } </code></pre> <h2> }</h2> <pre><code>In view: </code></pre> <p>paragraph id is "sdf" and button id is "asd"!!!!!</p> <pre><code>script: </code></pre> <hr> <pre><code> $("#asd").click(function () { var a = 1; var content = "&lt;table&gt;&lt;th&gt;Id&lt;/th&gt;&lt;th&gt;Name &lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;/tr&gt;"; $.ajax({ type: 'POST', url: '/Home/Onclick/', data: { 'id': a }, datatype: 'json', success: function (data) { $.each(data, function (i, item) { content += "&lt;tr&gt;"; content += "&lt;td style=\"background-color:White\"&gt;" + data[i].eid + "&lt;/td&gt;"; content += "&lt;td style=\"background-color:White\"&gt;" + data[i].name + "&lt;/td&gt;"; content += "&lt;td style=\"background-color:White\"&gt;" + data[i].age + "&lt;/td&gt;"; content += "&lt;/tr&gt;"; }); content += "&lt;/table&gt;"; $('#sdf').html(content); alert("success"); }, error: function () { } }); }); </code></pre> <h2> </h2> <p>Result displays:</p> <p>Id Name Age</p> <p>1 john 21</p> <p>2 albert 22</p> <p>3 martin 23</p> <hr> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload