Hi I'm trying to remove a user from my database. I'm not sure why I'm receiving this error though since none of my variables (employeeName, departmentName) are of type int.
The debugger keeps stopping at this block of code with the "Unable to cast object of type 'System.Int32' to type 'System.String'." error.
//Note: employeeName type is navchar
var empQuery =
from dep in db.Department
join emp in db.EmployeeDetails
on dep.departmentId equals emp.departmentId
where dep.departmentName == reqDep
&& emp.employeeName == reqUser
select emp;
//Debugger keeps pointing to the foreach with the error
foreach (var element in empQuery)
{
Console.WriteLine("user: " + element + " has been deleted");
db.EmployeeDetails.DeleteOnSubmit(element);
}
// Freeze the console window.
Console.ReadLine();
If it helps, I've included the entire code below.
namespace runLinqSql
{
// Table<T> abstracts database details per table/data type.
[Database]
public class FireEvacuation : DataContext
{
public Table<Employee> EmployeeDetails;
public Table<EmpDepartment> Department;
public FireEvacuation(string connection) : base(connection) { }
}
class Program
{
static void Main(string[] args)
{
// Use a connection string.
FireEvacuation db = new FireEvacuation
(@"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\runSqlLinq\FireEvacuation.mdf");
////specify department to be removed
string reqDep = "KOM";
// Attach the log to show generated SQL.
db.Log = Console.Out;
////specify user to be removed
string reqUser = "Jason";
//Note: employeeName type is navchar
var empQuery =
from dep in db.Department
join emp in db.EmployeeDetails
on dep.departmentId equals emp.departmentId
where dep.departmentName == reqDep
&& emp.employeeName == reqUser
select emp;
//Debugger keeps pointing to the foreach with the error
foreach (var element in empQuery)
{
Console.WriteLine("user: " + element + " has been deleted");
db.EmployeeDetails.DeleteOnSubmit(element);
}
// Freeze the console window.
Console.ReadLine();
try
{
db.SubmitChanges();
}
catch (Exception e)
{
Console.WriteLine(e);
// Provide for exceptions.
}