Question

I'm using the below to populate an arraylist with the contents of a directory.

Is it possible to use the result of the arraylist as a filter of items to exclude from an OLEDB query? If not can someone point me in the direction of a better alternative - many thanks

  Dim NodeFile As New IO.DirectoryInfo(tempMail & tvProgress.SelectedNode.FullPath)
            Dim NodeList As IO.FileInfo() = NodeFile.GetFiles("*.*")
            Dim report As New ArrayList()
            For Each NodeExcl In NodeList
                report.Add(Path.GetFileName(NodeExcl.Name))
            Next

Query code

     Try

        Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.aClients & ""
        Dim n As Integer


        For n = 0 To UBound(AllDetails)
            Dim NodeFile As New IO.DirectoryInfo(tempMail & tvProgress.SelectedNode.FullPath)
            Dim NodeList As IO.FileInfo() = NodeFile.GetFiles("*.*")
            Dim report As New ArrayList()
            For Each NodeExcl In NodeList
                report.Add(Path.GetFileName(NodeExcl.Name))
            Next

            '' Need to exclude arraylist from query

            If tvProgress.Nodes.Count = 0 Then Exit Sub

            If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = e.Node.Text Then
                lstRequired.DataSource = Nothing
                lstRequired.DataBindings.Clear()

                Dim eSearch As String = AllDetails(n).uCode
                Dim fSearch As String = AllDetails(n).uOps

                da.SelectCommand.Connection.ConnectionString = conn
                da.SelectCommand.CommandText = "SELECT Documents.DocName FROM Documents WHERE (Documents.UnitCode = ?) AND (Documents.OpName = ?) AND Documents.Required = True ORDER BY DocName"
                da.SelectCommand.Parameters.AddWithValue("@p1", eSearch)
                da.SelectCommand.Parameters.AddWithValue("@p2", fSearch)
                da.Fill(dt)
                dt.Rows.Add("Add Additional Requirement")
                lstRequired.DataSource = dt
                lstRequired.DisplayMember = StrConv("DocName", VbStrConv.ProperCase)
                lstRequired.Refresh()

                Dim dl As DataTable = CType(lstRequired.DataSource, DataTable)
                Using sR = New IO.StreamReader(tFiles & UCase("ProgExcluded.txt"))
                    While (sR.Peek() > -1)
                        Dim rows() = dl.Select("DocName = '" + sR.ReadLine + "'")
                        For Each row In rows
                            row.Delete()
                        Next
                        dl.AcceptChanges()
                    End While
                End Using

            End If
        Next

        Exit Sub


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

I've had a bit of a think and now adapted some of the code to

            Dim NodeFile As New IO.DirectoryInfo(tempMail & tvProgress.SelectedNode.FullPath)
            Dim NodeList As IO.FileInfo() = NodeFile.GetFiles("*.*")
            Dim report As New ArrayList()

            For Each NodeExcl In NodeList
                report.Add(Path.GetFileName(NodeExcl.Name))
            Next
            Dim newreport As String = String.Join(",", report.ToArray())

with the idea of adding a multi string parameter in the where cause. Still trying to work out if that will work

Was it helpful?

Solution 2

Cracked it..

Had to do a bit of concatenation with the string for the WHERE clause but it works a treat

Dim da As New OleDb.OleDbDataAdapter("", "")
        Dim dt As New DataTable
        Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.aClients & ""
        Dim n As Integer


        For n = 0 To UBound(AllDetails)
            If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = e.Node.Text Then

                Dim NodeFile As New IO.DirectoryInfo(Path.Combine(tempMail, tvProgress.SelectedNode.FullPath))
                Dim reports = NodeFile.EnumerateFiles().Select(Function(f) Path.GetFileName(f.Name)).ToList()

                Dim newreport As String = String.Join("' AND Documents.DocName <> '", reports.ToArray())

                If tvProgress.Nodes.Count = 0 Then Exit Sub

                Dim eSearch As String = AllDetails(n).uCode
                Dim fSearch As String = AllDetails(n).uOps
                Dim gsearch As String = "'" & newreport & "'"

                da.SelectCommand.Connection.ConnectionString = conn
                da.SelectCommand.CommandText = "SELECT Documents.DocName FROM Documents WHERE (Documents.UnitCode = ?) AND (Documents.OpName = ?) AND (Documents.DocName <> " & gsearch & ") AND Documents.Required = True ORDER BY DocName"
                da.SelectCommand.Parameters.AddWithValue("@p1", eSearch)
                da.SelectCommand.Parameters.AddWithValue("@p2", fSearch)
                da.Fill(dt)

                dt.Rows.Add("Add Additional Requirement")
                lstRequired.DataSource = dt
                lstRequired.DisplayMember = "DocName"
                lstRequired.Refresh()

OTHER TIPS

If I take your first block of code and simplify it to

Dim NodeFile As New IO.DirectoryInfo( _
    Path.Combine(tempMail, tvProgress.SelectedNode.FullPath))
Dim reports = NodeFile.EnumerateFiles().Select(Function(f) _
    Path.GetFileName(f.Name)).ToList()

You'll see that reports is a List(Of String). Welcome to the modern world.


If you need help to rewrite a swaith of legacy code please break it down into questions that will be useful to other people.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top