Search code examples
petapoco

The multi-part identifier could not be bound error message petapoco


I am using petapoco relationExtensions with the following :

Connection.db.FetchOneToMany<Project, UserProject>(x=>x.ID, string.Format(@"
            SELECT * 
            FROM Project                         
            WHERE project.id = userProject.ProjectID
                AND userProject.UserID =" + userID + 
                 " ORDER BY project.Name" )); 

but having the following error.

 > The multi-part identifier "userProject.ProjectID" could not be bound. The multi-part identifier "userProject.UserID" could not be bound.

What is wrong? Am I missing anything?

Project Class definition

    private int id;
        public  int ID
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
 public string Name
        {
            get { return name; }
            set { name = value; }
        }

UserProject Class definition

int id;
    int userId;
           int projectId;
 public int ID
    {
        get { return id; }
        set { id = value; }
    }
 public int UserID
    {
        get { return userId; }
        set { userId = value; }
    }
 public int ProjectID
    {
        get { return projectId; }
        set { projectId = value; }
    }

Solution

  • Assuming that there is a UserProject table; the error is a SQL error because you're not joining the UserProject table and referring to it in the sql. Try this :

    Connection.db.FetchOneToMany<Project, UserProject>(x=>x.ID, string.Format(@"
                SELECT * 
                FROM Project                   
                left join userproject on userProject.ProjectID = project.id
                    where userProject.UserID =" + userID + 
                     " ORDER BY project.Name" )); 
    

    The relation extensions don't change your sql