Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dataset clearing for apparently no reason....

Status
Not open for further replies.

herbally

Programmer
Jun 13, 2008
8
US
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:
Code:
Dim maxDate As Date = CDate(DsMembers.Members.Compute("MAX(LastUpdate)", Nothing))

Code:
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
Code:
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
 
I am not even going to try to figure out how you thought that this was the best way to deal with this...

You have the @lastupdate variable commented out.

Code:
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)

Why aren't you working with the database as an insert, then running a fresh select? are you not supposed to keep the new data?

-Sometimes the answer to your question is the hack that works
 
rofl....thanks for the response first of all!! Second, I started writing this application with no prior knowledge of programming except the hello world apps I wrote in QUICK BASIC in an "Intro to Computer Proramming" class. I know that's not ideal...it was something temporary that functioned so I moved on to bigger and better things.

That being said, I don't see where "@LastUpdate" is commented out?

also, the error is thrown before any lines relevant to "@LastUpdate" are run.

This is the line that throws the error:
Code:
Dim maxDate As Date = CDate(DsMembers.Members.Compute("MAX(LastUpdate)", Nothing))
 
oh and as far as not using an insert and a fresh select, I thought it would be less taxing on the database in a multi-user environment to just retrieve updated/added records rather than all 20000.
 
Code:
 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)

you get the max date out of the datagrid, then pass that value to the select. Ok, that makes a little more sense now.

You may just need to add a check to verify that the datagrid has data in it, before accessing it, so that when the form first loads, if there is no data, that it uses todays date, or yesterdays.


-Sometimes the answer to your question is the hack that works
 
but thats the thing...I'm using a database that already contains 20000 members and if I put the line that cause the error in the form_load event right after I fill the database it returns a date/time = Max(LastUpdate).

It only causes the error when I use the very same statement in he updateDataset method.

The only thing I can think of is that the ClientListener module is running on a different thread than the form, and it makes the call to the updateDataset method. I've been running through this code repeatedly and I just can't seem to find anything that might be the cause other than the fact that its being called from a different thread than the main form is running on.
 
Does this blow up when you first open the page?
If so, it because the the datatable doesn't have any results yet.

If not, but it errs when the the actual update occurs, you may once again want to try to check and see if the table has any data prior to pulling a value out.


Code:
DsMembers.Merge(dtUpdated)
I've never used this before, but should it be:
Code:
ds.Tables("Members").Merge(dtUpdatedData)

-Sometimes the answer to your question is the hack that works
 
in my first post I mentioned that the dataset is mysteriously cleared/empty when I call the updateDataset method from within the ClientListener module.

My problem is that I don't know why that dataset is empty since I fill it in the form_load event and I never clear it prior to the updateDataset method.

Furthermore, ds.t.merge isn't available in vb.net[2003], but ds.merge is. That's why I did it that way.
 
if it gets to the point where you can't recreate it in test, but it does it in live you have to start logging out values to the screen or a log file.
Write out the date, the number of affected rows. The time the update occurred. Write out the stack trace so you can see what the caller is.

-Sometimes the answer to your question is the hack that works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top