.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Loading a Listbox

1 REPLY 1
Reply
Message 1 of 2
Anonymous
345 Views, 1 Reply

Loading a Listbox

Everyone --
I'm using an OLEDB query to populate a form Listbox and I've been battaling this one for awhile now; doing OK with converting over to VB.Net but I cannot figure out how to pos data in the listbox control. Think I've tried pretty much everything.

{code}
Public Function INITF(ByVal DATAfile As String)
Dim errtrp As Boolean = False
If Dir(DATAfile, vbReadOnly) Like "" Then ' Error check: if the codefile is already open (or missing)
MsgBox("Error: The Layer Creator datafile: " & DATAfile & " cannot be located...")
Else
ed.WriteMessage(vbLf & "Companyname NCSLYR Initializing 02172010 ...")
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DATAfile & ";User ID=Admin;Password="
Dim ADOXCatalog As ADOX.Catalog = New ADOX.Catalog, oConn As OleDbConnection, oComm As OleDbCommand, oQuery As String
Dim oData As OleDbDataAdapter, resultSet As New DataSet

ADOXCatalog.let_ActiveConnection(connectionString)
oQuery = "SELECT * from NCS_Layers WHERE CAT is not null"
oConn = New OleDbConnection(connectionString) ' Instantiate the connectors
oComm = New OleDbCommand(oQuery, oConn) ' SQL statement or stored procedure to execute against a data source
oData = New OleDbDataAdapter(oQuery, oConn) ' Commands & database connection to fill / update the DataSet.
Try
oConn.Open() ' Opens connection with the properties set by ConnectionString
oComm.ExecuteNonQuery() ' Executes a SQL statement against a connection object.
' Adds or refreshes rows in DataSet to match those in the data source using the DataSet and DataTable names.
oData.Fill(resultSet, "NCS_LAYERS")
Dim reader As OleDbDataReader = oComm.ExecuteReader() ' Reading a forward-only stream of data rows from a data source
Dim Values(reader.FieldCount) As Object ' Gets the number of columns in the current row
CatListBox.MultiColumn = False ' Set the ListBox to display items in single column
CatListBox.SelectionMode = Windows.Forms.SelectionMode.One
ed.WriteMessage(" OLEDB Connection Open ")

Dim txt As String, z As Integer ' this doesn't work ...
Do While reader.Read
reader.GetValues(Values)
txt = reader.Item(0).ToString
For z = 1 To reader.FieldCount - 1
txt &= vbTab & reader.Item(z).ToString
Next z
CatListBox.Items.Add(txt)
Loop ' MsgBox(txt) - txt does have a value

' While reader.Read() ' Advances the OleDbDataReader to the next record
' reader.GetValues(Values) ' Gets all the attribute columns in the current row.
' End While

CatListBox.DisplayMember.ToString()
CatListBox.BeginUpdate() ' Shutdown the painting of the ListBox as items are added.
' CatListBox.Items.Add(Values) ' Does not work

For Each i In Values ' If Not i Is "" Then
If Not Len(i) <= 1 Then
ed.WriteMessage(vbLf & "Values Loop: " & i) ' this works...(@ the command prompt)...
CatListBox.Items.Add(i.ToString()) ' ...so I would think this would load the control...
End If
Next i
ed.WriteMessage(vbLf) ' advance prompt after last zero-string

' CatListBox.DataSource = Values - this doesn't do anything either...
'For x = 1 To 12 - CatListBox.Items.Add("Item" & x.ToString()) 'Next x ' this blows up acad ...

CatListBox.EndUpdate()

reader.Close()
oConn.Close()
Catch ex As Exception
ed.WriteMessage(" Error --> Can not open ODBC connection ! ")
Finally ' Dispose the connector objects
If Not (oConn Is Nothing) Then
oConn.Dispose()
oConn = Nothing
End If
If Not (oComm Is Nothing) Then
oComm.Dispose()
oComm = Nothing
End If
End Try
End If
Return True ' return success (and get around warning)
End Function
{code}

I'm sure I'm missing something fundamental and any help is very much appreciated.

Jim ... Edited by: Jim.Brinkmeyer@Otak.Com on Feb 17, 2010 11:37 PM
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: Anonymous

If you're adding strings to ListBox, then you shouldn't set the
DisplayMember or ValueMember properties.

The difficulty you have is largely a result of writing spaghetti
code, and trying to load the listbox multiple times, in a single
sub.

Throw it out, and start over from scratch.

