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. Web Development
  3. ASP.NET
  4. sequence contains no elements

sequence contains no elements

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestionlearning
4 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.
  • X Offline
    X Offline
    xnaLearner
    wrote on last edited by
    #1

    So a drop down is used to select the users name, then click 'view' and it will display all the holidays this user has previously booked. From here the user can click 'create new' which will bring them to a create new page. Instead of having to re-select their name from another drop down i carried across the currentuserID and replaced the second drop down with the users ID. Everything was working fine with 2 separate drop downs, the user could book holidays etc... However now (after I changed the second drop down to hold the ID value) I am getting an error from my listHoliday Method in my controller: Error: InvalidOperationException was unhandled by the user code Sequence contains no elements var dbPerson1 = (from p in db.People where p.Id == PersonId select p).Single(); Could anyone advise what to do? Im lost thanks for any replies.

    M 1 Reply Last reply
    0
    • X xnaLearner

      So a drop down is used to select the users name, then click 'view' and it will display all the holidays this user has previously booked. From here the user can click 'create new' which will bring them to a create new page. Instead of having to re-select their name from another drop down i carried across the currentuserID and replaced the second drop down with the users ID. Everything was working fine with 2 separate drop downs, the user could book holidays etc... However now (after I changed the second drop down to hold the ID value) I am getting an error from my listHoliday Method in my controller: Error: InvalidOperationException was unhandled by the user code Sequence contains no elements var dbPerson1 = (from p in db.People where p.Id == PersonId select p).Single(); Could anyone advise what to do? Im lost thanks for any replies.

      M Offline
      M Offline
      Matt U
      wrote on last edited by
      #2

      First, make sure the proper set of ID values is actually being bound to the drop-down. Secondly, the "Single()" method will throw that exception if the query does not return any results. If it's possible that the "p.Id == PersonId" condition could yield ZERO results, use "SingleOrDefault()" instead, which will assign a null value to dbPerson1 if nothing is found with the query. I'd say debug with a breakpoint and verify that, for one, the drop-down has the correct ID's assigned to it, and also that "PersonId" is capturing the correct value. Also, make sure that the "PersonId" does in fact exist in the "People" table.

      djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

      X 1 Reply Last reply
      0
      • M Matt U

        First, make sure the proper set of ID values is actually being bound to the drop-down. Secondly, the "Single()" method will throw that exception if the query does not return any results. If it's possible that the "p.Id == PersonId" condition could yield ZERO results, use "SingleOrDefault()" instead, which will assign a null value to dbPerson1 if nothing is found with the query. I'd say debug with a breakpoint and verify that, for one, the drop-down has the correct ID's assigned to it, and also that "PersonId" is capturing the correct value. Also, make sure that the "PersonId" does in fact exist in the "People" table.

        djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

        X Offline
        X Offline
        xnaLearner
        wrote on last edited by
        #3

        hey matt...thanks for teh reply man that sorted it

        M 1 Reply Last reply
        0
        • X xnaLearner

          hey matt...thanks for teh reply man that sorted it

          M Offline
          M Offline
          Matt U
          wrote on last edited by
          #4

          What was the issue? Was the list not getting the correct values or did a "SingleOrDefault()" call take care of it?

          djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.

          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