Wierd thing data is there twice

Qbert

Active member
Joined
Jun 11, 2007
Messages
33
Location
Minnesota
Programming Experience
Beginner
i have a form with a list box on it and it displays records from the database. when i open the form it is fine. then if i close the form and reopen it the data is there twice. i dont know why. if anyone can look at it and maybe see where i am making my mistake that would be great.

it also wont delete the record. which is weird because it used to delete it.

thanks

here is the code

VB.NET:
Public Class ClientList
    Dim ClientIDArray() As Integer
    Dim ClientNameArray() As String

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ClientList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IanConnect.Open()

        Dim ClientList As New System.Data.OracleClient.OracleCommand

        ClientList.Connection = IanConnect
        ClientList.CommandText = "select * from clients order by client_name"

        Dim dr As System.Data.OracleClient.OracleDataReader = ClientList.ExecuteReader()
        Dim counter As Integer = 0

        While dr.Read()
            ClientListListBox.Items.Add(dr.Item(1))
            ReDim Preserve ClientIDArray(counter + 1)
            ReDim Preserve ClientNameArray(counter + 1)
            ClientIDArray(counter) = dr.Item(0)
            ClientNameArray(counter) = dr.Item(1)
            counter += 1
        End While

        IanConnect.Close()
    End Sub

    Private Sub EditClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles EditClientToolStripMenuItem.Click
        Dim FormB As New EditClient()

        If ClientListListBox.SelectedIndex = -1 Then
            MsgBox("Please select a client to edit")
        Else
            PassedClientID = ClientIDArray(ClientListListBox.SelectedIndex)
            EditClient.ShowDialog()
            'Me.Close()
        End If
    End Sub

    Private Sub DeleteClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles DeleteClientToolStripMenuItem.Click
        If ClientListListBox.SelectedIndex = -1 Then
            'do nothing
        Else
            PassedClientID = ClientIDArray(ClientListListBox.SelectedIndex)
            PassedClientName = ClientNameArray(ClientListListBox.SelectedIndex)

            MsgBox("Delete Client """ & PassedClientName & """?", MsgBoxStyle.YesNo)

            If MsgBoxResult.No Then
                'do nothing
            ElseIf MsgBoxResult.Yes Then

                IanConnect.Open()

                Dim DeleteClient As New System.Data.OracleClient.OracleCommand
                DeleteClient.Connection = IanConnect
                DeleteClient.CommandText = "DELETE from CLIENTS where CLIENT_ID = " & PassedClientID

                Delete = DeleteClient.ExecuteNonQuery()

                If Delete > 0 Then
                    MsgBox("Client " & PassedClientName & "  Deleted")
                    Me.Refresh()
                Else
                    MsgBox("No")
                End If

                IanConnect.Close()

            End If
        End If
    End Sub
End Class
 
Last edited by a moderator:
Here's an example connect string that I use:


Data Source=live01;Persist Security Info=True;User ID=uid;Password=passwid;Unicode=True

Different to yours, yes.. but thats because I use the TNS name, LIVE01, whereas you have specified all the connect data. Both can work. I just thought you;d specified in your sqlnet.ora that you were using tns

As for the data showing up twice, I dont really understand what you mean. Download Windows Media Encoder, load it and perform a screen capture, 640x480, flash border during capture (easier to see) and jsut record your actions. Add it to a (passworded) zip file (if you dont want others to see it) and attach it to your post.. When I can see what steps youre taking I can maybe see where the problem lies
 
so here is my problem with the data showing up twice.

the first form in my application.

minnow_01.jpg



when i click the view clients

minnow_02.jpg


there is only one client in the database. then i close the client list form and when i open it again this happens.

minnow_03.jpg


it will continue to add a third and fourth one if i close and reopen the form.

i dont know why this is happening? i have another application that i am using the same code on and that does not happen.

here is the code that i am using to populate the listbox. this happens on the form load
VB.NET:
IanConnect.Open()

        Dim ClientList As New System.Data.OracleClient.OracleCommand

        ClientList.Connection = IanConnect
        ClientList.CommandText = "select * from clients order by client_name"

        Dim dr As System.Data.OracleClient.OracleDataReader = ClientList.ExecuteReader()
        Dim counter As Integer = 0

        While dr.Read()
            ClientListListBox.Items.Add(dr.Item(1))
            ReDim Preserve ClientIDArray(counter + 1)
            ReDim Preserve ClientNameArray(counter + 1)
            ClientIDArray(counter) = dr.Item(0)
            ClientNameArray(counter) = dr.Item(1)
            counter += 1
        End While

        IanConnect.Close()

thanks
 
So tell me..

Where do you clear the textbox?

I see your code that adds, but I dont see any code that clears it..


I dont do data access this way, it's far too boring and labour intensive. If you still have trouble, you could try doing it the newer way, and see if there is a difference..
 
i added this to my click event when i close the form. and it took care of my duplicate data problem.
VB.NET:
ClientListListBox.Items.Clear()

what is the newer way of data access? all the information that i have read gave me examples pretty much exactly as i am doing it. is there someplace that has an overview of the new way? would you recommend the newer way?
 
i added this to my click event when i close the form. and it took care of my duplicate data problem.
VB.NET:
ClientListListBox.Items.Clear()
there never really was a duplicate data problem. When you close the form, it isnt disposed of, it jsut hangs around until next time you show it.. Then it returns, same form, same listbox, same items etc, and then you go and add another item to it. The clue is, when you said "the third time there are three, fourth time there are four..."

Whenever you add items to something Clear() it first!

what is the newer way of data access? all the information that i have read gave me examples pretty much exactly as i am doing it. is there someplace that has an overview of the new way? would you recommend the newer way?

Sure.. Take a read of the DW2 link in my signature, Section on creating a Simple Data App.. It works for Oracle too.. When I say new.. i mean like.. Maybe 8 years old, instead of 20. :)
 
You know what the sad part is...

ss7thirty said:
The only reason I could think of would be that you are either not clearing out the array or dataset that you are using to populate the listbox or that for some reason the listbox is not being cleared out.
 
Back
Top