title: How to check if ID exists in database table in C#?
body: <p>I am trying to delete entries by ID. I want to notify user that ID they try to delete doesn't exist. It doesn't create any problems, but I want to make everything clear.</p>
  
  <p>How to do that? Do I have to use SQL string to do so?</p>
  
  <p>I am using MS Access 2007 and this is how I delete item:</p>
  
  <pre><code>string SQL = "DELETE FROM PersonalData WHERE DataID = " + txtEntryID.Text;
  
  private void DeleteData(string SQL)
  {
      // Creating an object allowing me connecting to the database.
      // Using parameters in command will avoid attempts of SQL injection.
      OleDbConnection objOleDbConnection = new OleDbConnection();
      // Creating command object.
      objOleDbConnection.ConnectionString =
          "Provider=Microsoft.ACE.OLEDB.12.0;" +
          "Data Source=" + filePath + ";" +
          "Persist Security Info=False;" +
          "Jet OLEDB:Database Password=" + pass + ";";
      OleDbCommand objOleDbCommand = new OleDbCommand();
  
      objOleDbCommand.CommandText = SQL;
  
      // Assigning a connection string to the command.
      objOleDbCommand.Connection = objOleDbConnection;
  
      try
      {
          // Open database connection.
          objOleDbConnection.Open();
          objOleDbCommand.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
          // Displaying any errors that 
          // might have occured.
          MessageBox.Show("Error: " + ex.Message);
      }
      finally
      {
          // Close the database connection.
          objOleDbConnection.Close();
      }
  
      // Refreshing state of main window.
      mainWindow.DisplayFileContent(filePath);
  
      MessageBox.Show("Data was successfully deleted.");
  
      // Clearing text box field.
      txtEntryID.Clear();
  }
  </code></pre>