Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
ptakja earned 1000 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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