Search code examples
c#databaseinsertdao

CS0120 errorcode c#


I'm trying to add some data to the database using an sql query in c#, but I got this error code in my service class, it says: "An object reference is required for the non-static field, method, or property 'SupervisorDao.AddSupervisor(supervisor)'.

I don't know what to do.

public void AddSupervisor(Supervisor supervisor)
{
    SupervisorDao.AddSupervisor(supervisor)
}

Solution

  • You need an instance of the SupervisorDao object before you can call the methods (unless you make it a static method).

    SupervisorDao superDao = new SupervisorDao();
    superDao.AddSupervisor(supervisor);
    

    Edit: Is SupervisorDao an interface? You'll have to create an object that inherits this interface.