Solved

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

Posted on 2004-08-30
10
149 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:pgilfeather
  • 4
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
ptakja earned 250 total points
ID: 11931699
The problem lies in this line:

ReDim MyArray(I)

ReDim CLEARS the contents of the array.  What you want is this:

ReDim Preserve MyArray(I)


That will preserve your array's previous contents while making it bigger.

Try that.

Jeff
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 250 total points
ID: 11931700
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
MyArray(I) = objreader.GetInt32(0)
Next
Loop
     
lblCount.Text = MyArray(2) 'Display one of the values of the array
objConnection2.close()    
End Sub
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931718
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
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931722
the more likely problem was the loop inside the while loop.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Expert Comment

by:ptakja
ID: 11931727
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

0
 
LVL 14

Expert Comment

by:ptakja
ID: 11931744
Whoops again...make that:

Dim ii As Integer = 0
Do While objreader.Read()
        MyArray(ii) = objreader.GetInt32(0)
        ii += 1            'Increment ii counter
Loop
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931761
isnt that what I said ?
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11931780
Hmmm...guess I didn't refresh my browser enough!  8-|

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now