void confirm_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txtName.Text)) { MessageBox.Show("Name of the book is required"); txtName.Focus(); } else if (String.IsNullOrEmpty(txtAuthor.Text)) { MessageBox.Show("Name of the Author is required"); txtAuthor.Focus(); } else { using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { try { //Add the details to the Tables TblBooks st = new TblBooks() { name = txtName.Text, author = txtAuthor.Text, description = txtDesc.Text, version = txtVersion.Text }; context.TblBooks.InsertOnSubmit(st); context.SubmitChanges(); MessageBox.Show("Book has been Added Successfully"); } catch (Exception) { MessageBox.Show("Unable to add book"); } } } }
private void confirm_Click(object sender, EventArgs e) { //throw new NotImplementedException(); using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { var BookLoaded = (from book in context.TblBooks where book.id == Int32.Parse(txtID.Text) select book).FirstOrDefault(); if (BookLoaded != null) { try { //Adding changes into the database String id = NavigationContext.QueryString["id"]; BookLoaded.author = txtAuthor.Text; BookLoaded.description = txtDesc.Text; BookLoaded.name = txtName.Text; BookLoaded.version = txtVersion.Text; context.SubmitChanges(); MessageBox.Show("Book Editted successfully"); //Move back to the view page NavigationService.Navigate(new Uri("/ViewBook.xaml?id=" + id, UriKind.RelativeOrAbsolute)); } catch (Exception) { MessageBox.Show("Unable to Edit the book"); } } } }
//delete book by id public void DeleteUser(String id) { using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { IQueryable <TblBooks> entityQuery = from c in context.TblBooks where c.id.Equals(id) select c; TblBooks entityToDelete = entityQuery.FirstOrDefault(); context.TblBooks.DeleteOnSubmit(entityToDelete); context.SubmitChanges(); } }
//Load books from the database to the list private void LoadBooks() { IList <TblBooks> list = null; using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { IQueryable <TblBooks> query = from c in context.TblBooks select c; list = query.ToList(); bookList.ItemsSource = list; } }
// Constructor public MainPage() { InitializeComponent(); //Check if the database exists using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { if (!context.DatabaseExists()) { context.CreateDatabase(); MessageBox.Show("You're Welcome to add new Books"); } else { MessageBox.Show("List of books already Exists"); } } // Sample code to localize the ApplicationBar //BuildLocalizedApplicationBar(); ApplicationBar = new ApplicationBar(); //add button ApplicationBarIconButton add = new ApplicationBarIconButton(); add.Text = "Add new"; add.IconUri = new Uri("/icons/add.png", UriKind.Relative); ApplicationBar.Buttons.Add(add); add.Click += add_Click; //clear button ApplicationBarIconButton clear = new ApplicationBarIconButton(); clear.Text = "Clear"; clear.IconUri = new Uri("/icons/cancel.png", UriKind.Relative); ApplicationBar.Buttons.Add(clear); clear.Click += clear_Click; //confirm button ApplicationBarIconButton confirm = new ApplicationBarIconButton(); confirm.Text = "Login"; confirm.IconUri = new Uri("/icons/check.png", UriKind.Relative); ApplicationBar.Buttons.Add(confirm); confirm.Click += confirm_Click; //About us... Menu item ApplicationBarMenuItem about = new ApplicationBarMenuItem(); about.Text = "About us"; ApplicationBar.MenuItems.Add(about); about.Click += about_Click; }
protected override void OnNavigatedTo(NavigationEventArgs e) { //Getting the book id from the former page String id = NavigationContext.QueryString["id"]; using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { var BookLoaded = (from book in context.TblBooks where book.id == Int32.Parse(id) select book).FirstOrDefault(); if (BookLoaded != null) { //Loading book details in their respective textboxes txtID.Text = BookLoaded.id.ToString(); txtAuthor.Text = BookLoaded.author; txtDesc.Text = BookLoaded.description; txtName.Text = BookLoaded.name; txtVersion.Text = BookLoaded.version; } } }
void confirm_Click(object sender, EventArgs e) { //validate the form if (String.IsNullOrEmpty(txtName.Text)) { MessageBox.Show("Name is required"); txtName.Focus(); } else if (String.IsNullOrEmpty(txtUname.Text)) { MessageBox.Show("Username is required"); txtUname.Focus(); } else if (String.IsNullOrEmpty(txtPasswd.Password)) { MessageBox.Show("Password is required"); txtPasswd.Focus(); } else { using (BooksDatacontext context = new BooksDatacontext(App.booksconnectionString)) { try { //Add user information in the database table TblAccounts TblAccounts tb = new TblAccounts(); tb.name = txtName.Text; tb.username = txtUname.Text; tb.passwd = txtPasswd.Password; context.TblAccounts.InsertOnSubmit(tb); context.SubmitChanges(); MessageBox.Show("Account Created Successfully"); } catch (Exception) { MessageBox.Show("unable to Create an Account"); } } } }