1

我有一個變量,其中包含一個整數,然後我想保存爲一列的值(整數在sql數據庫中的類型和int32在實體模型中),但當我嘗試這個我收到錯誤:從字符串「」轉換爲「Double」類型無效

"Conversion from string "" to type 'Double' is not valid"

我很困惑,因爲我沒有使用字符串或雙?該刁民行是:

UpdateBed.First.occupant = GetID 

這裏是完整的代碼片段:

Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click 
    Using dbContext As pbu_housingEntities = New pbu_housingEntities 
     ' Check that the room is still available. 
     Dim hall As String = CStr(Session("hall")) 
     Dim room As String = CStr(Session("room")) 
     Dim checkOccupants = From p In dbContext.Rooms _ 
          Let building_id = p.Building1.id _ 
          Where p.building_name = hall _ 
          Where p.room1 = room _ 
          Select p.current_occupancy, p.max_occupancy, p.id, building_id 

     If checkOccupants.First.current_occupancy >= checkOccupants.First.max_occupancy Then 
      ' If it isn't available, let the student know. 
      lblResult.Text = "Sorry, this room is now fully occupied. Please choose another room." 
     Else 
      ' If it is available, add the student to the room. 
      Dim Occupant As New Resident 
      Dim gender As String = CStr(Session("gender")) 
      Dim person_name As String = CStr(Session("person_name")) 
      Dim class_level As String = CStr(Session("class_level")) 
      Dim semester As String = CStr(Session("term")) 
      Dim people_code_id As String = CStr(Session("people_code_id")) 
      Dim first_name As String = CStr(Session("first_name")) 
      Dim last_name As String = CStr(Session("last_name")) 
      Dim building_id As String = checkOccupants.First.building_id 
      Dim room_id As String = checkOccupants.First.id 
      Occupant.building = building_id 
      Occupant.room = room_id 
      Occupant.gender = gender 
      Occupant.person_name = person_name 
      Occupant.class_level = class_level 
      Occupant.semester = semester 
      Occupant.people_code_id = people_code_id 
      Occupant.create_date = Date.Now 
      Occupant.first_name = first_name 
      Occupant.last_name = last_name 
      dbContext.Residents.AddObject(Occupant) 

      ' Increment the number of occupants in the room. 
      Dim UpdateOccupancy = (From p In dbContext.Rooms _ 
        Where p.building_name = hall _ 
        Where p.room1 = room _ 
        Select p).First 
      UpdateOccupancy.current_occupancy = UpdateOccupancy.current_occupancy + 1 

      ' Add the student to a bed. 
      Dim UpdateBed = From p In dbContext.Beds _ 
          Where p.building = building_id _ 
          Where p.room = room_id _ 
          Where p.occupant = "" _ 
          Select p 

      ' Get the student's ID from the residency table. 
      Dim GetID = (From p In dbContext.Residents _ 
         Where p.people_code_id = people_code_id _ 
         Order By p.id Descending _ 
         Select p.id).FirstOrDefault 

      UpdateBed.First.occupant = GetID 
      dbContext.SaveChanges() 
      lblResult.Text = "Success! You have successfully requested residency in this room!" 
     End If 
    End Using 
End Sub 
+1

如果GetID返回null,您將無法將其分配給「int」類型。 – slugster 2011-04-05 23:02:22

+1

「p.id」和「UpdateBed.First.occupant」的類型是什麼? – JaredPar 2011-04-05 23:03:22

+0

查看Bed和Resident類的定義將會很有用。 – 2011-04-05 23:06:56

回答

2

這條線在你的LINQ查詢......

Where p.occupant = "" 

...這轉讓...

UpdateBed.First.occupant = GetID 

似乎不是fi很好地結合在一起(如果GetID是Int32)。它不應該是Where p.occupant = 0什麼的?

+0

好點。有沒有辦法讓我說出哪裏p.occupant = NULL? – davemackey 2011-04-05 23:45:15

+0

好的,我回答了我自己的問題。如果我輸入「Is Nothing」而不是=「」,我可以做到這一點,這就解決了錯誤。 – davemackey 2011-04-05 23:46:48

相關問題