System.InvalidCastException:尝试选择Guid时,指定的强制转换无效

时间:2017-04-13 15:03:53

标签: sql asp.net vb.net

我对编程很新,所以请保持温和。我试图存储对手'的价值。这是tblOrders中唯一标识符到opponentID 在这里我称之为..

Dim opponentID As Guid = order.SelectOpponentID(orderID)

下面是我尝试的代码,但是我得到了System.InvalidCastException:指定的强制转换是无效的。如果有人能为我调整一下,我会非常感激..

VB代码

ArrayList

SQL CODE

Public Function SelectOpponentID(ByVal orderID As Guid)
    Dim DBConnect As New DBConn
    Using db As DbConnection = DBConnect.Conn("DBConnectionString")
        Dim cmd As SqlCommand = DBConnect.Command(db, "SelectOpponentID")

        cmd.Parameters.Add(New SqlParameter("orderID", SqlDbType.UniqueIdentifier, ParameterDirection.Input)).Value = orderID

        db.Open()
        Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Dim opponentID As Guid
        While DR.Read
            opponentID = DR("opponent")
        End While
        DR.Close()
        DR = Nothing
        cmd.Dispose()
        cmd = Nothing
        db.Dispose()
        db.Close()
    End Using
End Function 

1 个答案:

答案 0 :(得分:1)

试试这个:

opponentID = DR.GetGuid(DR.GetOrdinal("opponent"))
相关问题