First write a sub that takes an array of strings, and puts it in
the listbox.

Test the sub with a simple array of strings that you create in code,
to verify that the sub is doing what it should, and if it is, then pass
it the strings you read from the datasource.

Don't pile a bunch of code into a single sub and then wonder
why it isn't working, because the way you're doing it makes it
very hard to find that out.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message
news:6339244@discussion.autodesk.com...
Everyone --
I'm using an OLEDB query to populate a form Listbox and I've been battaling this
one for awhile now; doing OK with converting over to VB.Net but I cannot figure
out how to pos data in the listbox control. Think I've tried pretty much
everything.

{code}
Public Function INITF(ByVal DATAfile As String)
Dim errtrp As Boolean = False
If Dir(DATAfile, vbReadOnly) Like "" Then '
Error check: if the codefile is already open (or missing)
MsgBox("Error: The Layer Creator datafile: " & DATAfile & "
cannot be located...")
Else
ed.WriteMessage(vbLf & "Companyname NCSLYR Initializing 02172010
...")
Dim connectionString As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DATAfile & ";User
ID=Admin;Password="
Dim ADOXCatalog As ADOX.Catalog = New ADOX.Catalog, oConn As
OleDbConnection, oComm As OleDbCommand, oQuery As String
Dim oData As OleDbDataAdapter, resultSet As New DataSet

ADOXCatalog.let_ActiveConnection(connectionString)
oQuery = "SELECT * from NCS_Layers WHERE CAT is not null"
oConn = New OleDbConnection(connectionString) '
Instantiate the connectors
oComm = New OleDbCommand(oQuery, oConn) ' SQL
statement or stored procedure to execute against a data source
oData = New OleDbDataAdapter(oQuery, oConn) '
Commands & database connection to fill / update the DataSet.
Try
oConn.Open()
' Opens connection with the properties set by ConnectionString
oComm.ExecuteNonQuery() '
Executes a SQL statement against a connection object.
' Adds or refreshes rows in DataSet to match those in the
data source using the DataSet and DataTable names.
oData.Fill(resultSet, "NCS_LAYERS")
Dim reader As OleDbDataReader = oComm.ExecuteReader() '
Reading a forward-only stream of data rows from a data source
Dim Values(reader.FieldCount) As Object
' Gets the number of columns in the current row
CatListBox.MultiColumn = False '
Set the ListBox to display items in single column
CatListBox.SelectionMode = Windows.Forms.SelectionMode.One
ed.WriteMessage(" OLEDB Connection Open ")

Dim txt As String, z As Integer
' this doesn't work ...
Do While reader.Read
reader.GetValues(Values)
txt = reader.Item(0).ToString
For z = 1 To reader.FieldCount - 1
txt &= vbTab & reader.Item(z).ToString
Next z
CatListBox.Items.Add(txt)
Loop
' MsgBox(txt) - txt does have a value

' While reader.Read()
' Advances the OleDbDataReader to the next record
' reader.GetValues(Values) '
Gets all the attribute columns in the current row.
' End While

CatListBox.DisplayMember.ToString()
CatListBox.BeginUpdate() '
Shutdown the painting of the ListBox as items are added.
' CatListBox.Items.Add(Values) '
Does not work

For Each i In Values
' If Not i Is "" Then
If Not Len(i) <= 1 Then
ed.WriteMessage(vbLf & "Values Loop: " & i) '
this works...(@ the command prompt)...
CatListBox.Items.Add(i.ToString())
' ...so I would think this would load the control...
End If
Next i
ed.WriteMessage(vbLf) '
advance prompt after last zero-string

' CatListBox.DataSource = Values - this doesn't do anything
either...
'For x = 1 To 12 - CatListBox.Items.Add("Item" &
x.ToString()) 'Next x ' this blows up acad ...

CatListBox.EndUpdate()

reader.Close()
oConn.Close()
Catch ex As Exception
ed.WriteMessage(" Error --> Can not open ODBC connection !
")
Finally
' Dispose the connector objects
If Not (oConn Is Nothing) Then
oConn.Dispose()
oConn = Nothing
End If
If Not (oComm Is Nothing) Then
oComm.Dispose()
oComm = Nothing
End If
End Try
End If
Return True
' return success (and get around warning)
End Function
{code}

I'm sure I'm missing something fundamental and any help is very much
appreciated.

Jim ...

Edited by: Jim.Brinkmeyer@Otak.Com on Feb 17, 2010 11:37 PM

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report