By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

Object reference not set to an instance of an object. For Loop

maylortaylor
P: 72
Expand|Select|Wrap|Line Numbers
  1. 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
  2.         'Code to load user criteria from database
  3.         Dim ordertype As String
  4.         If _status = "Online" Then
  5.             ordertype = "Online"
  6.         ElseIf _status = "Tax Cert Call" Then
  7.             ordertype = "Call"
  8.         End If
  9.  
  10.  
  11.  
  12.         Dim TaxConnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ConfigurationManager.AppSettings("Database")
  13.         Dim dbConnection As OleDbConnection = New OleDbConnection(TaxConnStr)
  14.         Try
  15.  
  16.             Dim queryString As String
  17.  
  18.             queryString = "Select Username, Amount, Rank FROM UserCriteria "
  19.             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 & "';"
  20.  
  21.  
  22.             Dim dbCommand As OleDbCommand = New OleDbCommand
  23.             dbCommand.CommandText = queryString
  24.             dbCommand.Connection = dbConnection
  25.             Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter
  26.             dataAdapter.SelectCommand = dbCommand
  27.             Dim dataSet As DataSet = New DataSet
  28.             dataAdapter.Fill(dataSet)
  29.  
  30.  
  31.             If dataSet.Tables(0).Rows.Count >= 1 Then
  32.                 FindUserByCriteria = dataSet.Tables(0)
  33.             End If
  34.  
  35.  
  36.             Console.WriteLine(vbCrLf)
  37.             For i = 0 To FindUserByCriteria.Rows.Count - 1
  38.                 If Not IsUserOnline(FindUserByCriteria.Rows(i).Item("UserName")) Then
  39.                     FindUserByCriteria.Rows(i).Delete()
  40.                 End If
  41.             Next
  42.             FindUserByCriteria.AcceptChanges()
  43.  
  44.  
  45.         Catch ex As Exception
  46.             Console.WriteLine(ex.Message)
  47.             myLogger.Log(ex.Message)
  48.             SendMail(ex.Message)
  49.         Finally
  50.             dbConnection.Close()
  51.         End Try
  52.  
  53.     End Function
So, i get the "Object reference not set to an instance of an object." error at the
Expand|Select|Wrap|Line Numbers
  1. For i = 0 to FindUserByCriteria.rows.count - 1
line. I swear this was working for me not just 3 days ago...not sure what has changed in my code recently to make this error pop up. Any help would be nice.
May 8 '13 #1
Share this Question
Share on Google+
1 Reply


100+
P: 158
Hey maylortaylor, you don't have error in line number 32 in your code? 'FindUserByCriteria' is a method. But you're assigning a data-table to it.
May 9 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.