Saving a selected book to a storage space

The following code basically updates the book list in the selected storage space if it already contains the specific book (after confirming with the user). Otherwise, it will add the book to the book list as a new book:

private void UpdateStorageSpaceBooks(int storageSpaceId) 
{ 
    try 
    { 
        int iCount = (from s in spaces 
                      where s.ID == storageSpaceId 
                      select s).Count(); 
        if (iCount > 0) // The space will always exist 
        { 
            // Update 
            StorageSpace existingSpace = (from s in spaces 
              where s.ID == storageSpaceId select s).First(); 
 
            List<Document> ebooks = existingSpace.BookList; 
 
            int iBooksExist = (ebooks != null) ? (from b in ebooks 
              where $"{b.FileName}".Equals($"
{txtFileName.Text.Trim()}") select b).Count() : 0; if (iBooksExist > 0) { // Update existing book DialogResult dlgResult = MessageBox.Show($"A book
with the same name has been found in Storage Space
{existingSpace.Name}.
Do you want to replace the existing book
entry with this one?",
"Duplicate Title", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button2); if (dlgResult == DialogResult.Yes) { Document existingBook = (from b in ebooks where $"
{b.FileName}".Equals($"
{txtFileName.Text.Trim()}") select b).First(); existingBook.FileName = txtFileName.Text; existingBook.Extension = txtExtension.Text; existingBook.LastAccessed =
dtLastAccessed.Value; existingBook.Created = dtCreated.Value; existingBook.FilePath = txtFilePath.Text; existingBook.FileSize = txtFileSize.Text; existingBook.Title = txtTitle.Text; existingBook.Author = txtAuthor.Text; existingBook.Publisher = txtPublisher.Text; existingBook.Price = txtPrice.Text; existingBook.ISBN = txtISBN.Text; existingBook.PublishDate =
dtDatePublished.Value; existingBook.Category = txtCategory.Text; } } else { // Insert new book Document newBook = new Document(); newBook.FileName = txtFileName.Text; newBook.Extension = txtExtension.Text; newBook.LastAccessed = dtLastAccessed.Value; newBook.Created = dtCreated.Value; newBook.FilePath = txtFilePath.Text; newBook.FileSize = txtFileSize.Text; newBook.Title = txtTitle.Text; newBook.Author = txtAuthor.Text; newBook.Publisher = txtPublisher.Text; newBook.Price = txtPrice.Text; newBook.ISBN = txtISBN.Text; newBook.PublishDate = dtDatePublished.Value; newBook.Category = txtCategory.Text; if (ebooks == null) ebooks = new List<Document>(); ebooks.Add(newBook); existingSpace.BookList = ebooks; } } spaces.WriteToDataStore(_jsonPath); PopulateStorageSpacesList(); MessageBox.Show("Book added"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

Lastly, as a matter of housekeeping, the ImportBooks form contains the following code for displaying and enabling controls based on the button click events of btnCancelNewStorageSpace and btnAddNewStorageSpace buttons:

private void btnCancelNewStorageSpaceSave_Click(object sender, EventArgs e) 
{ 
    txtNewStorageSpaceName.Clear(); 
    txtNewStorageSpaceName.Visible = false; 
    lblStorageSpaceDescription.Visible = false; 
    txtStorageSpaceDescription.ReadOnly = true; 
    txtStorageSpaceDescription.Clear(); 
    btnSaveNewStorageSpace.Visible = false; 
    btnCancelNewStorageSpaceSave.Visible = false; 
    dlVirtualStorageSpaces.Enabled = true; 
    btnAddNewStorageSpace.Enabled = true; 
} 
 
private void btnAddNewStorageSpace_Click(object sender, EventArgs e) 
{ 
    txtNewStorageSpaceName.Visible = true; 
    lblStorageSpaceDescription.Visible = true; 
    txtStorageSpaceDescription.ReadOnly = false; 
    btnSaveNewStorageSpace.Visible = true; 
    btnCancelNewStorageSpaceSave.Visible = true; 
    dlVirtualStorageSpaces.Enabled = false; 
    btnAddNewStorageSpace.Enabled = false; 
} 

All that remains now is for us to complete the code in the Form1.cs form, which is the start-up form.