Help with setting variables...

lidds

Well-known member
Joined
Nov 9, 2004
Messages
210
I have figured out how to produce a user defined combo box within a propertygrid after some serious time. But the problem I have now is that instead of me entering hard coded values, I wish to get the values from a database (not a problem there), but I am having a problem in defining the combo content varable.

See code examples and hopefully it will become clear:

The following code is my hard coded combo values, obviously produces a varible containing "Bob", "John" and "Dave"

Code:
Dim _States As String() = New String() {"Bob", "John", "Dave"}

The following code is just a snippet of code that opens the database and get values. But I dont know how to loop through the table values (myReader) and add each value to a variable in the same format as code snippet above.

Code:
Private Sub buildArea()
        [COLOR=DarkRed]comboValues[/color] = New String() {}

        Dim myConn As New OleDb.OleDbConnection("Provider=sqloledb;Data Source=(local);initial catalog=test;user id=admin;password=lidds")
        Dim myCmd As New OleDb.OleDbCommand("SELECT * from areaTbl", myConn)

        myConn.Open()
        Dim myReader As OleDb.OleDbDataReader = myCmd.ExecuteReader()

        Do While myReader.Read
            [COLOR=DarkRed] This is where I am having the problem need 
             to loop through table values and somehow append comboValues
             variable to same format as previous code[/COLOR]
        Loop
        myConn.Close()
    End Sub

Cheers

Simon
 
If i got the problem right you would need an array of strings from the datareader in the _States variable.
Is there any specific reason for going to DataReader ?
I would prefer a dataadapter to the reader.

Dim _States as string()
Dim dsValues as new dataset
Dim intCount as integer
Dim myConn As New OleDb.OleDbConnection("Provider=sqloledb;Data Source=(local);initial catalog=test;user id=admin;password=lidds")

Dim myCmd As New OleDb.OleDbCommand("SELECT * from areaTbl", myConn)

Dim adapter As New OleDbDataAdapter()
adapter.SelectCommand = myCmd
adapter.Fill(dsValues)
intCount = dsValues.Tables(0).Rows.Count
ReDim _States(intCount-1)

for i = 0 to intcount-1
_States(i) = dsValues.Tables(0).Rows(i).Item(0)
next

but if you want to use only datareader,
then
intCount = 0
Do While myReader.Read
- ReDim the string array every time with preserve
- set the value from the reader, myReader.Item(0)
Loop
 
Thanks for your help seems like it will do the trick, there is only one more problem. I have a vb class called listStates and within in that class is where I define the variable _States, the problem is that for me to be able to put the above code (myConn etc.) into this class without errors is by added a private sub within the class and added the database connection code in there, but it seems to be not running any of the code within the private sub. Obviously I am doing something very wrong and as I am new to vb it very likely that I am approaching this wrong.

Any help would be appreshiated

Cheers

Simon
 
Back
Top