Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:40:00+00:00 2026-06-03T06:40:00+00:00

ExecuteNonQuery requires an open and available Connection. The connection’s current state is closed. Why

  • 0

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

Why always the system returns that message?

I have a sub:

Private Sub Initialization()    

    If myCEConnection.State = ConnectionState.Closed Then '--> *1
        Try
            myCEConnection.Open()
        Catch ex As Exception

        End Try
    End If

    Dim reader As SqlCeDataReader
    Dim myCommand As SqlCeCommand = myCEConnection.CreateCommand()

    myCommand.CommandText = "Command goes here"
    Try
        reader = myCommand.ExecuteReader() 
    Catch ex As Exception '--> *2

    End Try

    myCEConnection.Close()
End Sub

And I called that function from this sub:

Public myCEConnection As New SqlCeConnection("Connection String Goes Here")

Private Sub Report()
    If myCEConnection.State = ConnectionState.Closed Then '--> *1
        Try
            myCEConnection.Open()
        Catch ex As Exception

        End Try
    End If

    Initialization()

    myCEConnection.Close()
End Sub

When I try to set a breakpoint to the *1 line, the system returns myCEConnection.State = Open {1}. But when I continue to run the breakpoint, on line *2 I got: ExecuteReader requires an open and available Connection. The connection's current state is Closed.

enter image description here

What’s wrong with the code?

Note: Every sub/function on the class always have the If myCeConnection.State = ..... part to check the connection is already open or not. Maybe this caused the problem but I’m not quite sure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:40:01+00:00Added an answer on June 3, 2026 at 6:40 am

    You need to associate the connection and the command:

    myCommand.Connection = myCEConnection
    

    Also consider changing your code into something like this:

    Dim reader As SqlCeDataReader
    
    Using myCEConnection As New SqlCeConnection("Connection String Goes Here")
      myCEConnection.Open()
      Using myCommand As SqlCeCommand = myCEConnection.CreateCommand()
        myCommand.Connection = myCEConnection
        myCommand.CommandText = "Command goes here"
        reader = myCommand.ExecuteReader()
      End Using
    End Using
    

    You don’t really need the Try-Catch’es, and my guess is the initial myCEConnection.Open() is throwing an exception, but you never see it because of the Try-Catch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed. What
Im getting connection property has not been properly initialized error message on command2.ExecuteNonQuery(). I
I get the following error on cmd.ExecuteNonQuery . ExecuteNonQuery requires the command to have
In SQL we open connection conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); or using (conn = new SqlConnection(connectionString))
a) SqlCommand.ExecuteNonQuery is used for update, insert and delete operations. Besides the fact that
I've noticed that my code errors out on sqlWrite.ExecuteNonQuery(); after executing 200 Insert queries
I have a program that stores user projects as databases. Naturally, the program should
I have this 8 textboxes that require users to fill in the data in
I have a number of stored procedures I call from code with ExecuteNonQuery .
I have a SqlCommand object, and I execute its ExecuteNonQuery method. The query fails,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.