Asp.net 数据库连接上的VB.net空引用
我知道我在这里是个白痴,我就是搞不懂。但我正试图从vb.net数据库中获取一些数据。由于对象引用未设置为对象错误的实例,它正在崩溃。在代码运行之前,它说变量在设置之前就已经被使用了,但是我不知道怎么做。代码:Asp.net 数据库连接上的VB.net空引用,asp.net,sql-server,vb.net,Asp.net,Sql Server,Vb.net,我知道我在这里是个白痴,我就是搞不懂。但我正试图从vb.net数据库中获取一些数据。由于对象引用未设置为对象错误的实例,它正在崩溃。在代码运行之前,它说变量在设置之前就已经被使用了,但是我不知道怎么做。代码: Private taNotifications As dsDataTableAdapters.NotificationsTableAdapter = New dsDataTableAdapters.NotificationsTableAdapter Dim notificat
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上,作为String=notificationrow.ForwardLocation
问题是您从未在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
编辑:在上面的代码中,我最初只有
通知。行(0)
。这将生成一个DataRow对象,但它不是强类型的。您需要执行我添加的CType
,以便使用自定义属性ForwardLocation
问题在于您从未在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
编辑:在上面的代码中,我最初只有
通知。行(0)
。这将生成一个DataRow对象,但它不是强类型的。您需要执行我添加的CType
,以便使用自定义属性ForwardLocation
您从未将notificationrow
设置为任何内容。你的意思是这样设置的吗
Dim notificationrow As dsData.NotificationsRow = CType(notification.Rows(0), dsData.NotificationsRow)
您从未将
notificationrow
设置为任何内容。你的意思是这样设置的吗
Dim notificationrow As dsData.NotificationsRow = CType(notification.Rows(0), dsData.NotificationsRow)
非常感谢!真的很有用谢谢你!真的很有帮助