public void LoadAnhPhu()
 {
     try
     {
         // Load Anh Phu
         Anh anhphu = new Anh();
         DataSet dsAnh = anhphu.SelectBySanPhamMauID(int.Parse(ViewState["ID"].ToString()));
         if (dsAnh.Tables[0].Rows.Count > 0)
         {
             foreach (DataRow drAnh in dsAnh.Tables[0].Rows)
             {
                 HtmlImage img = new HtmlImage();
                 img.Src = "." + drAnh["DuongDan"];
                 img.Height = 50;
                 img.Width = 50;
                 img.ID = "img" + drAnh["AnhID"];
                 img.Attributes.Add("onclick",
                                    "return XoaAnhPhu('" + drAnh["AnhID"] + "', '" + img.Src + "', this.ID, this);");
                 pnlAnhPhu.Controls.Add(img);
             }
         }
     }
     catch (Exception ex)
     {
         Response.Write(ex.ToString());
     }
 }
Beispiel #2
0
    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;
    }