从字符串“”转换输入 'Double 无效
尝试执行此代码时,即使在值周围放置 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
I keep getting this error when attempting to execute this code, even after placing a CType around the value and defining it as 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它无法将空字符串转换为双精度值。放入 if 语句检查是否为空字符串,如果为空则将 double 设置为 0。
It can't convert empty string to a double. Put in an if statement checking for an empty string and if it is empty then set the double to 0.
我怀疑问题出在这一行:
这两个都是同一类型吗?我怀疑这一行的原因是因为在之前的另一个表中,您使用
占用
作为人名。我会检查 where 子句中的其他值是否也与您要比较的类型匹配。错误可能出现在赋值行中的原因是,在您运行
First
之前不会执行查询。该错误很可能出现在查询中,而不是出现在分配中。I suspect the problem is in this line:
Are both of these of the same type? The reason I suspect this line is because in another table earlier, you're using
occupant
as the name of the person. I would check that the other values in your where clauses also match the types that you are comparing them to as well.The reason that it may appear that the error is in the assignment line is that the query isn't being performed until you run
First
on it. The error is most likely in the query, not in the assignment.