从字符串“”转换输入 'Double 无效

发布于 2024-10-29 21:36:48 字数 1455 浏览 0 评论 0原文

尝试执行此代码时,即使在值周围放置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-11-05 21:37:00

它无法将空字符串转换为双精度值。放入 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.

人│生佛魔见 2024-11-05 21:36:59

我怀疑问题出在这一行:

Where p.occupant = find_id.FirstOrDefault.id _

这两个都是同一类型吗?我怀疑这一行的原因是因为在之前的另一个表中,您使用占用作为人名。我会检查 where 子句中的其他值是否也与您要比较的类型匹配。

错误可能出现在赋值行中的原因是,在您运行 First 之前不会执行查询。该错误很可能出现在查询中,而不是出现在分配中。

I suspect the problem is in this line:

Where p.occupant = find_id.FirstOrDefault.id _

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文