I'm trying to create a report that displays some information in a DataGrid but I am having a problem with the SQL method. The fields I am displaying are coming from all different tables. When I run the query in MySqlWorkbench it works but when I try run the program in visual studio I get an error.
private static string SearchSQL
{
get
{
return @" SELECT fleetchecklist.*,
cl.ChecklistNo As CheckListNo,
v.VehicleOwnerID AS VehicleOwnerID,
v.VehicleOwner AS VehicleOwnerName,
v.Reg As VehicleReg,
t.TrailerOwnerID AS TrailerOwnerID,
t.TrailerOwner AS TrailerOwnerName,
t.Reg As TrailerReg,
vdlc.Description AS VehicleDriverLicenseClassName,
tdlc.Description AS TrailerDriverLicenseClassName,
m1.MaintenanceNo AS MaintenanceNo,
m1.Date AS Date
FROM fleetchecklist
LEFT JOIN maintenance m1 ON m1.LinkedID
LEFT JOIN Vehicle v ON m1.LinkedID = v.ID
LEFT JOIN Trailer t ON m1.LinkedID = t.ID
left join Employee ve ON v.VehicleOwnerID = ve.ID
LEFT JOIN Employee te ON t.TrailerOwnerID = te.ID
LEFT JOIN driverlicenseclass vdlc ON ve.DriverLicenseClassID = vdlc.ID
LEFT JOIN driverlicenseclass tdlc ON te.DriverLicenseClassID = tdlc.ID
LEFT JOIN GeneralSmall gs ON m1.TypeID = gs.ID
LEFT JOIN fleetchecklist cl ON m1.ChecklistID = cl.ID
WHERE m1.Company_ID = ?compid ";
}
}
I get the error:
Column 'CheckListID' does not belong to table .
and when I try add code m1.ChecklistID As CheckListID,
for the CheckListID into the SQL I get the error:
Object cannot be cast from DBNull to other types.
The error happens in this method on the CheckListID line:
protected override void FillObject(DataRow dr)
{
ID = Convert.ToInt32(dr["ID"]);
CheckListID = Convert.ToInt32(dr["CheckListID"]);
LinkedItemID = Convert.ToInt32(dr["LinkedItemID"]);
ItemTitle = Convert.ToString(dr["ItemTitle"]);
Checked = Convert.ToBoolean(dr["Checked"]);
Defect = Convert.ToBoolean(dr["Defect"]);
Resolved = Convert.ToBoolean(dr["Resolved"]);
ResolvedDate = dr["ResolvedDate"].DateTimeOrNull();
ResolvedBy = dr["ResolvedBy"].IntOrNull();
if(dr["Comment"] != DBNull.Value)
Comment = Convert.ToString(dr["Comment"]);
if (dr["ResolvedComment"] != DBNull.Value)
ResolvedComment = Convert.ToString(dr["ResolvedComment"]);
}