Linq-ToSQL Invalid Cast
-
Using Linq-To-SQL. I have this update method. I'm getting a "Specified cast is not valid exception". The line number of the exception is the query line in the inner Try/Catch.
public void UpdateDashboardInfo(AssayDashboardInfoEntity entity)
{
try
{
using (var context = new AssayDashboardDataContext())
{
AssayDashboardInfo model = null;try { model = (from adi in context.AssayDashboardInfos where adi.ResultId.CompareTo(entity.ResultId) > 0 select adi).FirstOrDefault(); // <= EXCEPTION HERE } catch (Exception e1) { \_logger.Error("\*\*\*\*\*\*\*\*\*\* ARD\_DAL: UpdateDashboardInfo (Query)", e1); } if (model != null) { model.SiteId = entity.SiteId; model.InstrumentId = entity.InstrumentId; model.TowerLocation = entity.TowerLocation; context.SubmitChanges(); } } } catch (Exception e) { \_logger.Error("\*\*\*\*\*\*\*\*\*\* ARD\_DAL: UpdateDashboardInfo", e); }
}
"adi.ResultId" is a DB Guid. "entity.ResultId" is a .Net Guid in my entity class. Anyone see what's wrong with this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Using Linq-To-SQL. I have this update method. I'm getting a "Specified cast is not valid exception". The line number of the exception is the query line in the inner Try/Catch.
public void UpdateDashboardInfo(AssayDashboardInfoEntity entity)
{
try
{
using (var context = new AssayDashboardDataContext())
{
AssayDashboardInfo model = null;try { model = (from adi in context.AssayDashboardInfos where adi.ResultId.CompareTo(entity.ResultId) > 0 select adi).FirstOrDefault(); // <= EXCEPTION HERE } catch (Exception e1) { \_logger.Error("\*\*\*\*\*\*\*\*\*\* ARD\_DAL: UpdateDashboardInfo (Query)", e1); } if (model != null) { model.SiteId = entity.SiteId; model.InstrumentId = entity.InstrumentId; model.TowerLocation = entity.TowerLocation; context.SubmitChanges(); } } } catch (Exception e) { \_logger.Error("\*\*\*\*\*\*\*\*\*\* ARD\_DAL: UpdateDashboardInfo", e); }
}
"adi.ResultId" is a DB Guid. "entity.ResultId" is a .Net Guid in my entity class. Anyone see what's wrong with this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
It looks like you're trying to update the entity with a matching ID. But your query is updating the first entity whose ID is greater than the ID of the entity you've passed in. Does it even make sense to ask if one GUID is greater than another? :confused: I suspect your query should be:
model = (from adi
in context.AssayDashboardInfos
where adi.ResultId == entity.ResultId
select adi).FirstOrDefault();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It looks like you're trying to update the entity with a matching ID. But your query is updating the first entity whose ID is greater than the ID of the entity you've passed in. Does it even make sense to ask if one GUID is greater than another? :confused: I suspect your query should be:
model = (from adi
in context.AssayDashboardInfos
where adi.ResultId == entity.ResultId
select adi).FirstOrDefault();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
ya, should be "string.Equals()" But I was getting the cast exception BEFORE I added the Compare. I'm guessing that there's some problem comparing a SQL GUID against the .Net GUID
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.