InvalidProgramException: Invalid IL code (postgres db)

Good morning,
i tried to read a postgres database with my unity app. But when the user tries to enter his login, the following error occurs;

InvalidProgramException: Invalid IL code in Npgsql.NpgsqlCommand:get_Parameters (): IL_0000: ret      

(wrapper remoting-invoke-with-check) Npgsql.NpgsqlCommand:get_Parameters ()
PersonManager.SelectPerson (System.String role, System.String email) (at Assets/Scripts/PersonManager.cs:23)
Customer.SignIn (UnityEngine.GameObject obj) (at Assets/Scripts/Customer.cs:25)
UnityEngine.Events.InvokableCall`1[UnityEngine.GameObject].Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:188)
UnityEngine.Events.CachedInvokableCall`1[UnityEngine.GameObject].Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:306)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

I dont think the issue comes from my c# code, because when i try different methods from my terminal, it works !! meaning the code works fine without unity. So when i press on the login button , it calls the following method:

public void SignIn(GameObject obj)
    {
        company = new Company ("localhost", "kitbox", "bluebeel");
        Person person = company.PersonManager.SelectPerson("customer", obj.GetComponent<InputField> ().text);
        if (person != null)
        {
            StateManager.Instance.person = person;
            ScenesManager.OnChanges ("Intro");
        }
        else
        {
            GameObject.Find ("SignInEmailErrorPanel").GetComponent<Image> ().color = new Color(227, 78, 103, 1);
        }
    }

As you can see, it only calls one method from another class , so this is the code;

public class Company{

    [...]

    private NpgsqlConnection Init(string host, string db, string user)
    {
        string connectionString;
        connectionString = "Host=" + host + ";" + "Database=" +
            db + ";" + "Username=" + user + ";";

        NpgsqlConnection conn = new NpgsqlConnection (connectionString);

        return conn;
    }

    public Company(string host, string db, string user)
    {
        try
        {
            this.connection = Init(host, db, user);
        }
        catch (Exception e)
        {
            throw e;
        }
        this.personManager = new PersonManager(this.connection);
    }
[...]
}
public class PersonManager {

    private NpgsqlConnection connection;
    private NpgsqlCommand command;

        public PersonManager(NpgsqlConnection conn)
        {
            this.connection = conn;
        }

        public Person SelectPerson(string role, string email)
        {
            Person person = null;
            string select = SelectRole(role);
            this.connection.Open();
            this.command = new NpgsqlCommand(select, this.connection);
            this.command.Parameters.Add(new NpgsqlParameter("email", NpgsqlDbType.Varchar)).Value = email;

            NpgsqlDataReader reader = this.command.ExecuteReader();
            while (reader.Read())
            {
                person = new Person(role, (string)reader["name"], (string)reader["address"], (string)reader["phone"], (string)reader["email"], (string)reader["password"]);
                person.Id = (int)reader["id"];
            }
            reader.Close();
            this.connection.Close();

            return person;

        }
}

In the project folder, i have a “plugin” folder in Assets containing Npgsql.dll and System.Data.dll that Unity loads in the beginning.

So i am stuck and don’t really know where the problem is. Thanks in advance for a future answer

It looks like Npgsql is a .NET 4 DLL and right now Unity only supports .NET 3.5 DLL’s. Your options:

  • There’s a Unity beta version for 5.6 that allows you to use .NET 4 DLL’s, but it’s a beta…
  • Wait until 5.6 actually releases, which might support .NET 4.
  • Get the Npgsql code from their git website and try to recompile it as a .NET 3.5 project, or search around on the internet and see if someone else has done that already.