//String UserLogin; //String UserPassword; public UserView(String UserLogin, String UserPassword) { InitializeComponent(); //this.UserLogin = UserLogin; //this.UserPassword = UserPassword; try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { this.UserName = context.Patrons.FirstOrDefault(s => s.UserLogin == UserLogin && s.UserPassword == UserPassword).PersonFirstName; this.UserBirthday = context.Patrons.FirstOrDefault(s => s.UserLogin == UserLogin && s.UserPassword == UserPassword).PersonDateOfBirth; } } catch (Exception ex) { MessageBox.Show(ex.Message); } LoadingUserData(); DataFill(); }
public Boolean updateBook_borrow(Book_borrow item) { Boolean status = false; try { using (var db = new LibraryDB()) { Console.WriteLine("Book detail updated"); db.Book_borrow.AddOrUpdate(item); db.SaveChanges(); status = true; } } catch (Exception ex) { Console.WriteLine("Update book problem " + ex.InnerException); } return(status); }
public IActionResult Create(Book book) { if (!ModelState.IsValid && book.Price < 1) { return(RedirectToAction("Index")); } Book currentBook = new Book() { Title = book.Title, Author = book.Author, Price = book.Price }; using (var db = new LibraryDB()) { db.Books.Add(currentBook); db.SaveChanges(); }; return(RedirectToAction("Index")); }
private void btnSearchClient_Click(object sender, EventArgs e) { string searchText = txtSearchClient.Text.Trim().ToLower(); using (LibraryDB db = new LibraryDB()) { List <Models.Client> clientsList = db.Clients.Where(c => c.Name.ToLower().Contains(searchText) || c.Surname.ToLower().Contains(searchText) || c.Email.ToLower().Contains(searchText) || c.Phone.ToLower().Contains(searchText) ).ToList(); dgvClients.Rows.Clear(); foreach (var item in clientsList) { dgvClients.Rows.Add(item.Id, item.Name, item.Surname, item.Email, item.Phone); } } }
private bool BookSubmission() { // check if isbn is an int if (IsInt(txtISBN.Text) is false) { MessageBox.Show("ISBN must be an 8 digit number."); return(false); } // check if isbn is correct length if (IsRange(txtISBN.Text) is false) { MessageBox.Show("ISBN must be an 8 digit number."); return(false); } // check if isbn is already in db if (LibraryDB.IsBook(txtISBN.Text)) { MessageBox.Show("Duplicate ISBN. Try again."); return(false); } if (IsEmpty(txtTitle.Text)) { return(false); } if (IsEmpty(txtAuthor.Text)) { return(false); } if (IsEmpty(txtCategory.Text)) { return(false); } return(true); }
public LibrarianView(String UserLogin, String UserPassword) { InitializeComponent(); try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { this.UserName = context.Librarians.FirstOrDefault(s => s.UserLogin == UserLogin && s.UserPassword == UserPassword).PersonFirstName; this.UserBirthday = context.Librarians.FirstOrDefault(s => s.UserLogin == UserLogin && s.UserPassword == UserPassword).PersonDateOfBirth; } } catch (Exception ex) { MessageBox.Show(ex.Message); } LoadingLibrarianData(); DataFill(); ItemsViewing = true; PatronViewing = false; LibrarianViewing = false; ShowStripMenu(); }
private void removeLibrarianToolStripMenuItem_Click(object sender, EventArgs e) { if (ItemsDataGridView.SelectedRows.Count == 1) { DateTime LibBirthday = Convert.ToDateTime(ItemsDataGridView.SelectedRows[0].Cells["Birthday"].Value); String LibName = ItemsDataGridView.SelectedRows[0].Cells["Name"].Value.ToString(); try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { Librarian ToRemove = context.Librarians.Single(s => String.Concat(s.PersonFirstName, " ", s.PersonLastName) == LibName && s.PersonDateOfBirth == LibBirthday); context.Librarians.Remove(ToRemove); context.SaveChanges(); this.FillWithLibrarians(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void btnSave_Click(object sender, EventArgs e) { string name = txtName.Text; decimal price = Convert.ToDecimal(txtPrice.Text); int count = Convert.ToInt32(txtCount.Text); int selected = cmbCategories.SelectedIndex; int catID = categories[selected].Id; using (LibraryDB db = new LibraryDB()) { Models.Book book = new Models.Book() { Name = name, Price = price, Count = count, CategoryID = catID }; db.Books.Add(book); db.SaveChanges(); foreach (int item in lbAuthors.SelectedIndices) { AuthorsBook ab = new AuthorsBook { BookID = book.Id, AuthorID = authors[item].Id }; db.AuthorsBooks.Add(ab); db.SaveChanges(); } } FillBooks(); }
public AddBookForm() { InitializeComponent(); try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { GenreCombobox.DataSource = context.Genres.Select(s => s.GenreName).ToList(); LanguageCombobox.DataSource = context.Languages.Select(s => s.LanguageName).ToList(); TypeCombobox.DataSource = context.Types.Select(s => s.TypeName).ToList(); FormatCombobox.DataSource = context.Formats.Select(s => s.FormatName).ToList(); String[] Categories = { "Borrowable", "Non-borrowable" }; CategoryCombobox.DataSource = Categories; SendButton.Visible = true; BorrowButton.Visible = false; ReserveButton.Visible = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void LoadDataForEdit(String UserName, DateTime UserBirthday) { try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { FirstNameTextbox.Text = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).PersonFirstName; LastnameTextbox.Text = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).PersonLastName; BirthdayTimePicker.Value = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).PersonDateOfBirth; EmailTextbox.Text = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserEmail; SalaryTextbox.Text = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).Salary.ToString(); PasswordTextbox.Text = context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserPassword; if (!String.IsNullOrEmpty(context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressHousename)) { HouseNameTextbox.Text = context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressHousename; } StreetTextbox.Text = context.Streets.FirstOrDefault(s => s.StreetName == context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(p => p.PersonFirstName == UserName && p.PersonDateOfBirth == UserBirthday).UserAddress).AddressStreetName).StreetName; if (context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressHouseNumber != 0) { HouseNumberTextbox.Text = context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressHouseNumber.ToString(); } if (context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressFlatNumber != 0) { HouseNumberTextbox.Text = context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressFlatNumber.ToString(); } AreacodeTextbox.Text = context.AreaCodes.FirstOrDefault(a => a.AreaCodeCode == context.PostCodes.FirstOrDefault(p => p.PostCodeId == context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressPostCode).FirstCodePart).AreaCodeCode; ArbitrarycodeTextbox.Text = context.ArbitraryCodes.FirstOrDefault(a => a.ArbitraryCodeCode == context.PostCodes.FirstOrDefault(p => p.PostCodeId == context.Addresses.FirstOrDefault(m => m.AddressID == context.Librarians.FirstOrDefault(s => s.PersonFirstName == UserName && s.PersonDateOfBirth == UserBirthday).UserAddress).AddressPostCode).SecondCodePart).ArbitraryCodeCode; FirstNameTextbox.ReadOnly = true; BirthdayTimePicker.Enabled = false; SalaryTextbox.ReadOnly = true; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// // GET: /search/ public ActionResult Index(String s) { List <String> resultList = new List <String>(); try { using (var db = new LibraryDB()) { var authorObj = (from a in db.Author where (a.FirstName.Contains(s) || a.LastName.Contains(s)) select a).ToList(); var bookObj = (from b in db.Book where b.Title.Contains(s) select b).ToList(); if (authorObj != null || bookObj != null) { foreach (var a in authorObj) { resultList.Add(a.FirstName + " " + a.LastName); } foreach (var b in bookObj) { resultList.Add(b.Title); } ViewBag.SearchResult = resultList; return(View()); } else { return(View()); // lists == null } } } catch (NullReferenceException e) { return(View()); //s.length == 0 } }
protected void BtnCreate_Click(object sender, EventArgs e) { if (IsValid) { using (SqlConnection conn = new SqlConnection(connectionString)) { using (TransactionScope tran = new TransactionScope(TransactionScopeOption.RequiresNew)) { conn.Open(); try { LibraryDB.Insert(); tran.Complete(); } catch (Exception ex) { //ClientScript.RegisterStartupScript(GetType(), "ERRLOL", $"alert('{ex.Message}');", true); } } } Response.Redirect(@"~/Login.aspx"); } }
private void SearchButton_Click(object sender, EventArgs k) { try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { if (AuthorRadioButton.Checked) { var BorrowAbleItems = (from a in context.BorrowAbles join b in context.Titles on a.TitleID equals b.TitleID join c in context.Authors on b.AuthorID equals c.PersonId join d in context.Genres on b.GenreName equals d.GenreName join e in context.Publishers on a.PublisherName equals e.PublisherName join f in context.Languages on a.LanguageName equals f.LanguageName join g in context.Types on a.TypeName equals g.TypeName join h in context.Formats on a.FormatName equals h.FormatName where c.PersonFirstName == SearchTextbox.Text || c.PersonLastName == SearchTextbox.Text || String.Concat(c.PersonFirstName, " ", c.PersonLastName) == SearchTextbox.Text orderby b.TitleName select new { ID = a.ItemID, Title = b.TitleName, Author = String.Concat(c.PersonFirstName, " ", c.PersonLastName), Genre = d.GenreName, Publisher = e.PublisherName, Language = f.LanguageName, Type = g.TypeName, Format = a.FormatName, TheAmount = a.Amount }).ToList(); var NonBorrowAbleItems = (from a in context.NonBorrowAbles join b in context.Titles on a.TitleID equals b.TitleID join c in context.Authors on b.AuthorID equals c.PersonId join d in context.Genres on b.GenreName equals d.GenreName join e in context.Publishers on a.PublisherName equals e.PublisherName join f in context.Languages on a.LanguageName equals f.LanguageName join g in context.Types on a.TypeName equals g.TypeName join h in context.Formats on a.FormatName equals h.FormatName where c.PersonFirstName == SearchTextbox.Text || c.PersonLastName == SearchTextbox.Text || String.Concat(c.PersonFirstName, " ", c.PersonLastName) == SearchTextbox.Text orderby b.TitleName select new { ID = a.ItemID, Title = b.TitleName, Author = String.Concat(c.PersonFirstName, " ", c.PersonLastName), Genre = d.GenreName, Publisher = e.PublisherName, Language = f.LanguageName, Type = g.TypeName, Format = a.FormatName, TheAmount = a.Amount }).ToList(); BorrowAbleItems.AddRange(NonBorrowAbleItems); ItemsDataGridView.DataSource = BorrowAbleItems; } else if (TitleRadioButton.Checked) { var BorrowAbleItems = (from a in context.BorrowAbles join b in context.Titles on a.TitleID equals b.TitleID join c in context.Authors on b.AuthorID equals c.PersonId join d in context.Genres on b.GenreName equals d.GenreName join e in context.Publishers on a.PublisherName equals e.PublisherName join f in context.Languages on a.LanguageName equals f.LanguageName join g in context.Types on a.TypeName equals g.TypeName join h in context.Formats on a.FormatName equals h.FormatName where b.TitleName.Contains(SearchTextbox.Text) orderby b.TitleName select new { ID = a.ItemID, Title = b.TitleName, Author = String.Concat(c.PersonFirstName, " ", c.PersonLastName), Genre = d.GenreName, Publisher = e.PublisherName, Language = f.LanguageName, Type = g.TypeName, Format = a.FormatName, Availability = (!a.isBorrowed && !a.isReserved) ? "yes" : "no" }).ToList(); var NonBorrowAbleItems = (from a in context.NonBorrowAbles join b in context.Titles on a.TitleID equals b.TitleID join c in context.Authors on b.AuthorID equals c.PersonId join d in context.Genres on b.GenreName equals d.GenreName join e in context.Publishers on a.PublisherName equals e.PublisherName join f in context.Languages on a.LanguageName equals f.LanguageName join g in context.Types on a.TypeName equals g.TypeName join h in context.Formats on a.FormatName equals h.FormatName where b.TitleName.Contains(SearchTextbox.Text) orderby b.TitleName select new { ID = a.ItemID, Title = b.TitleName, Author = String.Concat(c.PersonFirstName, " ", c.PersonLastName), Genre = d.GenreName, Publisher = e.PublisherName, Language = f.LanguageName, Type = g.TypeName, Format = a.FormatName, Availability = (!a.isReserved) ? "yes" : "no" }).ToList(); BorrowAbleItems.AddRange(NonBorrowAbleItems); ItemsDataGridView.DataSource = BorrowAbleItems; ItemsViewing = true; PatronViewing = false; LibrarianViewing = false; ShowStripMenu(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void PutBlob(string filename, byte[] data) { // Guard if (String.IsNullOrWhiteSpace(filename)) { throw new Exception("Can't store in LibraryDB with null filename."); } // Calculate the MD5 of this blobbiiiieeeeee string md5 = StreamMD5.FromBytes(data); try { lock (DBAccessLock.db_access_lock) { using (var connection = GetConnection()) { connection.Open(); using (var transaction = connection.BeginTransaction()) { bool managed_update = false; using (var command = new SQLiteCommand("UPDATE LibraryItem SET MD5=@md5, DATA=@data, LAST_UPDATED_BY=@last_updated_by WHERE filename=@filename", connection, transaction)) { command.Parameters.AddWithValue("@filename", filename); command.Parameters.AddWithValue("@last_updated_by", Environment.UserName); command.Parameters.AddWithValue("@md5", md5); command.Parameters.AddWithValue("@data", data); int num_rows_updated = command.ExecuteNonQuery(); if (1 == num_rows_updated) { managed_update = true; } } if (!managed_update) { using (var command = new SQLiteCommand("INSERT INTO LibraryItem(filename, last_updated_by, md5, data) VALUES(@filename, @last_updated_by, @md5, @data)", connection, transaction)) { command.Parameters.AddWithValue("@filename", filename); command.Parameters.AddWithValue("@last_updated_by", Environment.UserName); command.Parameters.AddWithValue("@md5", md5); command.Parameters.AddWithValue("@data", data); command.ExecuteNonQuery(); } } transaction.Commit(); } connection.Close(); } // // see SO link in ../LibraryDB.cs at the `DBAccessLock.db_access_lock` declaration. // // We keep this *inside* the critical section so that we know we'll be the only active SQLite // action which just transpired. // *This* is also the reason why I went with a *global* lock (singleton) for *all* databases, // even while *theoretically* this is *wrong* or rather: *unnecessary* as the databases // i.e. Qiqqa Libraries shouldn't bite one another. I, however, need to ensure that the // added `System.Data.SQLite.SQLiteConnection.ClearAllPools();` statements don't foul up // matters in library B while lib A I/O is getting cleaned up. // // In short: Yuck. + Cave canem. // SQLiteConnection.ClearAllPools(); } } catch (Exception ex) { Logging.Error(ex, "IntranetLibraryDB::PutBLOB: Database I/O failure for DB '{0}'.", library_path); LibraryDB.FurtherDiagnoseDBProblem(ex, null, library_path); throw; } }
// ************************************************************************************************************* // *** MIGRATION TO OPEN SOURCE CODE *************************************************************************** // ************************************************************************************************************* private void AddLegacyWebLibrariesThatCanBeFoundOnDisk() { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); try { ConfigurationManager.ThrowWhenActionIsNotEnabled(nameof(AddLegacyWebLibrariesThatCanBeFoundOnDisk)); /** * Plan: * - Iterate through all the folders in the Qiqqa data directory. * - If a folder contains a valid Library record and it is a WEB library, * then add it to our list with the word '[LEGACY]' in front of it. */ string base_directory_path = UpgradePaths.V037To038.SQLiteUpgrade.BaseDirectoryForQiqqa; Logging.Info("Going to scan for web libraries at: {0}", base_directory_path); if (Directory.Exists(base_directory_path)) { string[] library_directories = Directory.GetDirectories(base_directory_path); foreach (string library_directory in library_directories) { Logging.Info("Inspecting directory {0} - Phase 1 : Web & Known Libraries", library_directory); string databaselist_file = Path.GetFullPath(Path.Combine(library_directory, @"Qiqqa.known_web_libraries")); if (File.Exists(databaselist_file)) { LoadKnownWebLibraries(databaselist_file, only_load_those_libraries_which_are_actually_present: true); } } foreach (string library_directory in library_directories) { Logging.Info("Inspecting directory {0} - Phase 2 : Intranet Libraries", library_directory); string databaselist_file = IntranetLibraryTools.GetLibraryDetailPath(library_directory); if (File.Exists(databaselist_file)) { IntranetLibraryDetail intranet_library_detail = IntranetLibraryDetail.Read(databaselist_file); UpdateKnownWebLibraryFromIntranet(library_directory, extra_info_message_on_skip: String.Format(" as obtained from file {0}", databaselist_file)); } } foreach (string library_directory in library_directories) { Logging.Info("Inspecting directory {0} - Phase 3 : Bundles", library_directory); // must be a qiqqa_bundle and/or qiqqa_bundle_manifest file set Logging.Warn("Auto bundle import at startup is not yet supported."); } foreach (string library_directory in library_directories) { Logging.Info("Inspecting directory {0} - Phase 4 : Local and Legacy Libraries", library_directory); string database_file = LibraryDB.GetLibraryDBPath(library_directory); string db_syncref_path = IntranetLibraryTools.GetLibraryMetadataPath(library_directory); // add/update only if this is not a Internet sync directory/DB! if (File.Exists(db_syncref_path)) { Logging.Info("Skip the Qiqqa Internet/Intranet Sync directory and the sync DB contained therein: '{0}'", db_syncref_path); // https://github.com/jimmejardine/qiqqa-open-source/issues/145 :: delete lib file when it is very small and was illegally // constructed by a previous v82beta Qiqqa release: if (File.Exists(database_file)) { long s3length = File.GetSize(database_file); if (6 * 1024 > s3length) { Logging.Warn("DELETE the wrongfully created DB file '{0}' in the Qiqqa Internet/Intranet Sync directory and the sync DB contained therein: '{1}', which has precedence!", database_file, db_syncref_path); FileTools.DeleteToRecycleBin(database_file); } else { Logging.Error("Inspect the Library DB file '{0}' in the Qiqqa Internet/Intranet Sync directory and the sync DB contained therein: '{1}', which MAY have precedence. Delete one of these manually to clean up your system as Qiqqa heuristics cannot tell which is the prevalent metadata database here!", database_file, db_syncref_path); } } continue; } if (File.Exists(database_file)) { var library_id = Path.GetFileName(library_directory); WebLibraryDetail new_web_library_detail = new WebLibraryDetail(); new_web_library_detail.Id = library_id; new_web_library_detail.Title = "Legacy Web Library - " + new_web_library_detail.Id; new_web_library_detail.IsReadOnly = false; // library: UNKNOWN type UpdateKnownWebLibrary(new_web_library_detail); } } } } catch (Exception ex) { Logging.Error(ex, "There was a problem while scanning for (legacy) libraries."); } }
public List <IntranetLibraryItem> GetIntranetLibraryItemsSummary() { List <IntranetLibraryItem> results = new List <IntranetLibraryItem>(); List <Exception> database_corruption = new List <Exception>(); try { lock (DBAccessLock.db_access_lock) { using (var connection = GetConnection()) { connection.Open(); string command_string = "SELECT filename, md5 FROM LibraryItem WHERE 1=1 "; using (var command = new SQLiteCommand(command_string, connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { IntranetLibraryItem result = new IntranetLibraryItem(); results.Add(result); try { result.filename = reader.GetString(0); result.md5 = reader.GetString(1); } catch (Exception ex) { string msg = String.Format("IntranetLibraryDB::GetIntranetLibraryItemsSummary: Database record #{3} decode failure for DB '{0}': filename_id={1}, md5={2}.", library_path, String.IsNullOrEmpty(result.filename) ? "???" : result.filename, String.IsNullOrEmpty(result.md5) ? "???" : result.md5, reader.StepCount // ~= results.Count + database_corruption.Count ); Logging.Error(ex, "{0}", msg); Exception ex2 = new Exception(msg, ex); database_corruption.Add(ex2); } } reader.Close(); } } connection.Close(); } // // see SO link above at the `DBAccessLock.db_access_lock` declaration. // // We keep this *inside* the critical section so that we know we'll be the only active SQLite // action which just transpired. // *This* is also the reason why I went with a *global* lock (singleton) for *all* databases, // even while *theoretically* this is *wrong* or rather: *unnecessary* as the databases // i.e. Qiqqa Libraries shouldn't bite one another. I, however, need to ensure that the // added `System.Data.SQLite.SQLiteConnection.ClearAllPools();` statements don't foul up // matters in library B while lib A I/O is getting cleaned up. // // In short: Yuck. + Cave canem. // SQLiteConnection.ClearAllPools(); } } catch (Exception ex) { Logging.Error(ex, "IntranetLibraryDB::GetLibraryItemsSummary: Database I/O failure for DB '{0}'.", library_path); LibraryDB.FurtherDiagnoseDBProblem(ex, database_corruption, library_path); throw; } if (database_corruption.Count > 0) { // report database corruption: the user may want to recover from this ASAP! if (MessageBoxes.AskErrorQuestion(true, "INTRANET Library (Sync Point) '{0}' has some data corruption. Do you want to abort the application to attempt recovery using external tools, e.g. a data restore from backup?\n\nWhen you answer NO, we will continue with what we could recover so far instead.\n\n\nConsult the Qiqqa logfiles to see the individual corruptions reported.", library_path)) { Logging.Warn("User chose to abort the application on database corruption report"); Environment.Exit(3); } } return(results); }
public UserService(LibraryDB dbContext) { _dbContext = dbContext; }
public LibraryService(LibraryDB dbContext) { _dbContext = dbContext; }
private void SendButton_Click(object sender, EventArgs e) { try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { int HouseVal = 0, FlatVal = 0; double SalaryVal = 0; if (int.TryParse(HouseNumberTextbox.Text, out int HouseTemp)) { HouseVal = int.Parse(HouseNumberTextbox.Text); } if (int.TryParse(FlatNumberTextbox.Text, out int FlatTemp)) { FlatVal = int.Parse(FlatNumberTextbox.Text); } if (Double.TryParse(SalaryTextbox.Text, out Double SalaryTemp)) { SalaryVal = Double.Parse(SalaryTextbox.Text); } context.Streets.AddOrUpdate(s => s.StreetName, new Street(StreetTextbox.Text)); context.AreaCodes.AddOrUpdate(s => s.AreaCodeCode, new AreaCode(AreacodeTextbox.Text)); context.ArbitraryCodes.AddOrUpdate(s => s.ArbitraryCodeCode, new ArbitraryCode(ArbitrarycodeTextbox.Text)); context.SaveChanges(); if (!context.PostCodes.Any(s => s.FirstCodePart == AreacodeTextbox.Text && s.SecondCodePart == ArbitrarycodeTextbox.Text)) { context.PostCodes.AddOrUpdate(s => new { s.FirstCodePart, s.SecondCodePart }, new PostCode(context.AreaCodes.FirstOrDefault(s => s.AreaCodeCode == AreacodeTextbox.Text).AreaCodeCode, context.ArbitraryCodes.FirstOrDefault(s => s.ArbitraryCodeCode == ArbitrarycodeTextbox.Text).ArbitraryCodeCode)); } context.SaveChanges(); if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && !String.IsNullOrEmpty(FlatNumberTextbox.Text)) { if (!context.Addresses.Any(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressHouseNumber == HouseVal && s.AddressFlatNumber == FlatVal && s.AddressStreetName == StreetTextbox.Text && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)) { context.Addresses.AddOrUpdate(s => new { s.AddressStreetName, s.AddressHouseNumber, s.AddressFlatNumber, s.AddressPostCode }, new Address(HouseNameTextbox.Text, context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName, HouseVal, FlatVal, context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)); } } else if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { if (!context.Addresses.Any(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressHouseNumber == HouseVal && s.AddressStreetName == StreetTextbox.Text && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)) { context.Addresses.AddOrUpdate(s => new { s.AddressStreetName, s.AddressHouseNumber, s.AddressPostCode }, new Address(HouseNameTextbox.Text, context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName, HouseVal, context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)); } } else if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { if (!context.Addresses.Any(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressStreetName == StreetTextbox.Text && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)) { context.Addresses.AddOrUpdate(s => new { s.AddressHousename, s.AddressStreetName, s.AddressPostCode }, new Address(HouseNameTextbox.Text, context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName, context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)); } } else if (String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && !String.IsNullOrEmpty(FlatNumberTextbox.Text)) { if (!context.Addresses.Any(s => s.AddressHouseNumber == HouseVal && s.AddressFlatNumber == FlatVal && s.AddressStreetName == StreetTextbox.Text && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)) { context.Addresses.AddOrUpdate(s => new { s.AddressStreetName, s.AddressHouseNumber, s.AddressFlatNumber, s.AddressPostCode }, new Address(context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName, HouseVal, FlatVal, context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)); } } if (String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { if (!context.Addresses.Any(s => s.AddressHouseNumber == HouseVal && s.AddressStreetName == StreetTextbox.Text && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)) { context.Addresses.AddOrUpdate(s => new { s.AddressStreetName, s.AddressHouseNumber, s.AddressPostCode }, new Address(context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName, HouseVal, context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId)); } } context.SaveChanges(); if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && !String.IsNullOrEmpty(FlatNumberTextbox.Text)) { context.Librarians.AddOrUpdate(s => new { s.PersonFirstName, s.PersonDateOfBirth }, new Librarian(FirstNameTextbox.Text, LastnameTextbox.Text, BirthdayTimePicker.Value, EmailTextbox.Text, PasswordTextbox.Text, SalaryVal, context.Addresses.FirstOrDefault(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressStreetName == context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName&& s.AddressHouseNumber == HouseVal && s.AddressFlatNumber == FlatVal && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId).AddressID)); } else if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { context.Librarians.AddOrUpdate(s => new { s.PersonFirstName, s.PersonDateOfBirth }, new Librarian(FirstNameTextbox.Text, LastnameTextbox.Text, BirthdayTimePicker.Value, EmailTextbox.Text, PasswordTextbox.Text, SalaryVal, context.Addresses.FirstOrDefault(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressStreetName == context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName&& s.AddressHouseNumber == HouseVal && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId).AddressID)); } else if (!String.IsNullOrEmpty(HouseNameTextbox.Text) && String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { context.Librarians.AddOrUpdate(s => new { s.PersonFirstName, s.PersonDateOfBirth }, new Librarian(FirstNameTextbox.Text, LastnameTextbox.Text, BirthdayTimePicker.Value, EmailTextbox.Text, PasswordTextbox.Text, SalaryVal, context.Addresses.FirstOrDefault(s => s.AddressHousename == HouseNameTextbox.Text && s.AddressStreetName == context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName&& s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId).AddressID)); } else if (String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && !String.IsNullOrEmpty(FlatNumberTextbox.Text)) { context.Librarians.AddOrUpdate(s => new { s.PersonFirstName, s.PersonDateOfBirth }, new Librarian(FirstNameTextbox.Text, LastnameTextbox.Text, BirthdayTimePicker.Value, EmailTextbox.Text, PasswordTextbox.Text, SalaryVal, context.Addresses.FirstOrDefault(s => s.AddressStreetName == context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName&& s.AddressHouseNumber == HouseVal && s.AddressFlatNumber == FlatVal && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId).AddressID)); } else if (String.IsNullOrEmpty(HouseNameTextbox.Text) && !String.IsNullOrEmpty(HouseNumberTextbox.Text) && String.IsNullOrEmpty(FlatNumberTextbox.Text)) { context.Librarians.AddOrUpdate(s => new { s.PersonFirstName, s.PersonDateOfBirth }, new Librarian(FirstNameTextbox.Text, LastnameTextbox.Text, BirthdayTimePicker.Value, EmailTextbox.Text, PasswordTextbox.Text, SalaryVal, context.Addresses.FirstOrDefault(s => s.AddressStreetName == context.Streets.FirstOrDefault(d => d.StreetName == StreetTextbox.Text).StreetName&& s.AddressHouseNumber == HouseVal && s.AddressPostCode == context.PostCodes.FirstOrDefault(p => p.FirstCodePart == AreacodeTextbox.Text && p.SecondCodePart == ArbitrarycodeTextbox.Text).PostCodeId).AddressID)); } context.SaveChanges(); } this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void LoadDataForEdit(String ItemName, String ItemAuthor) { try { using (LibraryApp.Models.LibraryDB context = new LibraryDB()) { if (context.BorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { BorrowAble ToEdit = context.BorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); TitleTextbox.Text = context.Titles.FirstOrDefault(s => s.TitleID == ToEdit.TitleID).TitleName; ISBNTextbox.Text = ToEdit.ISBN; OverviewTextbox.Text = ToEdit.Overview; PublisherTextbox.Text = context.Publishers.FirstOrDefault(s => s.PublisherName == ToEdit.PublisherName).PublisherName; GenreCombobox.SelectedIndex = GenreCombobox.Items.IndexOf(context.Genres.FirstOrDefault(g => g.GenreName == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).GenreName).GenreName); LanguageCombobox.SelectedIndex = LanguageCombobox.Items.IndexOf(context.Languages.FirstOrDefault(l => l.LanguageName == ToEdit.LanguageName).LanguageName); TypeCombobox.SelectedIndex = TypeCombobox.Items.IndexOf(context.Types.FirstOrDefault(t => t.TypeName == ToEdit.TypeName).TypeName); FormatCombobox.SelectedIndex = FormatCombobox.Items.IndexOf(context.Formats.FirstOrDefault(f => f.FormatName == ToEdit.FormatName).FormatName); CategoryCombobox.SelectedIndex = CategoryCombobox.Items.IndexOf("Borrowable"); PriceTextbox.Text = ToEdit.Price.ToString(); PublishedDateTimePicker.Value = ToEdit.PublishedDate; AuthorNameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonFirstName; AuthorSurnameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonLastName; AuthorBioTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).Biography; AuthorBirthdayTimePicker.Value = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonDateOfBirth; } else if (context.NonBorrowAbles.Any(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor)) { NonBorrowAble ToEdit = context.NonBorrowAbles.Single(s => context.Titles.FirstOrDefault(t => t.TitleID == s.TitleID).TitleName == ItemName && String.Concat(context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonFirstName, " ", context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(ti => ti.TitleID == s.TitleID).AuthorID).PersonLastName) == ItemAuthor); TitleTextbox.Text = context.Titles.FirstOrDefault(s => s.TitleID == ToEdit.TitleID).TitleName; ISBNTextbox.Text = ToEdit.ISBN; OverviewTextbox.Text = ToEdit.Overview; PublisherTextbox.Text = context.Publishers.FirstOrDefault(s => s.PublisherName == ToEdit.PublisherName).PublisherName; GenreCombobox.SelectedIndex = GenreCombobox.Items.IndexOf(context.Genres.FirstOrDefault(g => g.GenreName == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).GenreName).GenreName); LanguageCombobox.SelectedIndex = LanguageCombobox.Items.IndexOf(context.Languages.FirstOrDefault(l => l.LanguageName == ToEdit.LanguageName).LanguageName); TypeCombobox.SelectedIndex = TypeCombobox.Items.IndexOf(context.Types.FirstOrDefault(t => t.TypeName == ToEdit.TypeName).TypeName); FormatCombobox.SelectedIndex = FormatCombobox.Items.IndexOf(context.Formats.FirstOrDefault(f => f.FormatName == ToEdit.FormatName).FormatName); CategoryCombobox.SelectedIndex = CategoryCombobox.Items.IndexOf("Borrowable"); PriceTextbox.Text = ToEdit.Price.ToString(); PublishedDateTimePicker.Value = ToEdit.PublishedDate; AuthorNameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonFirstName; AuthorSurnameTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonLastName; AuthorBioTextbox.Text = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).Biography; AuthorBirthdayTimePicker.Value = context.Authors.FirstOrDefault(a => a.PersonId == context.Titles.FirstOrDefault(t => t.TitleID == ToEdit.TitleID).AuthorID).PersonDateOfBirth; } TitleTextbox.ReadOnly = true; ISBNTextbox.ReadOnly = true; PublisherTextbox.ReadOnly = true; LanguageCombobox.DropDownStyle = ComboBoxStyle.Simple; TypeCombobox.DropDownStyle = ComboBoxStyle.Simple; FormatCombobox.DropDownStyle = ComboBoxStyle.Simple; PriceTextbox.ReadOnly = true; PublishedDateTimePicker.Enabled = false; AuthorNameTextbox.ReadOnly = true; AuthorSurnameTextbox.ReadOnly = true; AuthorBirthdayTimePicker.Enabled = false; TitleTextbox.BorderStyle = BorderStyle.None; ISBNTextbox.BorderStyle = BorderStyle.None; PublisherTextbox.BorderStyle = BorderStyle.None; PriceTextbox.BorderStyle = BorderStyle.None; AuthorNameTextbox.BorderStyle = BorderStyle.None; AuthorSurnameTextbox.BorderStyle = BorderStyle.None; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// // GET: /search/ public ActionResult Index(String s) { List<String> resultList = new List<String>(); try { using (var db = new LibraryDB()) { var authorObj = (from a in db.Author where (a.FirstName.Contains(s) || a.LastName.Contains(s)) select a).ToList(); var bookObj = (from b in db.Book where b.Title.Contains(s) select b).ToList(); if (authorObj != null || bookObj != null) { foreach (var a in authorObj) { resultList.Add(a.FirstName + " " + a.LastName); } foreach (var b in bookObj) { resultList.Add(b.Title); } ViewBag.SearchResult = resultList; return View(); } else { return View(); // lists == null } } } catch (NullReferenceException e) { return View(); //s.length == 0 } }