0

I have created an Interface called ILoginService and a Class implementing this service called LoginService. I am passing a stored Procedure through this service that returns a Scalar value.

The problem is that ILoginService object returns a null value giving Object reference not set to an instance of an object Error

Please help with this code is as follows:

LoginService Code

 public partial class LoginService : ILoginService
{

    TimesheetsManagementEntities TimesheetDatabase = new TimesheetsManagementEntities();

    private readonly ILoginService _LoginService;
    object UserAutheticationStatusObject;

    int UserAutheticationStatusCode;

    public LoginService(ILoginService LoginService) {


        _LoginService = LoginService;
    }


    public int UserAuthetication(string EmailId, string LoginPassword)
    {
        using (TimesheetDatabase) {
             TimesheetDatabase.Database.Connection.Open();
             var command = TimesheetDatabase.Database.Connection.CreateCommand();
             command.CommandText = "dbo.UserLoginAuthentication";
             command.CommandType = System.Data.CommandType.StoredProcedure;
             UserAutheticationStatusObject = (command.ExecuteScalar());

        }

        UserAutheticationStatusCode = (int)UserAutheticationStatusObject;

            return UserAutheticationStatusCode;
    }
}

Controller code

 public class AccountsController : Controller
{

    private readonly ILoginService _LoginService;

    int UserAuthenticationStatus;




    public AccountsController(ILoginService LoginService) {

        _LoginService = LoginService;

    }


    public AccountsController() { }



    public ActionResult Login()
    {
        LoginViewModel LoginModel = new LoginViewModel();

      UserAuthenticationStatus = _LoginService.UserAuthetication(LoginModel.EmailId, LoginModel.LoginPassword);



        return View(LoginModel);
    }
}

ViewModel Code

public class LoginViewModel
{
    public string EmailId { get; set; }
    public string LoginPassword { get; set; }

}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Something seems wrong with this, there's no parameterless constructor for your LoginService class, and the only constructor available require a same type LoginService object ... I just don't see how this could work ...

Also, your ILoginService seems to be injected into your AccountController, make sure your DependencyInjection container can instanciate your ILoginService class properly.

Nicolas Boisvert
  • 1,141
  • 2
  • 10
  • 26