Hi guys please am new to c# sharp am having trouble to insert Events into the databse for an application am building
I keep haveing the this error"The connection was not closed. The connection's current state is open."
please any advice on what to thanks in advance guys.
here is the code....

try
            {
                connection.Open();
              // con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand();
             //   using (SqlConnection con = new SqlConnection(connectionString))
                cmd = new SqlCommand(insertStatement,connection);
                //SqlCommand command = new SqlCommand("INSERT INTO EVENTS(EVENT_TITLE,START_DATE,START_TIME,END_DATE,END_TIME, BLDG_ID,DEPT_ID,COMMENTS) VALUES ('" + startDTPEvents.Value + "','" + dtpEventsStartTime.Value + "','" + endDTPEvents.Value + "','" + dtpEventsEndTime.Value + "," + buildingComboBox.SelectedValue + "," + departmentComboBox.SelectedValue +"," + commentsTextBox.Text + "','" +txtEventsTitle.Text+ "')", connection);
                 insertStatement=("INSERT INTO EVENTS" +"(EVENT_TITLE,START_DATE,START_TIME,END_DATE,END_TIME, BLDG_ID,DEPT_ID,COMMENTS)" +"  VALUES('eventsTitle','start_Date','start_Time','end_Date','end_Time','bldg_ID','dept_ID', 'comments')");
                //Executes the insert statement
                cmd.ExecuteNonQuery();
                MessageBox.Show("Event saved successfully.");
                txtAddBuilding.Text = "";

            }  
            catch (SqlException ex)
            {
                //Display the error to the user
                MessageBox.Show("There is an error Carl sorry" + ex);
            }

            {
                connection.Close();
            }
        }

It looks like you're missing a finally before the block that closes the connection.

commented: Spot on +7

Just to clarify. The reason you are receiving that message is due to the fact that after the SqlConnection was opened it was never closed. So your statement connection.Close(); never executed. By inserting a breakpoint and stepping your code you could have determined as to why.

Change your code like below.

try
            {
                connection.Open();
              // con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand();
             //   using (SqlConnection con = new SqlConnection(connectionString))
                cmd = new SqlCommand(insertStatement,connection);
                //SqlCommand command = new SqlCommand("INSERT INTO EVENTS(EVENT_TITLE,START_DATE,START_TIME,END_DATE,END_TIME, BLDG_ID,DEPT_ID,COMMENTS) VALUES ('" + startDTPEvents.Value + "','" + dtpEventsStartTime.Value + "','" + endDTPEvents.Value + "','" + dtpEventsEndTime.Value + "," + buildingComboBox.SelectedValue + "," + departmentComboBox.SelectedValue +"," + commentsTextBox.Text + "','" +txtEventsTitle.Text+ "')", connection);
                 insertStatement=("INSERT INTO EVENTS" +"(EVENT_TITLE,START_DATE,START_TIME,END_DATE,END_TIME, BLDG_ID,DEPT_ID,COMMENTS)" +"  VALUES('eventsTitle','start_Date','start_Time','end_Date','end_Time','bldg_ID','dept_ID', 'comments')");
                //Executes the insert statement
                cmd.ExecuteNonQuery();
                MessageBox.Show("Event saved successfully.");
                txtAddBuilding.Text = "";

            }  
            catch (SqlException ex)
            {
                //Display the error to the user
                MessageBox.Show("There is an error Carl sorry" + ex);
            }
            finally
            {
                connection.Close();
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.