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
154 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 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
Technology Partners: 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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