转换

0

我不断收到试图执行这些代码时这个错误,甚至放置CTYPE围绕价值和其定义为双后键入“双是不是有效?转换

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

哪条线是扔? – JaredPar 2011-04-05 18:19:23

+2

你确定CType行是错误的地方吗?在下一个LINQ语句中使用它之前,您甚至不会检查find_id是否有值。这可能是一个问题。 – 2011-04-05 18:23:55

回答

1

我怀疑问题出在这行:

Where p.occupant = find_id.FirstOrDefault.id _ 

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

,它可能出现的错误是在分配行的原因是,直到你在它上面运行First未进行查询。错误很可能在查询中,而不是在分配中。

+0

这就是我在他的评论说,12分钟前... – 2011-04-05 18:36:39

+0

@Scott - 他会得到一个NullReference异常,如果FirstOrDefault返回空值。 'find_id'保证是非空的,但它可能是一个空集合。我只是认为'id'的类型与'occupant'不同。可能有一个错误等待发生,但我不认为它在这种情况下正在发生这种错误。仅供参考,在我写完答案之前,我没有看到您的评论。 – tvanfosson 2011-04-05 18:43:56

+0

不用担心!这绝对是行,其中的问题在于 – 2011-04-05 18:44:59

0

它不能将空字符串转换为双精度字符串。放在一个if语句检查一个空字符串,如果是空的,则设置双0