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();
            }
        }

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();
            }
        }
This article has been dead for over six months. Start a new discussion instead.