Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I'm having a hard time solving this one I'm working with vs2017 and access as my database. I don't really know what happened here.

I have a COMBOBOX that displays the name of all category name field from my database and from there I can delete a row in my database using the name displayed in the combobox But I encountered that error.

Below is my code:


VB
Private Sub cmbCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCategory.SelectedIndexChanged

Using cons As New OleDb.OleDbConnection(conSTR)
    Dim rdr As OleDb.OleDbDataReader

    With cmd
        .Connection = cons
        .CommandText = "SELECT CategoryName, CatPercent FROM Category WHERE CategoryName = @cn"
        .Parameters.AddWithValue("cn", cmbCategory.Text)
        .Connection.Open()
        rdr = cmd.ExecuteReader

        If rdr.HasRows Then

            While rdr.Read()

                txtCategory.Text = rdr("CategoryName").ToString
                numCategory.Text = rdr("CatPercent")

            End While

            rdr.Close()
            .Connection.Close()

        Else
            MessageBox.Show("NO DATA FOUND", "CATEGORY", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If


    End With


        End Using


    End Sub


The exception occurs in CONS which holds my connection the one I made bold and underlined above. I don't know what does it mean.

It occurs after I select in my combo box a name and then I delete it and then I select again and try to delete but this error pops up
COM object that has been separated from its underlying RCW cannot be used.


What I have tried:

I thought it's just my connection string that causing multiple connections but it wasn't.

It occurs after I select in my combo box a name and then I delete it and then I select again and try to delete but this error pops up <pre>COM object that has been separated from its underlying RCW cannot be used.
Posted
Updated 2-Apr-20 2:25am
Comments
Richard MacCutchan 2-Apr-20 5:02am    
Why are you trying to delete the same item more than once?

The error message suggests that you may be trying to access cons from a separate thread in your application.
Richard Deeming 2-Apr-20 8:20am    
Why are you reusing the OleDbCommand instance cmd? Just create a new instance for each command you want to execute, and wrap it in a Using block.

rdr should also be wrapped in a Using block.
lelouch_vi 2 2-Apr-20 21:12pm    
I see.Thank you for pointing that out. The error indeed is gone but can you explain a bit more about creating instances of cmd? I thought if I declare cmd as a local variable it can't be accessed by other functions?

1 solution

I suspect the problem is that you are storing your OleDbCommand object in a class-level field, and reusing it across methods.

Change your code to create the command instance when you need it, and wrap it in a Using block, and your error will almost certainly go away.
VB.NET
Private Sub cmbCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCategory.SelectedIndexChanged

    Using cons As New OleDb.OleDbConnection(conSTR)
        Using cmd As New OleDb.OleDbCommand()
            cmd.Connection = cons
            cmd.CommandText = "SELECT CategoryName, CatPercent FROM Category WHERE CategoryName = @cn"
            cmd.Parameters.AddWithValue("cn", cmbCategory.Text)
            
            cons.Open()
            Using rdr As OleDb.OleDbDataReader = cmd.ExecuteReader()
                If rdr.Read() Then
                    txtCategory.Text = rdr("CategoryName").ToString
                    numCategory.Text = rdr("CatPercent")
                Else
                    MessageBox.Show("NO DATA FOUND", "CATEGORY", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End Using
        End Using
    End Using
End Sub
 
Share this answer
 
Comments
Maciej Los 2-Apr-20 9:19am    
5ed!
lelouch_vi 2 2-Apr-20 21:15pm    
Hey, man thanks for all of you. I have found where the error is. I forgot I declared the cmd in a module so every class can accessed it but I think that's the error coming from. Like what you have said I may have multiple instances of cmd. Anyway Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900