2013-05-08 61 views
0
Function FindUserByCriteria(ByVal _state As String, ByVal _county As String, ByVal _status As String, ByVal _client As String, ByVal _department As String, ByVal _ordernumber As String) As DataTable 
    'Code to load user criteria from database 
    Dim ordertype As String 
    If _status = "Online" Then 
     ordertype = "Online" 
    ElseIf _status = "Tax Cert Call" Then 
     ordertype = "Call" 
    End If 



    Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database") 
    Dim dbConnection As OleDbConnection = New OleDbConnection(TaxConnStr) 
    Try 

     Dim queryString As String 

     queryString = "Select Username, Amount, Rank FROM UserCriteria " 
     queryString += "WHERE UserCriteria.State = '" & _state & "' AND UserCriteria.County = '" & _county & "' AND UserCriteria.Status = '" & _status & "' AND UserCriteria.Client = '" & _client & "' AND UserCriteria.Department = '" & _department & "' AND UserCriteria.OrderNumber = '" & _ordernumber & "';" 


     Dim dbCommand As OleDbCommand = New OleDbCommand 
     dbCommand.CommandText = queryString 
     dbCommand.Connection = dbConnection 
     Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter 
     dataAdapter.SelectCommand = dbCommand 
     Dim dataSet As DataSet = New DataSet 
     dataAdapter.Fill(dataSet) 


     If dataSet.Tables(0).Rows.Count >= 1 Then 
      FindUserByCriteria = dataSet.Tables(0) 
     End If 


     Console.WriteLine(vbCrLf) 
     For i = 0 To FindUserByCriteria.Rows.Count - 1 
      If Not IsUserOnline(FindUserByCriteria.Rows(i).Item("UserName")) Then 
       FindUserByCriteria.Rows(i).Delete() 
      End If 
     Next 
     FindUserByCriteria.AcceptChanges() 


    Catch ex As Exception 
     Console.WriteLine(ex.Message) 
     myLogger.Log(ex.Message) 
     SendMail(ex.Message) 
    Finally 
     dbConnection.Close() 
    End Try 

End Function 

所以,我得到了「對象引用未設置爲對象的實例」。錯誤在未將對象引用設置爲對象的實例。 For Loop

For i = 0 To FindUserByCriteria.Rows.Count - 1 

行。我發誓這不只是3天前...我不知道我的代碼最近有什麼變化,使這個錯誤彈出。你能幫忙的話,我會很高興。

+0

當'dataSet.Tables(0).Rows.Count = 0'時會發生什麼? – ja72 2013-05-08 23:44:39

回答

0

你需要扭轉的for循環

For i = FindUserByCriteria.Rows.Count - 1 to 0 step -1 

你需要向後刪除,否則你將到達的索引已經被刪除

,或者你只是需要把使用任何FindUserByCriteria的,如果內它在哪裏設置

If dataSet.Tables(0).Rows.Count >= 1 Then 
     FindUserByCriteria = dataSet.Tables(0) 


     Console.WriteLine(vbCrLf) 
     For i = 0 To FindUserByCriteria.Rows.Count - 1 
      If Not IsUserOnline(FindUserByCriteria.Rows(i).Item("UserName")) Then 
       FindUserByCriteria.Rows(i).Delete() 
      End If 
     Next 
     FindUserByCriteria.AcceptChanges() 
    End If 
相關問題