using a data reader to add items to an arraylist
-
I'm trying to use a data reader to add items to an arraylist I've tried several approaches - my code always reads the database correctly ( I can see the values it finds) but then I get an exception saying Object reference not set to an instance of an object. My original ideas were something like this: Dim IdNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader While (reader.Read()) m_ID = reader("id") IdNums.Add(m_ID) End while OR Dim IDNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader Dim i As Integer = 0 While (reader.Read()) IdNums.Add(reader.GetValue(i)) End while I thought the error message meant I had to create an object so I tried Dim IDNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader Dim IDobj As string = " " While (reader.Read()) IdNums.Add(IDobj) End while but same results Any thoughts Thanks
-
I'm trying to use a data reader to add items to an arraylist I've tried several approaches - my code always reads the database correctly ( I can see the values it finds) but then I get an exception saying Object reference not set to an instance of an object. My original ideas were something like this: Dim IdNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader While (reader.Read()) m_ID = reader("id") IdNums.Add(m_ID) End while OR Dim IDNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader Dim i As Integer = 0 While (reader.Read()) IdNums.Add(reader.GetValue(i)) End while I thought the error message meant I had to create an object so I tried Dim IDNums as arraylist Dim command As OleDbCommand = con.CreateCommand() command.CommandText = "SELECT Id from Candidates " Dim reader As IDataReader = command.ExecuteReader Dim IDobj As string = " " While (reader.Read()) IdNums.Add(IDobj) End while but same results Any thoughts Thanks
ssbelfast wrote:
Dim IDNums as arraylist
Try using the "New" keyword as in
Dim IDNums as New ArrayList
. -
ssbelfast wrote:
Dim IDNums as arraylist
Try using the "New" keyword as in
Dim IDNums as New ArrayList
.