Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Linq-ToSQL Invalid Cast

Linq-ToSQL Invalid Cast

Scheduled Pinned Locked Moved Database
databasecsharphelplinqquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups