void getData()
        {
            List <Entity.DatHang> lst = new List <Entity.DatHang>();

            lst = DatHangService.DatHang_GetByTop("", "", "");
            grvDatHang.DataSource = lst;
            grvDatHang.DataBind();
        }
        protected void btndathang_Click(object sender, EventArgs e)
        {
            List <Entity.KhachHang> lstKh = new List <Entity.KhachHang>();

            Entity.DatHang        dh    = new Entity.DatHang();
            List <Entity.DatHang> lstdh = new List <Entity.DatHang>();

            Entity.ChiTietDatHang lstctdh = new Entity.ChiTietDatHang();
            string Id_SP;

            if (txtdiachi.Text == "" || txtdienthoai.Text == "" || txtemail.Text == "" || txthovaten.Text == "" || ddlquan.SelectedIndex == 0 || ddlthanhpho.SelectedIndex == 0)
            {
                Response.Write("<script>alert('Bạn phải nhập đầy đủ thông tin!!1')</script>");
                return;
            }
            else if (Session["Link_Fb"] == null)
            {
                if (Session["TenDangNhap"] == null)
                {
                    Entity.KhachHang obj = new Entity.KhachHang();

                    obj.TenKH    = txthovaten.Text;
                    obj.TaiKhoan = "";
                    obj.MatKhau  = "";
                    obj.NgaySinh = "";
                    obj.GioiTinh = "True";

                    obj.Email   = txtemail.Text;
                    obj.SDT     = txtdienthoai.Text;
                    obj.DiaChi  = txtdiachi.Text;
                    obj.Tinh    = ddlthanhpho.SelectedItem.ToString();
                    obj.Quan    = ddlquan.SelectedItem.ToString();
                    obj.Link_Fb = "false";
                    KhachHangService.KhachHang_Insert(obj);
                    lstKh           = KhachHangService.KhachHang_GetByTop("", " id=(SELECT MAX(id) from tbl_KhachHang) ", "");
                    dh.Id_KhachHang = lstKh[0].Id;
                    dh.NgaylapHD    = DateTime.Now.ToString();
                    dh.IdPay        = ddlhinhthucthanhtoan.SelectedValue.ToString();
                    DatHangService.DatHang_Insert(dh);
                    lstdh             = DatHangService.DatHang_GetByTop("", " id=(SELECT MAX(id) from tbl_DatHang)", "");
                    lstctdh.Id_hoadon = lstdh[0].Id;
                    for (int i = 0; i < tbGioHang.Rows.Count; i++)
                    {
                        string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                        Id_SP              = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;
                        lstctdh.Id_hang    = Id_SP;
                        lstctdh.Size       = tbGioHang.Rows[i]["Size"].ToString();
                        lstctdh.SoLuongMua = tbGioHang.Rows[i]["SoLuong"].ToString();
                        lstctdh.ThanhTien  = tbGioHang.Rows[i]["TongGia"].ToString();
                        lstctdh.Gia        = tbGioHang.Rows[i]["Gia"].ToString();
                        ChiTietDatHangService.ChiTietDatHang_Insert(lstctdh);
                    }
                }
                else
                {
                    string TenDangNhap = Session["TenDangNhap"].ToString();

                    lstKh = KhachHangService.KhachHang_GetByTop("", " TaiKhoan='" + TenDangNhap + "'", "");
                    Entity.KhachHang kh = new Entity.KhachHang();
                    kh.Id     = lstKh[0].Id;
                    kh.TenKH  = txthovaten.Text;
                    kh.Email  = txtemail.Text;
                    kh.DiaChi = txtdiachi.Text;
                    kh.SDT    = txtdienthoai.Text;
                    kh.Tinh   = ddlthanhpho.SelectedItem.ToString();
                    kh.Quan   = ddlquan.SelectedItem.ToString();
                    KhachHangService.KhachHang_UpdateDatHang(kh);

                    dh.NgaylapHD    = DateTime.Now.ToString();
                    dh.IdPay        = ddlhinhthucthanhtoan.SelectedValue.ToString();
                    dh.Id_KhachHang = kh.Id;
                    DatHangService.DatHang_Insert(dh);

                    lstdh = DatHangService.DatHang_GetByTop("", " id=(SELECT MAX(id) from tbl_DatHang where id_KhachHang=" + kh.Id + ")", "");


                    lstctdh.Id_hoadon = lstdh[0].Id;
                    for (int i = 0; i < tbGioHang.Rows.Count; i++)
                    {
                        string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                        Id_SP              = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;
                        lstctdh.Id_hang    = Id_SP;
                        lstctdh.Size       = tbGioHang.Rows[i]["Size"].ToString();
                        lstctdh.SoLuongMua = tbGioHang.Rows[i]["SoLuong"].ToString();
                        lstctdh.ThanhTien  = tbGioHang.Rows[i]["TongGia"].ToString();
                        lstctdh.Gia        = tbGioHang.Rows[i]["Gia"].ToString();
                        ChiTietDatHangService.ChiTietDatHang_Insert(lstctdh);
                    }
                }
            }
            else
            {
                string Link_Fb = Session["Link_Fb"].ToString();

                lstKh = KhachHangService.KhachHang_GetByTop("", " Link_Fb='" + Link_Fb + "' ", "");
                Entity.KhachHang kh = new Entity.KhachHang();
                kh.Id     = lstKh[0].Id;
                kh.TenKH  = txthovaten.Text;
                kh.Email  = txtemail.Text;
                kh.DiaChi = txtdiachi.Text;
                kh.SDT    = txtdienthoai.Text;
                kh.Tinh   = ddlthanhpho.SelectedItem.ToString();
                kh.Quan   = ddlquan.SelectedItem.ToString();
                KhachHangService.KhachHang_UpdateDatHang(kh);

                dh.NgaylapHD    = DateTime.Now.ToString();
                dh.IdPay        = ddlhinhthucthanhtoan.SelectedValue.ToString();
                dh.Id_KhachHang = kh.Id;
                DatHangService.DatHang_Insert(dh);

                lstdh = DatHangService.DatHang_GetByTop("", " id=(SELECT MAX(id) from tbl_DatHang where id_KhachHang=" + kh.Id + ")", "");


                lstctdh.Id_hoadon = lstdh[0].Id;
                for (int i = 0; i < tbGioHang.Rows.Count; i++)
                {
                    string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                    Id_SP              = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;
                    lstctdh.Id_hang    = Id_SP;
                    lstctdh.Size       = tbGioHang.Rows[i]["Size"].ToString();
                    lstctdh.SoLuongMua = tbGioHang.Rows[i]["SoLuong"].ToString();
                    lstctdh.ThanhTien  = tbGioHang.Rows[i]["TongGia"].ToString();
                    lstctdh.Gia        = tbGioHang.Rows[i]["Gia"].ToString();
                    ChiTietDatHangService.ChiTietDatHang_Insert(lstctdh);
                }
            }
            Response.Write("<script>alert('Đặt hàng thành công!!')</script>");
            Session["GioHang"] = null;
            lblsl                 = (Label)Master.FindControl("lblslgiohang");
            lblsl.Text            = "0";
            lbltongtien.Text      = "0 VNĐ";
            tbGioHang             = null;
            grvsanpham.DataSource = tbGioHang;
            grvsanpham.DataBind();
        }
