我知道我在这里是个白痴,我无法解决这个问题。但我正试图从vb.net数据库中取回一些数据。它没有被设置为对象错误的实例。在代码运行之前,它表示变量在设置之前正在使用,但我看不清楚如何。代码:
Private taNotifications As dsDataTableAdapters.NotificationsTableAdapter = New dsDataTableAdapters.NotificationsTableAdapter
Dim notification As dsData.NotificationsDataTable = taNotifications.GetDataByClientID(userrow.UserID)
If notification.Rows.Count > 0 Then
Dim notificationrow As dsData.NotificationsRow
Dim forwardURL As String = notificationrow.ForwardLocation
End If
它落在Dim forwardURL As String = notificationrow.ForwardLocation
答案 0 :(得分:5)
问题是你从未在if语句中实例化notificationRow。你已经声明了它,但它不属于任何东西。在对此对象执行任何操作之前,您需要进行赋值或遍历行:
Dim notificationrow As dsData.NotificationsRow ' this is not instantiated
Dim forwardURL As String = notificationrow.ForwardLocation
在这种情况下你真正想要的是:
For Each notificationRow As dsData.NotificationRow In notification
Dim forwardURL As String = notificationRow.ForwardLocation
' Do Something
Next
如果你只有一行并且你知道你只有1行或0行,那么你可以通过这样做来使用你的if语句:
If notification.Rows.Count > 0 Then
Dim notificationrow As dsData.NotificationsRow = _
CType(notification.Rows(0), dsData.NotificationsRow)
Dim forwardURL As String = notificationrow.ForwardLocation
End If
编辑:在上面的代码中,我最初只有notification.Rows(0)
。这将生成一个DataRow对象,但不会强类型化。您需要执行我添加的CType
才能使用自定义属性ForwardLocation
。
答案 1 :(得分:1)
您永远不会将notificationrow
设置为任何内容。你的意思是这样设置吗?
Dim notificationrow As dsData.NotificationsRow = CType(notification.Rows(0), dsData.NotificationsRow)