30 06
30 06
using System.ComponentModel.DataAnnotations;
namespace QLBH.Models
public Category()
[StringLength(100)]
Product
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace QLBH.Models
[StringLength(200)]
[StringLength(200)]
namespace QLBH
public fManageProduct()
InitializeComponent();
cbCategories.DisplayMember = "Name";
cbCategories.ValueMember = "CategoryID";
}
}
cbCategories_SelectedIndexChanged(sender, e);
if (ckCategory.Checked)
}).ToList();
else
if (Utility.IsOpeningForm("fNewProduct"))
using QLBH.Models;
namespace QLBH
public fNewProduct()
InitializeComponent();
mQuantity.ValidatingType = typeof(UInt64);
mPrice.ValidatingType = typeof(decimal);
mMarketPrice.ValidatingType = typeof(decimal);
cbCategories.DisplayMember = "Name";
cbCategories.ValueMember = "CategoryID";
File|*.bmp;*.dib|JPEG|*.jpg;*.jpe;*.jpeg;*.jfif|GIF|*.gif|TIFF|*.tif;*.tiff|PNG|*
.png|ICO|*.ico";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
txtImageFile.Text = openFileDialog1.FileName;
pictureBox1.ImageLocation = openFileDialog1.FileName;
if (string.IsNullOrWhiteSpace(txtName.Text))
txtName.Focus();
return;
if (string.IsNullOrWhiteSpace(cbCategories.Text))
1000);
cbCategories.Focus();
return;
}
if (string.IsNullOrWhiteSpace(mQuantity.Text))
mQuantity.Focus();
return;
if (string.IsNullOrWhiteSpace(mPrice.Text))
mPrice.Focus();
return;
try
product = new Product(); //Tạo một thể hiện cho sản phẩm mới
product.Name = txtName.Text;
product.Quantity = Convert.ToInt32(mQuantity.Text);
product.Price = Convert.ToDecimal(mPrice.Text);
product.MarketPrice =
string.IsNullOrWhiteSpace(mMarketPrice.Text) ? (decimal?)null :
Convert.ToDecimal(mMarketPrice.Text);
product.Description = rDescription.Text;
product.Status = ckStatus.Checked;
product.CategoryID = Convert.ToInt64(cbCategories.SelectedValue);
//Lưu hình vào thư mục hình và tên tập tin hình vào csdl
if (!string.IsNullOrWhiteSpace(txtImageFile.Text))
string ext =
txtImageFile.Text.Substring(txtImageFile.Text.LastIndexOf("."),
txtImageFile.Text.Length - txtImageFile.Text.LastIndexOf("."));
pictureBox1.Image.Save(Utility.ImagePath +
product.ProductID + ext);
txtName.Text = null;
//cbCategories.Text = null;
mQuantity.Text = null;
mPrice.Text = null;
mMarketPrice.Text = null;
rDescription.Text = null;
ckStatus.Checked = true;
txtImageFile.Text = null;
pictureBox1.ImageLocation = null;
1000);
txtName.Focus();
e)
e.Cancel = false;
System.ComponentModel.CancelEventArgs e)
if (string.IsNullOrWhiteSpace(txtName.Text))
{
e.Cancel = true;
1000);
e.Cancel = true;
System.ComponentModel.CancelEventArgs e)
pictureBox1.Image = null;
if (string.IsNullOrWhiteSpace(txtImageFile.Text))
0, 1000);
e.Cancel = true;
}
else if (!File.Exists(txtImageFile.Text))
toolTip1.Show("Tên tập tin sai hoặc Tập tin không tồn tại?",
txtImageFile, 0, 0, 1000);
e.Cancel = true;
Else
pictureBox1.ImageLocation = txtImageFile.Text;
System.ComponentModel.CancelEventArgs e)
tên loại
1000);
e.Cancel = true;
TypeValidationEventArgs e)
{
if (!e.IsValidInput) //Nếu nhập sai kiểu
1000);
e.Cancel = true;
else
e.Cancel = true;
TypeValidationEventArgs e)
if (!e.IsValidInput)
e.Cancel = true;
else
if ((decimal)e.ReturnValue < 0)
{
e.Cancel = true;
TypeValidationEventArgs e)
if (string.IsNullOrWhiteSpace(mMarketPrice.Text))
if (!e.IsValidInput)
1000);
e.Cancel = true;
else
if ((decimal)e.ReturnValue < 0)
e.Cancel = true;
}
}