【问题标题】:Conversion from string "" to type 'Double' is not valid从字符串“”到类型“Double”的转换无效
【发布时间】:2011-07-30 09:29:03
【问题描述】:

我有一个包含整数的变量,然后我想将其保存为列的值(sql 数据库中的整数类型和实体模型中的 int32 类型),但是当我尝试这个时,我收到错误:

“从字符串“”到类型“Double”的转换无效”

我对此感到非常困惑,因为我没有使用字符串或双精度数?麻烦的是:

UpdateBed.First.occupant = GetID

这里是完整的代码sn-p:

    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

【问题讨论】:

  • 如果 GetID 返回 null,您将无法将其分配给 int 类型。
  • p.id和UpdateBed.First.occupant的类型是什么?
  • 看看 Bed 和 Resident 类的定义会很有用。
  • slugster:我可以通过调试器看到它​​不为空。
  • JaredPar:p.id 是整数,UpdateBed.First.occupant 是整数,在 SQL 级别。对于实体模型,它们是 Int32。

标签: asp.net vb.net entity-framework entity-framework-4 linq-to-entities


【解决方案1】:

您的 LINQ 查询中的这一行 ...

Where p.occupant = ""

...还有这个作业...

UpdateBed.First.occupant = GetID

似乎不能很好地结合在一起(如果 GetID 是 Int32)。不应该是Where p.occupant = 0 之类的吗?

【讨论】:

  • 好点。有没有办法让我说 Where p.occupant = NULL?
  • 好的,我回答了我自己的问题。如果我输入“Is Nothing”而不是 =“”,我可以做到这一点......这可以修复错误。
猜你喜欢
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多