Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27201

VS 2012 Object Error when trying to run a query that involves a Date Time Picker

$
0
0
Hi,

I have the following code that runs on Load and that reads from text files in a directory and stores the info in an Array. This then loads the relevant info into a listbox based on the date from a Date Time picker

Code:

Dim loaddate As String = Calendar.Value.ToString("dd/MM/yy")

        ReDim AllDetail(0 To 0)
        numfiles = 0

        lstPlanned.Items.Clear()

      Dim allfiles = lynxin.GetFiles("*.txt")
        ReDim AllDetails(allfiles.Count)

        lstProgress.Items.Clear()

        lstPlanned.Items.Add("No Jobs Planned Today!")
        lstPlanned.Enabled = False

        For Each txtfi In (allfiles)
            Dim allLines() As String = File.ReadAllLines(txtfi.FullName)

            AllDetails(numfiles) = New FileDetail()

            AllDetails(numfiles).uPath = Microsoft.VisualBasic.Left((txtfi.FullName), Len(txtfi.FullName) - 4)

            AllDetails(numfiles).uFile = Path.GetFileNameWithoutExtension(txtfi.Name)

            Dim line = allLines.Where(Function(x) (x.StartsWith("unitname="))).SingleOrDefault()
            If line IsNot Nothing Then
                AllDetails(numfiles).uName = line.Split("="c)(1)
            End If

            line = allLines.Where(Function(x) (x.StartsWith("unitcode="))).SingleOrDefault()
            If line IsNot Nothing Then
                AllDetails(numfiles).uCode = line.Split("="c)(1)
            End If

            line = allLines.Where(Function(x) (x.StartsWith("opername="))).SingleOrDefault()
            If line IsNot Nothing Then
                AllDetails(numfiles).uOps = line.Split("="c)(1)
            End If

            line = allLines.Where(Function(x) (x.StartsWith("plandate="))).SingleOrDefault()
            If line IsNot Nothing Then
                AllDetails(numfiles).uPlan = line.Split("="c)(1)
            End If

            line = allLines.Where(Function(x) (x.StartsWith("cliecode="))).SingleOrDefault()
            If line IsNot Nothing Then
                AllDetails(numfiles).uClient = line.Split("="c)(1)
            End If

            If AllDetails(numfiles).uPlan = loaddate Then

                lstPlanned.Items.Remove("No Jobs Planned Today!")
                lstPlanned.Enabled = True
                lstPlanned.Items.Insert(0, AllDetails(numfiles).uName & " - " & AllDetails(numfiles).uCode & " - " & AllDetails(numfiles).uOps)
                numfiles = numfiles + 1

            End If

        Next

        Dim RootDir As New System.IO.DirectoryInfo(aMailbox)
        For Each Dir As IO.DirectoryInfo In RootDir.GetDirectories
            Dim item = Dir.Name
            lstProgress.Items.Add(item)
            If lstPlanned.Items.Contains(item) Then
                lstPlanned.Items.Remove(item)
            End If
        Next




    End Sub

I then have this code that woks on a double click event and move the string from one listbox to another along with doing some unziping and copying files

Code:

Dim n As Integer
        Dim i As Integer = lstPlanned.SelectedIndex

        If lstPlanned.SelectedItems.Count = 0 Then Exit Sub

        For n = 0 To UBound(AllDetails)

            If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lstPlanned.SelectedItem Then

                If Not My.Computer.FileSystem.DirectoryExists(zMailbox & AllDetails(n).uFile) Then

                    MsgBox("No files located for " & vbNewLine & (AllDetails(n).uName & " (" & AllDetails(n).uCode) & ")" & vbNewLine & " " & vbNewLine & "Please try another...", MsgBoxStyle.OkOnly, "Lynx Control Panel")
                    Exit Sub
                End If

                System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)

                For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
                    If File.Exists(f) Then
                        File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                    End If
                Next

                For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.ini", SearchOption.AllDirectories)
                    If File.Exists(f) Then
                        File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                    End If
                Next

                For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.txt", SearchOption.AllDirectories)
                    If File.Exists(f) Then
                        File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
                    End If
                Next

                lstProgress.Items.Add(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
                If i >= 0 And i < lstPlanned.Items.Count Then
                    lstPlanned.Items.RemoveAt(i)


                End If
                Exit Sub
            End If
        Next

    End Sub

The bit of code I'm having an issue with is the following. Basically this works on a single click event and runs a query based on the selected item and returns values from the database into a 3rd listbox.

Code:

Dim da As New OleDb.OleDbDataAdapter("", "")
        Dim dt As New DataTable
        Dim conn As String
        Dim n As Integer

        For n = 0 To UBound(AllDetails)

            If lstProgress.SelectedItems.Count = 0 Then Exit Sub

            If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lstProgress.SelectedItem Then

                Dim eSearch As String = AllDetails(n).uCode

                conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.aClients & ""

                da.SelectCommand.Connection.ConnectionString = conn
                da.SelectCommand.CommandText = "SELECT UCASE(DocName) as DocNm FROM Documents WHERE (UnitCode = " & eSearch & ") AND (Required = True)"
                da.Fill(dt)
                lstRequired.DataSource = dt
                lstRequired.DisplayMember = "DocNm"
                lstRequired.Refresh()
                Exit Sub
            End If
        Next
    End Sub

All these codes work as they should and all variables are declared however in the final code i come across a "Object reference not set to an instance of an object." error if I single click on an item that has a different date within the Array than what the Date Time picker is showing. If i change the the DTP to match the date then the click event works.

So, what i need is to ignore the date form the DTP and the Array when trying to perform the single click event.

Any ideas???

Thanks

Viewing all articles
Browse latest Browse all 27201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>