Link to home
Start Free TrialLog in
Avatar of pgilfeather
pgilfeather

asked on

I am trying to pull a column of ID numbers into the array and then be able to display an arbitrary array value.

The following code wont run it keeps assigning 0 to lblCount.Text. Any Ideas?
I am trying to pull a column of ID numbers into the array and then be able to display an arbitrary array value.


Sub createArray(ByVal sender As System.Object, ByVal e As System.EventArgs)
      
Dim cnn2 As String = (ConfigurationSettings.AppSettings("SomeWebSite"))
Dim objConnection2 as new sqlConnection(cnn2)
objConnection2.Open()
Dim objCommand2 as new SqlCommand("procThatPullsIDsFromTable", ObjConnection2)
objCommand2.CommandType = CommandType.StoredProcedure
objCommand2.Connection = ObjConnection2
Dim objreader As SqlDataReader = objCommand2.ExecuteReader()
                
ReDim MyArray(countIDs)   'countIDs is set in a different section of code
      
Do While objreader.Read()
For I = 1 to countIDs
ReDim MyArray(I)
MyArray(I) = objreader.GetInt32(0)
Next
Loop
      
lblCount.Text = MyArray(2) 'Display one of the values of the array
objConnection2.close()      
End Sub
ASKER CERTIFIED SOLUTION
Avatar of ptakja
ptakja
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually he redims the array to its maximum size before ...

Sub createArray(ByVal sender As System.Object, ByVal e As System.EventArgs)
     
Dim cnn2 As String = (ConfigurationSettings.AppSettings("SomeWebSite"))
Dim objConnection2 as new sqlConnection(cnn2)
objConnection2.Open()
Dim objCommand2 as new SqlCommand("procThatPullsIDsFromTable", ObjConnection2)
objCommand2.CommandType = CommandType.StoredProcedure
objCommand2.Connection = ObjConnection2
Dim objreader As SqlDataReader = objCommand2.ExecuteReader()
             
ReDim MyArray(countIDs)   'countIDs is set in a different section of code
dimi as integer i = 0    
Do While objreader.Read()
MyArray(I) = objreader.GetInt32(0)
 i+= 1
Loop
     
lblCount.Text = MyArray(2) 'Display one of the values of the array
objConnection2.close()    
End Sub
the more likely problem was the loop inside the while loop.
Whoops.  Lets try that again.  REMOVE the ReDim from inside the loop.  You have already sized the array outside the loop.

Do While objreader.Read()
        MyArray(I) = objreader.GetInt32(0)
Loop

Whoops again...make that:

Dim ii As Integer = 0
Do While objreader.Read()
        MyArray(ii) = objreader.GetInt32(0)
        ii += 1            'Increment ii counter
Loop
isnt that what I said ?
Hmmm...guess I didn't refresh my browser enough!  8-|