I have a stored procedure which returns 2 tables. How can I call this stored procedure from a controller in MVC.
(I'm using Entity Framework 4)
Stored procedure:
create proc [dbo].[sp_list33](@emp dbo.list READONLY )
as
select * from dbo.Items
select * from dbo.dept
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_list33 @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 content in the Item table only. How to get two entities from stored procedure? It is only retrieving the first select statement. Can any one help me to solve this..?