title: C# SQLite database locked
body: <p>I've tried all the solutions I could find to this problem, but for some reason I still get an exception stating the database I'm using is locked.
  My code is as follows:</p>
  
  <pre><code>string connectionString = "Data Source=D:\\CCIW\\LCM\\Organisational Database\\OrganisationalDB;" +
                          "MultipleActiveResultSets=True";
  using (SQLiteConnection OriginatorDBConnection = new SQLiteConnection(connectionString))
  {
      string originatorName, originatorOrganisation, originatorAddress, originatorCellNumber, originatorTelNumber, originatorEmail;
  
      originatorName = originatorNameTextBox.Text;
      originatorOrganisation = originatorOrganisationTextBox.Text;
      originatorAddress = originatorAddressRichTextBox.Text;
      originatorCellNumber = originatorCellTextBox.Text;
      originatorTelNumber = originatorTelTextBox.Text;
      originatorEmail = originatorEmailTextBox.Text;
  
      OriginatorDBConnection.Open();
      string originatorINSERT = "INSERT INTO Originator (Name, Organisation, Address, CellphoneNumber, TelephoneNumber, Email) VALUES ('" + originatorName + "', '" + originatorOrganisation + "', '" + originatorAddress + "', '" + originatorCellNumber + "', '" + originatorTelNumber + "', '" + originatorEmail + "');";
  
      using (SQLiteCommand originatorCommand = new SQLiteCommand(originatorINSERT, OriginatorDBConnection))
      {
          originatorCommand.ExecuteNonQuery();                    
      }
      OriginatorDBConnection.Close();
  }
  </code></pre>
  
  <p>The closest solution on here that I could find to the problem was here: <a href="https://stackoverflow.com/questions/17592671/sqlite-database-locked-exception">SQLite Database Locked exception</a></p>
  
  <p>It didn't seem to work on my problem, however.
  What am I doing wrong?</p>
  
  <p>I have an additional function wherein I use the connection:</p>
  
  <pre><code> public AdminForm()
   {
          //Initialise AdminForm components.
          InitializeComponent();
  
          //Establish and open connection to ObservationDB.
          string connectionString = "Data Source=D:\\CCIW\\LCM\\Organisational Database\\OrganisationalDB;" +
                                    "MultipleActiveResultSets=True";
          ObservationDBConnection = new SQLiteConnection(connectionString);
          ObservationDBConnection.Open();
  
          //Query database to find all originators
          string originatorSELECT = "SELECT * FROM Originator;";
          string ECPNumberSELECT = "SELECT * FROM ECP";
  
          SQLiteCommand command = new SQLiteCommand(originatorSELECT, ObservationDBConnection);
          SQLiteDataReader reader = command.ExecuteReader();
  
          SQLiteCommand command2 = new SQLiteCommand(ECPNumberSELECT, ObservationDBConnection);
          SQLiteDataReader reader2 = command2.ExecuteReader();
  
          //Populate OriginatorName combobox with names of existing originators.
          List&lt;string&gt; originatorNames = new List&lt;string&gt;();
          while (reader.Read())
          {
              originatorNames.Add(Convert.ToString(reader["Name"]));
          }
  
          OriginatorNameComboBox.DataSource = originatorNames;
  
          //Populate ECP combobox with numbers of existing ECP.
          List&lt;string&gt; ECPNumbers = new List&lt;string&gt;();
          while (reader2.Read())
          {
              ECPNumbers.Add(Convert.ToString(reader2["Number"]));
          }
  
          ECPNumComboBox.DataSource = ECPNumbers;
  
          //Populate TC Decision combobox with options.
          List&lt;string&gt; TCDecision = new List&lt;string&gt;();
          TCDecision.Add("Rework");
          TCDecision.Add("Reject");
          TCDecision.Add("Approve");
  
          TCDecisionComboBox.DataSource = TCDecision;
  
          ObservationDBConnection.Close();
      }
  </code></pre>
  
  <p>And here:</p>
  
  <pre><code>private void SaveButton_Click(object sender, EventArgs e)
      {
          ObservationDBConnection.Open();
  
          ...
  
          string ImpactTypeINSERT = "INSERT INTO ImpactType (ImpactType, Description) VALUES ('" + impactType + "', '" + impactDescription + "');";
          SQLiteCommand ImpactTypeCommand = new SQLiteCommand(ImpactTypeINSERT, ObservationDBConnection);
          //SQLiteDataReader ImpactTypeReader = ImpactTypeCommand.ExecuteReader();
          ImpactTypeCommand.ExecuteNonQuery();
  
          ...
  
          string TCDecisionINSERT = "INSERT INTO TCDecision (Decision, Description) VALUES ('" + TechnicalCommitteeDecision + "', '" + TechnicalCommitteeDescription + "');";
          SQLiteCommand TCDecisionCommand = new SQLiteCommand(TCDecisionINSERT, ObservationDBConnection);
          SQLiteDataReader TCDecisionReader = ImpactTypeCommand.ExecuteReader();
          TCDecisionCommand.ExecuteNonQuery();
  
          ...
  
          string OperationalDecisionINSERT = "INSERT INTO OperationalDecision (Decision, Description) VALUES ('" + operationalDecision + "', '" + operationalDescription + "');";
          SQLiteCommand OperationalDecisionCommand = new SQLiteCommand(OperationalDecisionINSERT, ObservationDBConnection);
          //SQLiteDataReader OperationalDecisionReader = OperationalDecisionCommand.ExecuteReader();
          OperationalDecisionCommand.ExecuteNonQuery();
  
          ...
  
          ...
  
              string OriginatorIDSELECT = "SELECT * FROM Originator WHERE Name='" + OriginatorNameComboBox.Text + "';";
              SQLiteCommand OriginatorIDCommand = new SQLiteCommand(OriginatorIDSELECT, ObservationDBConnection);
              SQLiteDataReader OriginatorIDReader = OriginatorIDCommand.ExecuteReader();
              originatorIDOBS = OriginatorIDReader.GetOrdinal("ID");
              //ImpactType
              string impactTypeSELECT = "SELECT * FROM ImpactType WHERE ImpactType='" + impactType + "';";
              SQLiteCommand impactTypeOBSCommand = new SQLiteCommand(impactTypeSELECT, ObservationDBConnection);
              SQLiteDataReader impactTypeOBSReader = impactTypeOBSCommand.ExecuteReader();
              impactTypeOBS = impactTypeOBSReader.GetOrdinal("ID");            
  
              string operationalDecisionOBSSELECT = "SELECT * FROM OperationalDecision WHERE Decision='" + operationalDecision + "';";
              SQLiteCommand operationalDecisionOBSCommand = new SQLiteCommand(operationalDecisionOBSSELECT, ObservationDBConnection);
              SQLiteDataReader operationalDecisionOBSReader = operationalDecisionOBSCommand.ExecuteReader();
              operationalDecisionOBS = operationalDecisionOBSReader.GetOrdinal("ID");
  
             ...
  
              string ECPOBSSELECT = "SELECT * FROM ECP WHERE Number='" + ECPNumComboBox.Text + "';";
              SQLiteCommand ECPCommand = new SQLiteCommand(ECPOBSSELECT, ObservationDBConnection);
              SQLiteDataReader ECPReader = ECPCommand.ExecuteReader();
              ECPOBS = ECPReader.GetOrdinal("ID");
  
  
              string CNISObservationINSERT = "INSERT INTO CNISObservation (Title, ReceiveDate, TableDate, OriginatorID, OriginatorReference, OriginatorDate, ObservationNumber, RevisionNumber, Description, Status, ImpactDescription, ImpactType, OperationalRequirementDescription, OperationalImpact, OperationalDecision, ProposedAction, TCDecision, ECP, SolutionOperationalImpact, TechnicalSolutionImpact) VALUES ('" + 
                                                                          titleOBS + "','" 
                                                                          + receiveDateOBS + "','" 
                                                                          + tableDateOBS + "','"
                                                                          + originatorIDOBS + ",'"
                                                                          + originatorReferenceOBS +"','"
                                                                          + originatorDateOBS + "','"
                                                                          + observationNumberOBS + "',"
                                                                          + revisionNumberOBS + ",'"
                                                                          + descriptionOBS + "',"
                                                                          + statusOBS + ",'"
                                                                          + impactDescriptionOBS + "',"
                                                                          + impactTypeOBS + ",'"
                                                                          + operationalRequirementDescriptionOBS + "','"
                                                                          + operationalImpactOBS + "',"
                                                                          + operationalDecisionOBS + ",'"
                                                                          + TCDecisionOBS + ","
                                                                          + ECPOBS + ",'"
                                                                          + solutionOperationalImpactOBS + "','"
                                                                          + technicalSolutionImpactOBS + "');"; 
  
          ...
  
          string obsOBSSELECT = "SELECT * FROM CNISObservation ORDER BY ID DESC LIMIT 1;";
          SQLiteCommand CNISObservationIDCommand = new SQLiteCommand(obsOBSSELECT, ObservationDBConnection);
          SQLiteDataReader CNISObservationIDReader = CNISObservationIDCommand.ExecuteReader();
          observationID = CNISObservationIDReader.GetOrdinal("ID");
  
          ...
  
          foreach (var capabilityID in capabilitiesSelected)
          {
              string ObservationOperationalCapabilitiesINSERT = "INSERT INTO ObservationOperationalCapabilities (CapabilityID, ObservationID) VALUES (" + capabilityID + "," + observationID + ");";
              SQLiteCommand ObservationOperationalCapabilitiesCommand = new SQLiteCommand(ObservationOperationalCapabilitiesINSERT, ObservationDBConnection);
              // SQLiteDataReader ObservationOperationalCapabilitiesReader = ObservationOperationalCapabilitiesCommand.ExecuteReader();
              ObservationOperationalCapabilitiesCommand.ExecuteNonQuery();
  
          }
  
          ...
  
          string CNISObservationIDSELECT = "SELECT * FROM CNISObservation ORDER BY ID DESC LIMIT 1;";
          SQLiteCommand CNISObservationCommand = new SQLiteCommand(CNISObservationIDSELECT, ObservationDBConnection);
          SQLiteDataReader CNISObservationReader = CNISObservationCommand.ExecuteReader();
          CNISObservationID = CNISObservationReader.GetOrdinal("ID");
  
          string CNISReleaseINSERT = "INSERT INTO CNISSection VALUES (" + CNISObservationID + "," + CNISRelease + "," + chapter + ",'" + paragraph + "','" + section + "','" + page +"');";
          SQLiteCommand CNISReleaseCommand = new SQLiteCommand(CNISReleaseINSERT, ObservationDBConnection);
          //SQLiteDataReader CNISReleaseReader = CNISReleaseCommand.ExecuteReader();
          CNISReleaseCommand.ExecuteNonQuery();
  
          ObservationDBConnection.Close();
      }
  </code></pre>