Note that there are some explanatory texts on larger screens.

plurals
  1. POException raised when using a Linq query with Entity Framework
    primarykey
    data
    text
    <p><em>I removed a substantial amount of text from this question because I feel it needed to be more succinct. - Serg</em></p> <p>Here's what I'm trying to do. User selects a State, then a ComboBox loads with Cities in that State, then when user selects a Сity a ListBox loads all projects from that City.</p> <p>When running the method that's supposed to load the projects to the ComboBox, I receive this error:</p> <blockquote> <p>Exception has been thrown by the target of an invocation. Inner Exception is: "{"Specified cast is not valid."}"valid."}"</p> </blockquote> <p>This is the method that causes the exception:</p> <pre><code>private void LoadProjectsToListBox(long idCity) { ProjectRepository projectRepo = new ProjectRepository(); //This line causes the error. var projects = projectRepo.FindAllProjects().Where(c =&gt; c.IDCity == 1).ToList(); } </code></pre> <p>In the Linq query, you'll see that I'm comparing against a hard coded number. 1, is a valid ID of a City that has Projects so it should work. The exception is only raised when I give the Linq query a City ID that has Projects. So it seems the error is raised when the query returns results.</p> <p>Here is the SQL statements I used to generate the Project and City tables:</p> <pre><code>create table City ( ID integer primary key autoincrement, Name string, IDState integer references State(ID) ); create table Project ( ID integer primary key autoincrement, Name string, StartDate text, IDManager integer references Manager(ID), IDCity integer references City(ID), IDDepartment integer references Department(ID), ContactNumber string, Description string ); </code></pre> <p>What strikes me as odd is that using the <strong>exact same pattern and access code</strong> to show a list of Departments (just another table in my database) everything works as expected. For example here's how I load Departments right now just for testing:</p> <pre><code>private void LoadProjectsToListBox(long idCity) { ProjectRepository projectRepo = new ProjectRepository(); DepartmentRepository departmentRepo = new DepartmentRepository(); //Doesn't work - raises exception. var projects = projectRepo.FindAllProjects().Where(c =&gt; c.IDCity == 1).ToList(); //Works exactly as expected. var deps = departmentRepo.FindAllDepartments().Where(c =&gt; c.IDParentDepartment == 7).ToList(); lstProjects.Items.Clear(); foreach (var item in deps) { lstProjects.Items.Add(item.Name); } } </code></pre> <p><strong>Edit:</strong></p> <p>The FindAllDepartments(), FindAllCities(), FindAllProjects() methods are essentially the same.</p> <pre><code>DocumentsDBEntities db = new DocumentsDBEntities(); public IQueryable&lt;Project&gt; FindAllProjects() { return db.Projects; } public IQueryable&lt;City&gt; FindAllCities() { return db.Cities; } public IQueryable&lt;Department&gt; FindAllDepartments() { return db.Departments; } </code></pre> <p>As mentioned before, Department fetching works; Project fetching causes the exception.</p> <p><strong>Edit 3:</strong></p> <p>I fixed it! The problem was with using the datatype of 'Text' in the Project table. If I changed the datatype from that column from 'Text' to 'String' it works flawlessly. Can anyone tell me why? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload