private void TestConnection_Click(object sender, RoutedEventArgs e) { var server = ServerTextBox.Text; var database = DbTextBox.Text; var username = UsernameTextBox.Text; var password = PasswordTextBox.Password; var builder = new SqlConnectionStringBuilder(); builder.DataSource = server; builder.InitialCatalog = database; builder.UserID = username; builder.Password = password; var connectionString = builder.ConnectionString; //Thu connect voi db var db = new MyShopEntities(connectionString); var tc = db.TestConnection(); if (tc == true) { MessageBox.Show("Connect is OK"); //Chuyen toi man hinh chinh } else { MessageBox.Show("Connect was failed"); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); var c = db.Categories.ToList(); var qC = c.FirstOrDefault(i => i.Category_ID == _data.Catgory_ID); productCategoryComboBox.Items.Add(qC); productCategoryComboBox.SelectedIndex = 0; var temp = (Double)_data.Price; productNameTextbox.Text = _data.Product_Name.ToString(); productQuantityTextbox.Text = _data.Quantity.ToString(); productPriceTextbox.Text = temp.ToString(); this.DataContext = _data; var array = _data.Photos.First().Data as byte[]; using (var stream = new MemoryStream(array)) { var image = new BitmapImage(); image.BeginInit(); image.CreateOptions = BitmapCreateOptions.PreservePixelFormat; image.CacheOption = BitmapCacheOption.OnLoad; image.UriSource = null; image.StreamSource = stream; image.EndInit(); image.Freeze(); productPhoto.ImageSource = image; } }
public void UserControl_Initialized(object sender, EventArgs e) { var db = new MyShopEntities(); categoriesComboBox.ItemsSource = db.Categories.ToList(); categoriesComboBox.SelectedIndex = 0; sortComboBox.SelectedIndex = -1; filterComboBox.SelectedIndex = -1; keywordTextBox.Text = ""; }
public void Refresh() { var db = new MyShopEntities(); categoriesComboBox.ItemsSource = db.Categories.ToList(); categoriesComboBox.SelectedIndex = 0; sortComboBox.SelectedIndex = -1; filterComboBox.SelectedIndex = -1; keywordTextBox.Text = ""; }
private void editCategoryButton_Click(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); var category = db.Categories.Find(_data.Category_ID); if (!string.IsNullOrEmpty(categoryTextbox.Text.ToString())) { category.Category_Name = categoryTextbox.Text.ToString().ToUpper(); db.SaveChanges(); } DialogResult = true; }
private void editCategory(object sender, RoutedEventArgs e) { var _selectedCategoryIndex = categoriesComboBox.SelectedIndex; var qCategory = _categories[_selectedCategoryIndex]; var screen = new EditCategoryWindow(qCategory); if (screen.ShowDialog() == true) { var db = new MyShopEntities(); categoriesComboBox.ItemsSource = db.Categories.ToList(); categoriesComboBox.SelectedIndex = _selectedCategoryIndex; } }
private void addCategoryButton_Click(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); var NewCategoryInfo = new Category() { Category_Name = categoryTextbox.Text.ToString().ToUpper(), }; db.Categories.Add(NewCategoryInfo); db.SaveChanges(); DialogResult = true; }
private void deleteProduct(object sender, RoutedEventArgs e) { if (getId != -1) { var db = new MyShopEntities(); var products = db.Products.ToList(); var photos = db.Photos.ToList(); var qProduct = products.FirstOrDefault(i => i.Product_ID == getId); var qPhoto = photos.FirstOrDefault(i => i.Product_id == getId); db.Photos.Remove(qPhoto); db.SaveChanges(); db.Products.Remove(qProduct); db.SaveChanges(); CalculatePagingInfo(); UpdateProductView(); } }
private void editProduct(object sender, RoutedEventArgs e) { if (getId != -1) { var db = new MyShopEntities(); var products = db.Products.ToList(); var photos = db.Photos.ToList(); var qProduct = products.FirstOrDefault(i => i.Product_ID == getId); var qPhoto = photos.FirstOrDefault(i => i.Product_id == getId); var screen = new EditProductWindow(qProduct, qPhoto); if (screen.ShowDialog() == true) { CalculatePagingInfo(); UpdateProductView(); } } }
private void addProductButton_Click(object sender, RoutedEventArgs e) { if (_filename == "") { MessageBox.Show("Không được để trống hình ảnh"); return; } try { var db = new MyShopEntities(); NewProductInfo = new Product() { Product_Name = productNameTextbox.Text.ToString(), Price = int.Parse(productPriceTextbox.Text.ToString().Replace(",", "")), Quantity = int.Parse(productQuantityTextbox.Text.ToString()) }; var _selectedCategoryIndex = productCategoryComboBox.SelectedIndex; db.Categories.ToList()[_selectedCategoryIndex].Products.Add(NewProductInfo); db.SaveChanges(); var image = new BitmapImage(new Uri(_filename, UriKind.Absolute)); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); using (var stream = new MemoryStream()) { encoder.Save(stream); var photo = new Photo() { Product_id = NewProductInfo.Product_ID, Data = stream.ToArray() }; db.Photos.Add(photo); db.SaveChanges(); } DialogResult = true; } catch { MessageBox.Show("Không được để trống thông tin"); return; } }
private void editButton_Click(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); var product = db.Products.Find(_data.Product_ID); try { if (!string.IsNullOrEmpty(_filename)) { product.Product_Name = productNameTextbox.Text.ToString(); product.Price = int.Parse(productPriceTextbox.Text.ToString().Replace(",", "")); product.Quantity = int.Parse(productQuantityTextbox.Text.ToString()); db.SaveChanges(); var image = new BitmapImage(new Uri(_filename, UriKind.Absolute)); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); var photo = db.Photos.Find(_photo.Photo_id); using (var stream = new MemoryStream()) { encoder.Save(stream); photo.Data = stream.ToArray(); db.SaveChanges(); } DialogResult = true; } else { product.Product_Name = productNameTextbox.Text.ToString(); product.Price = int.Parse(productPriceTextbox.Text.ToString().Replace(",", "")); product.Quantity = int.Parse(productQuantityTextbox.Text.ToString()); db.SaveChanges(); DialogResult = true; } } catch { MessageBox.Show("Không được để trống thông tin"); return; } }
private void deleteCategory(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); var _selectedCategoryIndex = categoriesComboBox.SelectedIndex; var c = _categories[_selectedCategoryIndex]; var categories = db.Categories.ToList(); var qCategory = categories.FirstOrDefault(i => i.Category_ID == c.Category_ID); var products = db.Products.ToList(); var qProduct = products.FindAll(i => i.Catgory_ID == c.Category_ID); var photos = db.Photos.ToList(); foreach (var p in qProduct) { var qPhoto = photos.FirstOrDefault(i => i.Product_id == p.Product_ID); db.Photos.Remove(qPhoto); db.SaveChanges(); db.Products.Remove(p); db.SaveChanges(); } db.Categories.Remove(qCategory); db.SaveChanges(); Refresh(); }
void CalculatePagingInfo() { var db = new MyShopEntities(); _categories = new List <Category>(db.Categories); var selectedCategory = categoriesComboBox.SelectedItem as Category; if (selectedCategory == null) { selectedCategory = _categories[0]; } var products = db.Categories.Find(_categories[0].Category_ID).Products; bool alreadyExists = _categories.Any(x => x.Category_ID == selectedCategory.Category_ID); if (!alreadyExists) { products = db.Categories.Find(_categories[0].Category_ID).Products; } else { products = db.Categories.Find(selectedCategory.Category_ID).Products; } var keyword = keywordTextBox.Text; int start = Int32.MinValue; int end = Int32.MaxValue; var filter = filterComboBox.SelectedIndex; switch (filter) { case 0: end = 200000; break; case 1: start = 200000; end = 400000; break; case 2: start = 400000; end = 600000; break; case 3: start = 600000; break; default: break; } var query = from product in products where product.Product_Name.ToLower().Contains(keyword.ToLower()) && product.Price >= start && product.Price <= end select new { Id = product.Product_ID, Name = product.Product_Name, Price = product.Price, Quantity = product.Quantity, WhiteColor = product.Quantity > 10, GreenColor = product.Quantity < 10, Thumbnail = product.Photos .FirstOrDefault().Data }; var count = query.Count(); // Tính toán các thông tin phân trang var _totalProducts = count; // Tổng số sản phẩm var _totalPages = _totalProducts / _rowsPerPage; // Tổng số trang, chia lấy phần nguyên if (_totalProducts % _rowsPerPage != 0) // Nếu còn dư thì thêm một trang { _totalPages++; } _currentPage = 1; pagingInfo = new PagingInfo(_totalPages); pagesComboBox.ItemsSource = pagingInfo.Items; pagesComboBox.SelectedIndex = 0; totalProductTextBlock.Text = $"TOTAL PRODUCTS: {count} "; productsListView.ItemsSource = query.Take(_rowsPerPage); }
private async void loginButton_Click(object sender, RoutedEventArgs e) { if (textBoxUsername.Text.Length == 0) { MessageBox.Show("Bạn phải nhập tài khoản!!!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); textBoxUsername.Focus(); } else if (textBoxPassword.SecurePassword.Length == 0) { MessageBox.Show("Bạn phải nhập mật khẩu!!!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK); textBoxPassword.Focus(); } else { var connectionString = @"Data Source=.;Initial Catalog=MyShop;Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); //command.Connection = connection; string query = "Select * From Account Where username='******' AND Matkhau='" + textBoxPassword.Password + "'"; SqlCommand command = new SqlCommand(query, connection); SqlDataReader dta = command.ExecuteReader(); if (dta.Read() == true) { //Lay thong tin server trong appconfig var server = ConfigurationManager.AppSettings["server"]; var database = ConfigurationManager.AppSettings["database"]; var username = textBoxUsername.Text; var password = textBoxPassword.Password; var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (Check.IsChecked == true) { config.AppSettings.Settings["username"].Value = username; config.AppSettings.Settings["password"].Value = password; } else { config.AppSettings.Settings["username"].Value = ""; config.AppSettings.Settings["password"].Value = ""; config.AppSettings.Settings["entropy"].Value = ""; } config.Save(ConfigurationSaveMode.Minimal); ConfigurationManager.RefreshSection("appSettings"); var db = new MyShopEntities(); var account = db.Accounts.Find(username); MessageBox.Show($"{account.username} - Quyền truy cập {account.rolename}"); loginProgressBar.IsIndeterminate = true; for (int i = 3; i >= 1; i--) // Keep running progress Ring for 3 seconds { await Task.Delay(1000); } loginProgressBar.IsIndeterminate = false; if (account != null) { if (account.rolename == "admin") { var m = new MainWindow(); m.Show(); this.Close(); } else { var s = new SaleWindow(); s.Show(); this.Close(); } } else { MessageBox.Show($"Login failed!"); } } else { MessageBox.Show("Tài khoản hoặc mật khẩu không đúng!!!"); } connection.Close(); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { var db = new MyShopEntities(); productCategoryComboBox.ItemsSource = db.Categories.ToList(); }
private void importExecl_Click(object sender, RoutedEventArgs e) { var screen = new OpenFileDialog(); if (screen.ShowDialog() == true) { var filename = screen.FileName; var fileinfo = new FileInfo(filename); var excelFile = new Workbook(filename); var tabs = excelFile.Worksheets; var db = new MyShopEntities(); foreach (var tab in tabs) { var category = new Category() { Category_Name = tab.Name }; db.Categories.Add(category); db.SaveChanges(); var row = 3; var cell = tab.Cells[$"C3"]; while (cell.Value != null) { var product = new Product() { //SKU = tab.Cells[$"C{row}"].StringValue, Product_Name = tab.Cells[$"D{row}"].StringValue, Price = tab.Cells[$"E{row}"].IntValue, Quantity = tab.Cells[$"F{row}"].IntValue }; category.Products.Add(product); db.SaveChanges(); var imageName = tab.Cells[$"H{row}"].StringValue; var imageFull = $"{fileinfo.Directory}\\images\\{imageName}"; var image = new BitmapImage(new Uri(imageFull, UriKind.Absolute)); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); using (var stream = new MemoryStream()) { encoder.Save(stream); var photo = new Photo() { Product_id = product.Product_ID, Data = stream.ToArray() }; db.Photos.Add(photo); db.SaveChanges(); } row++; cell = tab.Cells[$"C{row}"]; } } MessageBox.Show("Data Imported"); UserControl_Initialized(sender, e); } }
private void UpdateProductView() { var db = new MyShopEntities(); var _selectedCategoryIndex = categoriesComboBox.SelectedIndex; if (_selectedCategoryIndex < 0) { _selectedCategoryIndex = 0; } if (_selectedCategoryIndex > _categories.Count() - 1) { _selectedCategoryIndex = 0; } var products = _categories[_selectedCategoryIndex].Products; var keyword = keywordTextBox.Text; int start = Int32.MinValue; int end = Int32.MaxValue; var filter = filterComboBox.SelectedIndex; switch (filter) { case 0: end = 200000; break; case 1: start = 200000; end = 400000; break; case 2: start = 400000; end = 600000; break; case 3: start = 600000; break; default: break; } var query = from product in products where product.Product_Name.ToLower().Contains(keyword.ToLower()) && product.Price >= start && product.Price <= end select new { Id = product.Product_ID, Name = product.Product_Name, Price = product.Price, Quantity = product.Quantity, WhiteColor = product.Quantity > 10, GreenColor = product.Quantity < 10, Thumbnail = product.Photos .FirstOrDefault().Data }; var skip = (_currentPage - 1) * _rowsPerPage; var take = _rowsPerPage; var sort = sortComboBox.SelectedIndex; switch (sort) { case 0: productsListView.ItemsSource = query.OrderBy(q => q.Price).Skip(skip).Take(take); break; case 1: productsListView.ItemsSource = query.OrderByDescending(q => q.Price).Skip(skip).Take(take); break; case 2: productsListView.ItemsSource = query.OrderBy(q => q.Name).Skip(skip).Take(take); break; case 3: productsListView.ItemsSource = query.OrderByDescending(q => q.Name).Skip(skip).Take(take); break; default: productsListView.ItemsSource = query.Skip(skip).Take(take); break; } }