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

时间:2011-04-05 18:16:29

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

在尝试执行此代码时,即使在围绕值放置CType并将其定义为double后,我仍然会收到此错误?

Private Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    ' The deletion of the individual row is automatically handled by the GridView.
    Dim dbDelete As New pbu_housingEntities
    ' Remove individual from the bed.
    Dim occupant As String = GridView1.Rows(e.RowIndex).Cells(2).Text
    Dim room As String = GridView1.Rows(e.RowIndex).Cells(5).Text
    Dim building As String = GridView1.Rows(e.RowIndex).Cells(4).Text
    Dim find_id = From p In dbDelete.Residents _
                  Where p.person_name = occupant _
                  Select p


    Dim remove_bed = From p In dbDelete.Beds _
                     Where p.occupant = find_id.FirstOrDefault.id _
                     Where p.room = room _
                     Where p.building = building _
                     Order By p.id Descending _
                     Select p
    remove_bed.First.occupant = CType(0, Double)
    dbDelete.SaveChanges()

    ' Increase number of open spaces in room.
    Dim update_occupancy = From p In dbDelete.Rooms _
                           Where p.room1 = room
                           Where p.building = building _
                           Select p

    update_occupancy.First.current_occupancy = update_occupancy.First.current_occupancy - 1
    dbDelete.SaveChanges()

End Sub

2 个答案:

答案 0 :(得分:1)

我怀疑问题出在这一行:

Where p.occupant = find_id.FirstOrDefault.id _

两者是否属于同一类型?我怀疑这一行的原因是因为在之前的另一个表中,您使用occupant作为此人的姓名。我会检查你的where子句中的其他值是否也与你比较它们的类型相匹配。

错误出现在分配行中的原因是,在您对其运行First之前,不会执行查询。错误最有可能出现在查询中,而不是在分配中。

答案 1 :(得分:0)

它无法将空字符串转换为double。放入if语句检查空字符串,如果为空,则将double设置为0。