Command text was not set for the command object.

Greetings;

I receive the error 'Command text was not set for the command object.' when attempting to add a row to a Microsoft Access database. I am stuck attempting to debug the symptom.

I included the class function below for reference:
    Private Function _SaveProductSubCat(ByVal _strProductSubCat As String) As Boolean

        Dim _cmdProduct As New OleDb.OleDbCommand

        Try
            Me._cnnProduct.ConnectionString = ConfigurationManager.ConnectionStrings.Item("Product").ToString

            _cmdProduct.Connection = _cnnProduct

            _cnnProduct.CreateCommand.CommandText = "up_i_ProductSubCategory"
            _cnnProduct.CreateCommand.CommandType = CommandType.StoredProcedure

            _cmdProduct.CreateParameter.ParameterName = "?"
            _cmdProduct.CreateParameter.OleDbType = OleDb.OleDbType.VarChar
            _cmdProduct.CreateParameter.Value = _strProductSubCat

            _cnnProduct.Open()
            _cmdProduct.ExecuteNonQuery()
            _cnnProduct.Close()
        Catch ex As Exception
            Me._strMessage = ex.Message
            Return False
        End Try

        Return True

    End Function

Open in new window

Thank you in advance to any light you may be able to shed on this.

David Bach
David BachAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You're calling the CreateCommand method repeatedly. This creates a new OleDbCommand in each case. Refactor your code to use the command object that you created at the top of your method.

e.g.

Private Function _SaveProductSubCat(ByVal _strProductSubCat As String) As Boolean

    Dim _cmdProduct As New OleDb.OleDbCommand

    Try
        Me._cnnProduct.ConnectionString = ConfigurationManager.ConnectionStrings.Item("Product").ToString

        _cmdProduct.Connection = _cnnProduct            

        _cmdProduct.CommandText = "up_i_ProductSubCategory"
        _cmdProduct.CommandType = CommandType.StoredProcedure

        _cmdProduct.Parameters.AddWithValue("?", _strProductSubCat)

        _cnnProduct.Open()
        _cmdProduct.ExecuteNonQuery()
        _cnnProduct.Close()
    Catch ex As Exception
        Me._strMessage = ex.Message
        Return False
    End Try

    Return True

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David BachAuthor Commented:
Dear kaufmed;

I've been in the business of writing software for 42 years and feel a tad bit embarrassed. It has been roughly a decade since I've needed to query a Microsoft Access database. I'm used to Microsoft SQL Server databases. My mind was out to lunch.

Thank you for making it so darn simple! I read more into what I thought I needed rather than just keeping it simple.

David Bach
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.