Esempio n. 3
0
        protected void btndathang_Click(object sender, EventArgs e)
        {
            List <Entity.KhachHang> lstKh = new List <Entity.KhachHang>();

            Entity.DatHang        dh    = new Entity.DatHang();
            List <Entity.DatHang> lstdh = new List <Entity.DatHang>();

            Entity.ChiTietDatHang lstctdh = new Entity.ChiTietDatHang();
            string Id_SP;

            if (txtdiachi.Text == "" || txtdienthoai.Text == "" || txtemail.Text == "" || txthovaten.Text == "") //|| ddlquan.SelectedIndex == 0 || ddlthanhpho.SelectedIndex == 0
            {
                Response.Write("<script>alert('" + ddlquan.SelectedValue + "')</script>");
                Response.Write("<script>alert('Bạn phải nhập đầy đủ thông tin!!1')</script>");
                return;
            }
            else
            {
                if (Session["TenDangNhap"] == null)
                {
                    Entity.KhachHang obj = new Entity.KhachHang();
                    //Response.Write("<script>alert('Vào đây')</script>");
                    obj.TenKH    = txthovaten.Text;
                    obj.TaiKhoan = "";
                    obj.MatKhau  = "";
                    obj.NgaySinh = "";
                    obj.GioiTinh = "True";

                    obj.Email   = txtemail.Text;
                    obj.SDT     = txtdienthoai.Text;
                    obj.DiaChi  = txtdiachi.Text;
                    obj.Tinh    = ddlthanhpho.SelectedItem.ToString();
                    obj.Quan    = ddlquan.SelectedItem.ToString();
                    obj.Link_Fb = "false";
                    KhachHangService.KhachHang_Insert(obj);
                    lstKh = KhachHangService.KhachHang_GetByTop("", " id=(SELECT MAX(id) from tbl_KhachHang) ", "");

                    tbl_DatHang dhs = new tbl_DatHang();
                    dhs.id_KhachHang = int.Parse(lstKh[0].Id);
                    dhs.NgaylapHD    = DateTime.Now;
                    dhs.IdPay        = int.Parse(ddlhinhthucthanhtoan.SelectedValue);
                    ///////////////////////////////////////////////////////////////////
                    dbs.tbl_DatHangs.InsertOnSubmit(dhs);
                    dbs.SubmitChanges();
                    lstdh             = DatHangService.DatHang_GetByTop("", " id=(SELECT MAX(id) from tbl_DatHang)", "");
                    lstctdh.Id_hoadon = lstdh[0].Id;
                    for (int i = 0; i < tbGioHang.Rows.Count; i++)
                    {
                        string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                        Id_SP              = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;
                        lstctdh.Id_hang    = Id_SP;
                        lstctdh.Size       = tbGioHang.Rows[i]["Size"].ToString();
                        lstctdh.SoLuongMua = tbGioHang.Rows[i]["SoLuong"].ToString();
                        lstctdh.ThanhTien  = tbGioHang.Rows[i]["TongGia"].ToString();
                        lstctdh.Gia        = tbGioHang.Rows[i]["Gia"].ToString();
                        ChiTietDatHangService.ChiTietDatHang_Insert(lstctdh);
                    }
                }
                else
                {
                    string TenDangNhap = Session["TenDangNhap"].ToString();

                    lstKh = KhachHangService.KhachHang_GetByTop("", " TaiKhoan='" + TenDangNhap + "'", "");
                    Entity.KhachHang kh = new Entity.KhachHang();
                    kh.Id     = lstKh[0].Id;
                    kh.TenKH  = txthovaten.Text;
                    kh.Email  = txtemail.Text;
                    kh.DiaChi = txtdiachi.Text;
                    kh.SDT    = txtdienthoai.Text;
                    kh.Tinh   = ddlthanhpho.SelectedItem.ToString();
                    kh.Quan   = ddlquan.SelectedItem.ToString();
                    KhachHangService.KhachHang_UpdateDatHang(kh);

                    tbl_DatHang dhs = new tbl_DatHang();
                    dhs.id_KhachHang = int.Parse(kh.Id);
                    dhs.NgaylapHD    = DateTime.Now;
                    dhs.IdPay        = int.Parse(ddlhinhthucthanhtoan.SelectedValue);
                    ///////////////////////////////////////////////////////////////////
                    dbs.tbl_DatHangs.InsertOnSubmit(dhs);
                    dbs.SubmitChanges();

                    lstdh = DatHangService.DatHang_GetByTop("", " id=(SELECT MAX(id) from tbl_DatHang where id_KhachHang=" + kh.Id + ")", "");

                    lstctdh.Id_hoadon = lstdh[0].Id;
                    for (int i = 0; i < tbGioHang.Rows.Count; i++)
                    {
                        string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                        Id_SP              = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;
                        lstctdh.Id_hang    = Id_SP;
                        lstctdh.Size       = tbGioHang.Rows[i]["Size"].ToString();
                        lstctdh.SoLuongMua = tbGioHang.Rows[i]["SoLuong"].ToString();
                        lstctdh.ThanhTien  = tbGioHang.Rows[i]["TongGia"].ToString();
                        lstctdh.Gia        = tbGioHang.Rows[i]["Gia"].ToString();
                        ChiTietDatHangService.ChiTietDatHang_Insert(lstctdh);
                    }
                }
            }

            Response.Write("<script>alert('Đặt hàng thành công!! Cảm ơn bạn nhiều nhé!! ')</script>");

            // MailMessage message = new MailMessage("*****@*****.**",txtemail.Text, "Shop Fashion(Clothing Store) ",grvsanpham.Rows[0].Cells[0].Text);
            // message.IsBodyHtml = true;

            //  SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            //client.EnableSsl = true;
            //client.UseDefaultCredentials = false;
            //client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //client.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Baonguyen741999@!");
            //client.Send(message);

            for (int i = 0; i < tbGioHang.Rows.Count; i++)
            {
                var          from         = "*****@*****.**";
                var          to           = txtemail.Text;
                const string Password     = "******";
                string       mail_subject = "Clothing Store";
                string       mail_message = "From : Clothing Store" + "\n";
                mail_message += "From : [email protected] " + "\n";
                mail_message += "Thông tin mua hàng của quý khách" + "\n";
                string tenSP = tbGioHang.Rows[i]["TenSP"].ToString();
                Id_SP = SanPhamService.SanPham_GetByTop("", " TenHang=N'" + tenSP + "'", "")[0].Id;

                mail_message += "ID sản phẩm : " + Id_SP + "\n";
                mail_message += "Tên sản phẩm :" + tbGioHang.Rows[i]["TenSP"].ToString() + "\n";
                mail_message += "Size: " + tbGioHang.Rows[i]["Size"].ToString() + "\n";
                mail_message += "Số lượng: " + tbGioHang.Rows[i]["SoLuong"].ToString() + "\n";
                mail_message += "Tổng giá: " + tbGioHang.Rows[i]["TongGia"].ToString() + "\n";
                mail_message += "Giá: " + tbGioHang.Rows[i]["Gia"].ToString() + "\n";
                var smtp = new SmtpClient();
                {
                    smtp.Host           = "smtp.gmail.com";
                    smtp.Port           = 587;
                    smtp.EnableSsl      = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Credentials    = new NetworkCredential(from, Password);
                    smtp.Timeout        = 10000;
                }
                smtp.Send(from, to, mail_subject, mail_message);
            }



            Session["GioHang"]     = null;
            Session["slspgiohang"] = null;
            lblsl                 = (Label)Master.FindControl("lblslgiohang");
            lblsl.Text            = "0";
            lbltongtien.Text      = "0 VNĐ";
            tbGioHang             = null;
            grvsanpham.DataSource = tbGioHang;
            grvsanpham.DataBind();
            Response.AddHeader("REFRESH", "1;URL=TrangChu.aspx");
        }