关于asp.net:从字符串””转换为类型” Double无效”

Conversion from string “” to type 'Double is not valid

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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


我怀疑问题出在此行:

1
Where p.occupant = find_id.FirstOrDefault.id _

这两个都是同一类型吗?我怀疑这行的原因是因为在先前的另一个表中,您使用occupant作为人员名称。我将检查where子句中的其他值是否也与您将它们进行比较的类型匹配。

在分配行中可能出现错误的原因是,直到在其上运行First才执行查询。该错误最有可能在查询中,而不是在分配中。


它不能将空字符串转换为双精度。放入if语句,检查是否有空字符串,如果为空,则将double设置为0。