• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 960
  • Last Modified:

binding stored procedure in ASP.NET MVC and Entity Framework

Hi experts,

I saw this example and so I decided do this example.
Execute Stored Procedure using Entity Framework in ASP.NET MVC
http://dotnetvisio.blogspot.com/2014/01/execute-stored-procedure-using-entity.html

I'm using Visual Studio 2013 Ultimate and Sql Server 2008 R2.

On this example, it doesn't have the code or script to create the table in the example.

Here are the script I created, to create the table and stored procedures.

EmpTable

CREATE TABLE [dbo].[EMPTABLE](
	[ID] [int] NOT NULL,
	[DEPTID] [int] NULL,
	[NAME] [varchar](50) NULL,
	[COUNTRY] [varchar](50) NULL,
	[MARRIED] [char](1) NULL,
 CONSTRAINT [PK_EMPTABLE] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (1, 1, N'RAJESH', N'INDIA', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (2, 1, N'SURESH', N'US', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (3, 1, N'ALEX', N'BRAZIL', N'N')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (4, 2, N'LANDER', N'IRAN', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (5, 1, N'JOEL', N'INDIA', N'N')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (6, 2, N'ALBERT', N'INDIA', N'N')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (7, 3, N'KRISTY', N'US', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (8, 3, N'KIRUBA', N'INDIA', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (9, 1, N'YUSAN', N'US', N'Y')
INSERT [dbo].[EMPTABLE] ([ID], [DEPTID], [NAME], [COUNTRY], [MARRIED]) VALUES (10, 2, N'KREG', N'UK', N'Y')

Open in new window


FETCHEMPLOYEES stored procedure

CREATE PROCEDURE [dbo].[FETCHEMPLOYEES]
AS
BEGIN
      SELECT [ID]
            ,[DEPTID]
            ,[NAME]
            ,[COUNTRY]
            ,[MARRIED]
      FROM [MVCEmployeeDB].[dbo].[EMPTABLE]
END
GO

Open in new window


FETCHEMPLOYEE stored procedure

CREATE PROCEDURE [dbo].[FETCHEMPLOYEE](@ID INT)
AS
BEGIN
      SELECT [ID]
            ,[DEPTID]
            ,[NAME]
            ,[COUNTRY]
            ,[MARRIED]
      FROM [MVCEmployeeDB].[dbo].[EMPTABLE]
      WHERE [ID] = @ID
END
GO

Open in new window



So i followed the steps described in the link above.

My Visual Studio 2013 MVC 4 project directory looks like this. I used the MVC 4 blank template and razor when I created my project.

my project directory
When I run my project I get this error message on my view Index.cshtml

my error
You can download my Visual Studio project here which contains my code:

Download Visual Studio 2013 Project

Anyone know why I am getting this error? What did I do wrong?
0
maqskywalker
Asked:
maqskywalker
1 Solution
 
Gary DavisDir Internet SvcsCommented:
This is in the controller. The first action is not passing the model to the view so in the view, Model is null causing the exception.

       
 public ActionResult Index()
        {
            List<EMPTABLE> result = mod.GetEmployees();
            return View();
        }

        public ActionResult Details(int id)
        {
            EMPTABLE result = mod.GetEmployee(id);
            return View(result);
        }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now