Hi
Fatal Error - no parameterless constructor defined
I need to get rid of this error. Codes are here. Do you know how to avoid it?
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace SportsStore.Entities
{
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public Product()
{
string connection_string = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\USER\\source\\repos\\SportsStore\\SportsStore\\App_Data\\SportsStoreDatabase.mdf;Integrated Security=True";
SqlConnection connect = new SqlConnection(connection_string);
// SqlCommand cmd = new SqlCommand();
try
{
connect.Open();
HttpContext.Current.Response.Write("Database Connection Open !");
}
catch
{
HttpContext.Current.Response.Write("Database Connection Error !");
}
finally
{
connect.Close();
HttpContext.Current.Response.Write("Database Connection Closed !");
}
}
}
}
using SportsStore.Abstract;
using SportsStore.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SportsStore.Controllers
{
public class ProductController : Controller
{
private IProductRepository repository;
public int PageSize = 4;
// GET: Product
/**/
/* public ProductController()
{
//repository.Products.ToList();
}*/
public IProductRepository rep;
/*
private readonly ApplicationDbContext _applicationDbContext;
string connectionString = string.Empty;
public void CommonService(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
connectionString = _applicationDbContext.Database.GetDbConnection().ConnectionString;
}*/
/*
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}*/
public ProductController(IProductRepository productRepository)
{
}
/*
public ViewResult List(int page = 1) {
return View(repository.Products.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize));
}*/
public string HelloWorld()
{
return "HelloWorld";
}
}
}
Server Error in '/' Application.
No parameterless constructor defined for this object.
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.MissingMethodException: No parameterless constructor defined for this object.
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:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +122
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +239
System.Activator.CreateInstance(Type type, Boolean nonPublic) +85
System.Activator.CreateInstance(Type type) +12
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'SportsStore.Controllers.ProductController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +102
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +188
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +105
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace SportsStore.Entities
{
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public Product()
{
string connection_string = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\USER\\source\\repos\\SportsStore\\SportsStore\\App_Data\\SportsStoreDatabase.mdf;Integrated Security=True";
SqlConnection connect = new SqlConnection(connection_string);
// SqlCommand cmd = new SqlCommand();
try
{
connect.Open();
HttpContext.Current.Response.Write("Database Connection Open !");
}
catch
{
HttpContext.Current.Response.Write("Database Connection Error !");
}
finally
{
connect.Close();
HttpContext.Current.Response.Write("Database Connection Closed !");
}
}
}
}