Results 1 to 4 of 4

Thread: [RESOLVED] Fill array problem

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    32

    Resolved [RESOLVED] Fill array problem

    Hello Guru's,

    I need to fill an array with whole column from database table, so i have used reader to read the values from that column, but can not figure out how to put them into aray (code below doesn't work)
    Code:
    Dim aObject As Object
    Dim i As Integer=0
    Dim cmd As New SqlServerCe.SqlCeCommand(aQuery, aConnection)
    Dim aReader As SqlServerCe.SqlCeDataReader
    
    While aReader.Read()
    aObject(i) = aReader("ID")  ' ID is column name
    i += 1
    End While
    If i put those values into list box then everything runs ok
    Code:
    While aReader.Read()
    ListBox1.Items.Add(aReader("ID"))  ' ID is column name
    End While
    Please help me with that.

    Tank you in advance.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Fill array problem

    Well you haven't actually declared an array in your code snippet!

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Fill array problem

    You didn't declare 'aObject' as an array. Now, it's just one Object.
    If you use an array, then you have to specify how many items it will contain. Since you probably don't know that (since the data is coming from a database) a better option might be to use a List(Of Object) instead. You add items to it just like you add items to the ListBox, so you will probably find that a bit easier.
    Code:
    Dim objList As List(Of Object) = New List(Of Object)
    While aReader.Read
       objList.Add(aReader("ID"))
    End While
    If you really do need an array after this, for whatever reason, you can use the ToArray function of that list and it will be converted to an array for you:
    Code:
    Dim objArray() As Object = objList.ToArray()

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    32

    Re: Fill array problem

    Quote Originally Posted by NickThissen View Post
    You didn't declare 'aObject' as an array. Now, it's just one Object.
    If you use an array, then you have to specify how many items it will contain. Since you probably don't know that (since the data is coming from a database) a better option might be to use a List(Of Object) instead. You add items to it just like you add items to the ListBox, so you will probably find that a bit easier.
    Code:
    Dim objList As List(Of Object) = New List(Of Object)
    While aReader.Read
       objList.Add(aReader("ID"))
    End While
    If you really do need an array after this, for whatever reason, you can use the ToArray function of that list and it will be converted to an array for you:
    Code:
    Dim objArray() As Object = objList.ToArray()

    Thak you very much! Works as charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width