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
153 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11931722
the more likely problem was the loop inside the while loop.
0
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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