Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working on Entity Framework. How do I get the result of a stored procedure displayed in a label?

C#
using (var gb = new GaushalaDBEntities())
               {
                   var obj = gb.sp_Get_Latest_Event();
                   if (obj != null)
                   { Label1.Text= //how do i display the result from stored procedure
                   }
               }


What I have tried:

I have tried the solution given here But FirstOrDefault() is not coming.

http://stackoverflow.com/questions/30691349/assign-stored-procedure-result-to-list-using-entity-framework
Posted
Comments
FranzBe 13-Feb-16 4:49am    
What does your stored procedure return?
prapti.n3 13-Feb-16 4:50am    
It returns a Title and an ImageURL. Both are string. It returns only one row

1 solution

If your stored procedure is somehow similar to this

SQL
CREATE  PROCEDURE  [dbo].[sp_Get_Latest_Event]
AS
  SELECT 'thisIsTheLatestEvent' AS EventTitle
  , 'http://192.168.100.101:8080/rootdir/images/theEventImage.png' As EventImageUrl
GO



and you build your entity framework in a way as described here

Stored Procedure in Entity Framework[^]

then you will have something generated like this

C#
public partial class sp_Get_Latest_Event_Result
{
    public string EventTitle { get; set; }
    public string EventImageUrl { get; set; }
}



so the piece of code you are searching for should be like

C#
void TestCallToStoredProc()
{
  using (var context = new GaushalaDBEntities())
  {
    var lastestEventFromSp = context.sp_Get_Latest_Event();
    var firstItem = lastestEventFromSp.FirstOrDefault();
    Console.WriteLine(firstItem.EventTitle);
    Console.WriteLine(firstItem.EventImageUrl);
  }
}


What you get from the sp-call is a ObjectResult<t> where T is sp_Get_Latest_Event_Result

In case you set up things different, you should mention in your question


btw: if you are working with ms sql server naming a stored procedure with "sp_*" is not recommended
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900