herbally 0 Newbie Poster

Ok, I fill my dataset in the form_load event, and the datagrid loads up with the data fine. I can search it and update/add members.

When another machine on the network adds a member (haven't even tested an update yet) it sends a message to the machine in question telling it to update. The troubled machine receives this message on a separate thread utilizing my clientListener module. After negotiating the connection and receiving the update message it calls my "updateDataset" method which is on the main form.

For some reason, the original dataset is empty at this point and I can't understand why since I didn't clear it anywhere. Maybe its related to being in another thread? Any help would be much appreciated!

Code follows:

An InvalidCastException error occurs on the following line within the updateDataset method because DsMembers.Members is empty:

Dim maxDate As Date = CDate(DsMembers.Members.Compute("MAX(LastUpdate)", Nothing))
Public Sub updateDataset()
        '    DsMembers.Members.PrimaryKey = New DataColumn() {DsMembers.Members.Columns("TDL")}
        Try

            Dim dtUpdated As DataTable = DsMembers.Members.Clone()

            Dim adapter As New OleDbDataAdapter("SELECT Address, Barred, BarredReason, City, DateExpired, DateJoined, DOB, FirstName, LastName, MemberID, MiddleName, State, TDL, Zip, LastUpdate FROM Members WHERE LastUpdate > @LastUpdate", odbconMembers)
            Dim maxDate As Date = CDate(DsMembers.Members.Compute("MAX(LastUpdate)", Nothing))

            '    adapter.SelectCommand.Parameters("@LastUpdate").Value = maxDate 'Get only data added since the last query.adapter.Fill(table)

            '     adapter.SelectCommand.Parameters.Add("@LastUpdate", Date.MinValue)
            adapter.SelectCommand.Parameters.Add("@LastUpdate", maxDate)
            adapter.Fill(dtUpdated)

            DsMembers.Merge(dtUpdated)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
Public Module ClientListener
    Private Delegate Sub DelegateUpdateDataset()

    Public Sub Main()
        'Create a new listener on port 8893
        Dim Listener As New TcpListener(address, 8893)

        Console.WriteLine("About to initialize port.")
        Listener.Start()

        Console.WriteLine("Listenening for a connection...")
        '  Dim ClientNum As Integer
        Do
            Try
                'Wait for a connection request,
                'and return a TcpClient initialized for communication.
                Dim Client As TcpClient = Listener.AcceptTcpClient
                Console.WriteLine("Server: Connection accepted.")

                'Retrieve the network stream.
                Dim Stream As NetworkStream = Client.GetStream

                'Create a BinaryWriter for writing to the stream.
                Dim w As New BinaryWriter(Stream)

                'Create a BinaryReader for reading from the stream.
                Dim r As New BinaryReader(Stream)

                If r.ReadString() = ClientMessages.RequestConnect Then
                    w.Write(ServerMessages.AcknowledgeOK)
                    Console.WriteLine("Connection completed.")
                    If r.ReadString = ServerMessages.UpdateData Then
                        Try
                            Dim frmMemberMatic As New MemberMatic.frmMemberMatic
                            Call frmMemberMatic.updateDataset()
                            Console.WriteLine("Member updated.")
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try
                    End If
                    Do
                    Loop Until r.ReadString() = ClientMessages.Disconnect
                    Console.WriteLine()
                    Console.WriteLine("Disconnect request received.")
                    w.Write(ServerMessages.Disconnect)
                Else
                    Console.WriteLine("Could not complete connection")
                End If

                'Close the connection socket.
                Client.Close()
                Console.WriteLine("Connection Closed")

                'Close the underlying socket (stop listening for new requests).
                Listener.Stop()
                Console.WriteLine("Listener stopped.")

                'Create a new object to handle this connection.
                'ClientNum += 1
                ' Dim Handler As New ClientHandler(Client, "Client " & ClientNum.ToString)

                'Start this object working on another thread.
                'Dim HandlerThread As New System.Threading.Thread(AddressOf Handler.Start)
                'HandlerThread.IsBackground = True
                'HandlerThread.Start()


            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        Loop


        Console.ReadLine()
    End Sub


End Module
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.