Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.IO;
- using Microsoft.Win32;
- namespace WPF2
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// CRUD
- /// FILES
- /// FILES WITH DATABASE
- /// CLOSE HIDE MULTIPLE WINDOWS
- /// LOGIN
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- WPFCRUD1Entities1 context = new WPFCRUD1Entities1();
- User usr = new User();
- //Save
- private void btnSave_Click(object sender, RoutedEventArgs e)
- {
- usr.Username = txtUser.Text;
- usr.Pass = txtPass.Password;
- context.Users.Add(usr);
- context.SaveChanges();
- }
- //Update
- private void btnUpdate_Click(object sender, RoutedEventArgs e)
- {
- int id = int.Parse(txtSearch.Text);
- bool isFound = context.Users.Where(x => x.ID == id).Any();
- if (isFound)
- {
- usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
- usr.Pass = txtPass.Password;
- usr.Username = txtUser.Text;
- context.SaveChanges();
- MessageBox.Show("Data Updated");
- }
- else
- {
- MessageBox.Show("Update Failed");
- }
- }
- private void btnDelete_Click(object sender, RoutedEventArgs e)
- {
- int id = int.Parse(txtSearch.Text);
- bool isFound = context.Users.Where(x => x.ID == id).Any();
- if (isFound)
- {
- usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
- context.Users.Remove(usr);
- context.SaveChanges();
- MessageBox.Show("Record Deleted");
- }
- else
- {
- MessageBox.Show("No ID found");
- }
- }
- private void btnSearch_Click(object sender, RoutedEventArgs e)
- {
- int id = int.Parse(txtSearch.Text);
- bool isFound = context.Users.Where(x => x.ID == id).Any();
- if (isFound)
- {
- usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
- txtUser.Text = usr.Username;
- txtPass.Password = usr.Pass;
- }
- else
- MessageBox.Show("Record Not Found");
- }
- private void ckShowGrid_Checked(object sender, RoutedEventArgs e)
- {
- var data = context.Users.ToList();
- dataGrid.ItemsSource = data;
- }
- private void ckShowGrid_Unchecked(object sender, RoutedEventArgs e)
- {
- dataGrid.ItemsSource = null;
- }
- private void btnOpenFile_Click(object sender, RoutedEventArgs e)
- {
- System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
- open.Title = "Open File";
- open.Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*";
- if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- StreamReader read = new StreamReader(File.OpenRead(open.FileName));
- txtUpload.Text = read.ReadToEnd();
- read.Dispose();
- }
- }
- private void btnSaveFile_Click(object sender, RoutedEventArgs e)
- {
- System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
- save.Title = "Save File";
- save.Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*";
- if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- StreamWriter write = new StreamWriter(File.Create(save.FileName));
- write.Write(txtUpload.Text);
- write.Dispose();
- }
- }
- private void btnDbFile_Click(object sender, RoutedEventArgs e)
- {
- DBFILE win2 = new DBFILE();
- win2.Show();
- }
- //POP-UP OPEN
- private void Show_Click(object sender, RoutedEventArgs e)
- {
- MyPopup.IsOpen = true;
- }
- //POP-UP CLOSE / CLICKING OUTSIDE ITS DONE IN XAML WITH STAYSOPEN
- private void Hide_Click(object sender, RoutedEventArgs e)
- {
- MyPopup.IsOpen = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement