Get result from Stored Procedure using Entity Framework DBContext in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to get Stored Procedure result and bind into List

I've database as following 

CREATE TABLE [dbo].[Genres](
	[GenreId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](max) NULL,
	[Description] [nvarchar](max) NULL,
 CONSTRAINT [PK_dbo.Genres] PRIMARY KEY CLUSTERED 
(
	[GenreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Genres] ON 
GO
INSERT [dbo].[Genres] ([GenreId], [Name], [Description]) VALUES (1, N'Rock', NULL)
GO
INSERT [dbo].[Genres] ([GenreId], [Name], [Description]) VALUES (2, N'Jazz', NULL)
GO
SET IDENTITY_INSERT [dbo].[Genres] OFF
GO
/****** Object:  StoredProcedure [dbo].[getGenre]    Script Date: 24/4/2021 2:59:44 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[getGenre]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

 select [GenreId], [Name] from [dbo].[Genres]
END
GO

Class and method as follow 

    public class getGenre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }

        public static List<getGenre> GetGenre()
        {
            List<getGenre> Genre = new List<getGenre>();
            using (MusicStoreDB entities = new MusicStoreDB())
            {
                string procedureName = "getGenre";

                var getObj = entities.Database.ExecuteSqlCommand(procedureName);
            }

            return Genre;
        }
    }

I need help to complete

public static List<getGenre> GetGenre()
Posted 3 months ago

hI wkm1925,

Check this example. Now please take its reference and correct your code.

Using EntityFramework

protected void Page_Load(object sender, EventArgs e)
{
    List<Genre> geners = getGenre.GetGenre()
        .Select(x => new Genre
        {
            GenreId = x.GenreId,
            Name = x.Name
        }).ToList();
}
public class getGenre
{
    public int GenreId { get; set; }
    public string Name { get; set; }
    public static List<getGenre> GetGenre()
    {
        List<Genre> Genre = new List<Genre>();
        using (MusicStoreDB entities = new MusicStoreDB())
        {
            string procedureName = "getGenre";
            return entities.Database.SqlQuery<getGenre>(procedureName).ToList();
        }
    }
}

Using ADo.Net

Namespaces

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

Code

protected void Page_Load(object sender, EventArgs e)
{
    List<Genre> geners = getGenre.GetGenre()
        .Select(x => new Genre
        {
            GenreId = x.GenreId,
            Name = x.Name
        }).ToList();
}
public class getGenre
{
    public int GenreId { get; set; }
    public string Name { get; set; }

    public static List<getGenre> GetGenre()
    {
        List<getGenre> genres = new List<getGenre>();
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
        {
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand("getGenre", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                getGenre genre = new getGenre();
                genre.GenreId = Convert.ToInt32(dr["GenreId"]);
                genre.Name = Convert.ToString(dr["Name"]);
                genres.Add(genre);
            }
        }

        return genres;
    }
}

Screenshot