private bool LuuSanPham() { SanPham sanpham = new SanPham(); try { string relativePath = ""; DateTime? NgayBDKM = null; DateTime? NgayKTKM = null; string strMoTaKhuyenMai = null; string path = Server.MapPath("../Upload/ProdImages"); decimal? giamoi = null; string MaSoSanPham = null; int spmid = 0; if (Request.QueryString["spmid"] != null) { spmid = int.Parse(Request.QueryString["spmid"]); } if (ViewState["Action"].ToString() == "ADD") { MaSoSanPham = GenerateMaSoSanPham(); } if (chkKhuyenMai.Checked) { NgayBDKM = DateTime.Parse(wdcBatDauKM.Value.ToString()); NgayKTKM = DateTime.Parse(wdcKetThucKM.Value.ToString()); txtMoTaKhuyenMai.Enabled = true; strMoTaKhuyenMai = txtMoTaKhuyenMai.Text; } if (chkGiamGia.Checked) { txtGiaMoi.Enabled = true; cvGiaMoi.Enabled = true; giamoi = decimal.Parse(txtGiaMoi.Text); } if (fileAnhChinh.PostedFile.FileName != "") { try { if ((fileAnhChinh.PostedFile.ContentLength <= 100000) || (Common.LoaiNguoiDungID() == 3)) { string randomString = ""; path += "\\" + Common.NguoiDungID(); relativePath = "./Upload/ProdImages" + "/" + Common.NguoiDungID() + "/"; int pos = fileAnhChinh.PostedFile.FileName.LastIndexOf('\\'); string absolutePath = path + "\\" + fileAnhChinh.PostedFile.FileName.Remove(0, pos + 1); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (File.Exists(absolutePath)) { randomString = DateTime.Now.Ticks + "_"; absolutePath = path + "\\" + randomString + fileAnhChinh.PostedFile.FileName.Remove(0, pos + 1); } if (Common.LoaiNguoiDungID() != 3) Common.ResizeFromStream(absolutePath, 320, fileAnhChinh.PostedFile.InputStream); //fileAnhChinh.PostedFile.SaveAs(absolutePath); relativePath += randomString + fileAnhChinh.PostedFile.FileName.Remove(0, pos + 1); if (ViewState["Action"].ToString() == "ADD") { ViewState["ID"] = sanpham.InsertFields(txtTenSanPham.Text, MaSoSanPham, decimal.Parse(txtGiaSanPham.Text), "VNĐ", wheMoTaSanPham.Text, wheThongTinSanPham.Text, int.Parse(ddlNhomSanPham.SelectedValue), int.Parse(ddlHangSanXuat.SelectedValue), Common.NguoiDungID(), relativePath, chkKhuyenMai.Checked, strMoTaKhuyenMai, giamoi, NgayBDKM, NgayKTKM, null, int.Parse(ddlKhuVuc.SelectedValue), null, null, chkGiamGia.Checked, null, DateTime.Now, null, null, null, 0, null, null, null, null, spmid, DateTime.Now, txtTenPhu.Text); } else if (ViewState["Action"].ToString() == "EDIT") { sanpham.UpdateFields(int.Parse(ViewState["ID"].ToString()), txtTenSanPham.Text, null, decimal.Parse(txtGiaSanPham.Text), null, wheMoTaSanPham.Text, wheThongTinSanPham.Text, int.Parse(ddlNhomSanPham.SelectedValue), int.Parse(ddlHangSanXuat.SelectedValue), Common.NguoiDungID(), relativePath, chkKhuyenMai.Checked, strMoTaKhuyenMai, giamoi, NgayBDKM, NgayKTKM, null, int.Parse(ddlKhuVuc.SelectedValue), null, null, chkGiamGia.Checked, null, null, null, null, null, null, null, null, null, null, null, DateTime.Now, txtTenPhu.Text); } imgAnhChinh.ImageUrl = "." + relativePath; } else { lblAnhChinhErr.Text = "Kích cỡ ảnh quá lớn!"; return false; } } catch (Exception ex) { Response.Redirect("../message.aspx?msg=" + ex.Message.Replace("\r\n", ""), false); } } else { if (Request.QueryString["spmid"] != null) { if (hidAnhChinh.Value != "") { relativePath = hidAnhChinh.Value; } else { relativePath = null; } } if (ViewState["Action"].ToString() == "ADD") { ViewState["ID"] = sanpham.InsertFields(txtTenSanPham.Text, MaSoSanPham, decimal.Parse(txtGiaSanPham.Text), "VNĐ", wheMoTaSanPham.Text, wheThongTinSanPham.Text, int.Parse(ddlNhomSanPham.SelectedValue), int.Parse(ddlHangSanXuat.SelectedValue), Common.NguoiDungID(), relativePath, chkKhuyenMai.Checked, strMoTaKhuyenMai, giamoi, NgayBDKM, NgayKTKM, null, int.Parse(ddlKhuVuc.SelectedValue), null, null, chkGiamGia.Checked, null, DateTime.Now, null, null, null, 0, null, null, null, null, spmid, DateTime.Now, txtTenPhu.Text); } else if (ViewState["Action"].ToString() == "EDIT") { sanpham.UpdateFields(int.Parse(ViewState["ID"].ToString()), txtTenSanPham.Text, null, decimal.Parse(txtGiaSanPham.Text), null, wheMoTaSanPham.Text, wheThongTinSanPham.Text, int.Parse(ddlNhomSanPham.SelectedValue), int.Parse(ddlHangSanXuat.SelectedValue), Common.NguoiDungID(), null, chkKhuyenMai.Checked, strMoTaKhuyenMai, giamoi, NgayBDKM, NgayKTKM, null, int.Parse(ddlKhuVuc.SelectedValue), null, null, chkGiamGia.Checked, null, null, null, null, null, null, null, null, null, null, null, DateTime.Now, txtTenPhu.Text); } } if (Request.QueryString["spmid"] != null) { //CuaHangNhomSanPham chnsp = new CuaHangNhomSanPham(); //DataSet dsnsp = chnsp.s Anh anh = new Anh(); DataSet dsAnh = anh.SelectBySanPhamMauID(int.Parse(Request.QueryString["spmid"])); for (int i = 0; i < dsAnh.Tables[0].Rows.Count; i++) { DataRow dr = dsAnh.Tables[0].Rows[i]; anh.InsertFields(int.Parse(ViewState["ID"].ToString()), dr["DuongDan"].ToString(), dr["Bak1"].ToString() , dr["Bak2"].ToString(), int.Parse("0" + dr["Bak3"]), int.Parse(Request.QueryString["spmid"])); } } LuuThuocTinhSanPham(); } catch (Exception ex) { Response.Write(ex.ToString()); } return true; }
protected void btnUpLoad_Click(object sender, EventArgs e) { if (fileAnhPhu.PostedFile.FileName != "") { string relativePath = ""; string path = Server.MapPath("../Upload/ProdImages"); //path += "\\" + Common.NguoiDungID.ToString(); try { if (fileAnhPhu.PostedFile.ContentLength <= 100000) { string randomString = ""; path += "\\" + Common.NguoiDungID(); relativePath = "./Upload/ProdImages" + "/" + Common.NguoiDungID() + "/"; int pos = fileAnhPhu.PostedFile.FileName.LastIndexOf('\\'); string absolutePath = path + "\\" + fileAnhPhu.PostedFile.FileName.Remove(0, pos + 1); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (File.Exists(absolutePath)) { randomString = DateTime.Now.Ticks + "_"; absolutePath = path + "\\" + randomString + fileAnhPhu.PostedFile.FileName.Remove(0, pos + 1); } //fileAnhPhu.PostedFile.SaveAs(absolutePath); Common.ResizeFromStream(absolutePath, 320, fileAnhPhu.PostedFile.InputStream); relativePath += randomString + fileAnhPhu.PostedFile.FileName.Remove(0, pos + 1); try { Anh anhphu = new Anh(); anhphu.InsertFields(int.Parse(ViewState["ID"].ToString()), relativePath, null, null, null, null); LoadAnhPhu(); } catch (Exception ex) { Response.Write(ex.ToString()); } } else { lblAnhPhuErr.Text = "Kích cỡ ảnh quá lớn!"; LoadAnhPhu(); return; //Response.Redirect("../message.aspx?msg=File size is too big!", false); } } catch (Exception ex) { Response.Redirect("../message.aspx?msg=" + ex.Message.Replace("\r\n", ""), false); } } }