none
How to do a 'For each' with databound listbox?

    Question

  • Hello good people...

    I have the below routine which works perfectly with a listox with items entered manually (i.e. (collection)). However, it will not run with a listbox containing databound items from a SQL database.

    How would I go about getting this to work with a listbox is populated through a datasource?

    Many thanks!

    ----------------------------------------------------

     For Each i As String In AccessoarerListBox.Items

                Dim path As String = "g:\produktbilder\" & i

                Try
                    ' Determine whether the directory exists.
                    If Directory.Exists(path) Then
                        'MsgBox("Folder already exists")
                        Exit Sub
                    End If

                    ' Try to create the directory.
                    Dim di As DirectoryInfo = Directory.CreateDirectory(path)
                    'MsgBox("Folder created")


                Catch a As Exception
                    Console.WriteLine("The process failed: {0}.", a.ToString())
                End Try

            Next

    ----------------------------------------------------

    Sunday, March 19, 2017 9:21 AM

All replies

  • Tried

    For Each dataRow As DataRowView In AccessoarerListBox.DataSource

    ?


    Sunday, March 19, 2017 9:38 AM
  • Tried ListBox.DataSource as well as ListBox.DataSource.ToString. First one renders an error second one seems to execute, but doesnt render any result (i.e. no folder(s) created)

    Sunday, March 19, 2017 9:51 AM
  • Hello,

    When you say the data is coming from a SQL database you did not mention how you loaded the ListBox. How it was loaded makes a difference how I can respond.

    Did you use the data wizards (selected data from the "Data Source" window and setup the ListBox or perhaps another way? Bottom line is it could be populated with a BindingSource that has a DataSource of a DataSet that the ListBox uses a DataTable from there or it could be simply a DataTable assigned to the DataSource of the ListBox.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 19, 2017 10:37 AM
    Moderator
  • Thank you Mr Parrott...I used this code to get it to work:

    -----------------------------------------------

    For Each dataRow As DataRowView In AccessoarerListBox.Items

                Dim value As String = dataRow.Item("Artikelnr")

                Dim path As String = "G:\Produktbilder\" & value

                Try

                    Dim di As DirectoryInfo = Directory.CreateDirectory(path)

                Catch a As Exception
                    Console.WriteLine("The process failed: {0}.", a.ToString())
                End Try

            Next

    -----------------------------------------------

    Much obliged!

    Sunday, March 19, 2017 10:39 AM
  • Thank you Mr Parrott...I used this code to get it to work:

    -----------------------------------------------

    For Each dataRow As DataRowView In AccessoarerListBox.Items

                Dim value As String = dataRow.Item("Artikelnr")

                Dim path As String = "G:\Produktbilder\" & value

                Try

                    Dim di As DirectoryInfo = Directory.CreateDirectory(path)

                Catch a As Exception
                    Console.WriteLine("The process failed: {0}.", a.ToString())
                End Try

            Next

    -----------------------------------------------

    Much obliged!

    The code may work but it's not wise to have VB.NET do the conversion from Object to String, best to use Option Strict On (where you seem to have Option Strict Off set) as working this way will bite you sooner or later down the road.

    This would be strong typed

    Dim path As String = ""
    CType(AccessoarerListBox.DataSource, DataTable) _
        .AsEnumerable.ToList.ForEach(
        Sub(row)
            path = IO.Path.Combine("G:\Produktbilder",
                                    row.Field(Of String)("Artikelnr"))
            ' continue logic
        End Sub)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 19, 2017 10:55 AM
    Moderator
  • Hi Ricardo,

    Since your problem has been solved by yourself, please click "Mark as answer" to let more community members to know this answer.

    Thanks for your understanding.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 8:07 AM
    Moderator
  • Thank you Mr Parrott...I used this code to get it to work:

    -----------------------------------------------

    For Each dataRow As DataRowView In AccessoarerListBox.Items

                Dim value As String = dataRow.Item("Artikelnr")

                Dim path As String = "G:\Produktbilder\" & value

                Try

                    Dim di As DirectoryInfo = Directory.CreateDirectory(path)

                Catch a As Exception
                    Console.WriteLine("The process failed: {0}.", a.ToString())
                End Try

            Next

    -----------------------------------------------

    Much obliged!

    That means that your table is not bound, but that you simply have set individually each item. A bound listbox has no items, that is when those are set as string. 

    Try to do it like is advices. 

    I write it mainly for those who find this question when it is marked as answer. 


    Success
    Cor

    Monday, March 20, 2017 12:14 PM
  • @Cherry Blue,

    Please don't propose this kind of replies when you have not really investigated it, but simply because of somebody writes "it works". 


    Thanks
    Cor



    Monday, March 20, 2017 12:16 PM