Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HomeContext homecontext = new HomeContext();
            homecontext.Database.Connection.Open();
          Home hom=  homecontext.home.Single(emp => emp.EmployeeId == 1);
            return View(hom);


What I have tried:

I Want to get data from database on view using entity framework but it's give some error like("
An error occurred while executing the command definition. See the inner exception for details. 
")
Please any one help me
Thank You
Posted
Updated 8-Apr-18 5:12am
Comments
Thomas.D Williams 6-Apr-18 14:50pm    
Are you using Entity Framework? Is "home" a DbSet property on your HomeContext? Normally you would not need to call Database.Connection.Open() in EF, it's handled automatically when it's injected into your controller.
Dave Kreskowiak 6-Apr-18 15:03pm    
So, what does the InnerException property of the exception you're looking at say?
satyanand mishra 8-Apr-18 8:41am    
You are very right sir I am using Entity framework but When I run this code:{
HomeContext homecontext = new HomeContext();
Home hom= homecontext.home.Single(emp => emp.EmployeeId == 1);
return View(hom);}
and my model code is
{
public DbSet<home> home { get; set; }
}
and
{[Table("std")]
public class Home
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Gender { get; set; }
}
and view code is
{


EmployeeId:- @Model.EmployeeId
Name:-@Model.Name
City:-@Model.City
Gender:-@Model.Gender


}
and my connection string is
{
<connectionstrings>
<add name="HomeContext" connectionString= "server=SATYA\SAT;database=student; integrated security=SSPI;" providerName="System.Data.SqlClient"/>


}
and
{
this code on global.asax file
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Database.SetInitializer<mvcapplication7.models.homecontext>(null);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
It's give exception like
{An error occurred while executing the command definition. See the inner exception for details.}
{
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityCommandExecutionException', but this dictionary requires a model item of type 'MvcApplication7.Models.Home'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityCommandExecutionException', but this dictionary requires a model item of type 'MvcApplication7.Models.Home'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityCommandExecutionException', but this dictionary requires a model item of type 'MvcApplication7.Models.Home'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +378
System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +614
System.Web.Mvc.ViewDataDictionary`1..ctor(ViewDataDictionary viewDataDictionary) +37
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +98
System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +38
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +425
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.<>c__DisplayClass1a.<invokeactionresultwithfilters>b__17() +74
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +388
System.Web.Mvc.<>c__DisplayClass1c.<invokeactionresultwithfilters>b__19() +72
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult

1 solution

C# is case sensitive!

On your DbSet line, you have to tell the DbSet which type it's going to store. That type is "Home", because that's what you called your Home class, not "home".
C#
public DbSet<home> home { get; set; }

should be
C#
public DbSet<Home> home { get; set; }

See the difference?
 
Share this answer
 
Comments
satyanand mishra 8-Apr-18 12:40pm    
I correct it but i get still same exception
Dave Kreskowiak 8-Apr-18 13:53pm    
What does the complete Razor code look like for the View you're trying to use?
satyanand mishra 9-Apr-18 8:52am    
HomeController Code is=>
----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication7.Models;
namespace MvcApplication7.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
try
{
HomeContext homecontext = new HomeContext();

Home home1 = homecontext.home.Single(emp => emp.EmployeeId == 1);
return View(home1);
}
catch (Exception ex)
{
return View(ex);
}
}

}
}
--------------------Model Code--------
First Model Is Home.cd----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication7.Models
{
[Table("tblHome")]
public class Home
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Gender { get; set; }
}
}
---------------------Second Model Is- HomeContext.cs-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication7.Models
{
public class HomeContext:DbContext
{
public DbSet<Home> home { get; set; }

}
----------------And My View Code is-----------------
@model MvcApplication7.Models.Home

@{
Layout = null;
}

<!DOCTYPE html>



<meta name="viewport" content="width=device-width" />
<title>Index



EmployeeId:- @Model.EmployeeId
Name:-@Model.Name
City:-@Model.City
Gender:-@Model.Gender



------And My Connection String-------------
<connectionstrings>
<add name="HomeContext" connectionString= "server=SATYA\SAT;database=employee; integrated security=SSPI;" providerName="System.Data.SqlClient"/>


And -----------GlobalFile.asax.cs file code is---------------------
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Database.SetInitializer<mvcapplication7.models.homecontext>(null);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
I want to get data from database on view
but it's give exception like
------------------------------------------------------------------
Server Error in '/MvcApplication7' Application.
The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityException', but this dictionary requires a model item of type 'MvcApplication7.Models.Home'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityException', but this dictionary requires a model item of type 'MvcApplication7.Models.Home'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Core.EntityException', but this dictionary requires a model item of type 'MvcApplication7.Models
Dave Kreskowiak 9-Apr-18 9:23am    
In your controller code, you have a try/catch block. In the catch block, you're returning the default Home view but passing in an Exception. The view expects a Home instance, but you're giving it an Exception instance. You have to return a different view if you're going to pass in an Exception, one that expects an Exception instance.

To figure out why the EF code is failing, put a breakpoint on the line "return View(ex);" and run the code. When it fails, it'll stop at the breakpoint and you can then look at the ex variable in the debugger and examine the exception and the inner exception inside it for more information on why the query is failing. Chances are good you don't have anything that has an employeeId of 1.
satyanand mishra 12-Apr-18 10:20am    
Thank You very much Sir For Giving me suggestion finally I resolve the problem.

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