Sie sind auf Seite 1von 10

using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.

Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; using System.Web.Configuration;

public partial class GioHang : System.Web.UI.Page { XuLyGioHang DB = new XuLyGioHang(); DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"];

if (dt.Rows.Count == 0) { Label1.Text = "Bn cha chn mua sn phm no"; BasicDatePicker1.Visible = false; } else { //dt.Columns.Add("MaSP", typeof(Int32)); //dt.Columns.Add("SoLuong", typeof(Int32)); //dt.Columns.Add("DonGia", typeof(float)); //dt.Columns.Add("ThanhTien", typeof(float)); dt = (DataTable)Session["giohang"]; GridView1.DataSource = dt; GridView1.DataBind(); total(); } } BasicDatePicker1.MinimumDate = DateTime.Today; }

public void total() { DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"]; double tongGia = 0;

foreach (DataRow dr in dt.Rows) { tongGia += double.Parse(dr[4].ToString()); } GridView1.FooterRow.Cells[4].Text = tongGia.ToString() + "VND"; Session["tonggia"] = tongGia.ToString(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { string str = GridView1.DataKeys[e.RowIndex].Value.ToString(); string tensanpham = ((Label)GridView1.Rows[e.RowIndex].FindControl("lbltensp")).Text; string masp = ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text; string str1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSoLuong")).Text; if (Convert.ToInt32(str1) < 0) { Response.Write("<script>alert('Bn nhp sai gi tr')</script>"); str1 = "1"; DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"]; if (dt.Rows.Count != 0) { foreach (DataRow dr in dt.Rows) {

if (dr[0].ToString().Equals(masp)) { dr[1] = tensanpham.ToString(); dr[3] = Int32.Parse(str1.ToString()); dr[4] = double.Parse(dr[2].ToString()) * Int32.Parse(dr[3].ToString()); } } } Session["giohang"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); total(); } if (Convert.ToInt32(str1) == 0) { Response.Write("<script>alert('Bn nhp sai gi tr.')</script>"); str1 = "1"; DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"]; if (dt.Rows.Count != 0) { foreach (DataRow dr in dt.Rows) { if (dr[0].ToString().Equals(masp)) {

dr[1] = tensanpham.ToString(); dr[3] = Int32.Parse(str1.ToString()); dr[4] = double.Parse(dr[2].ToString()) * Int32.Parse(dr[3].ToString()); } } } Session["giohang"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); total(); } else { DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"]; if (dt.Rows.Count != 0) { foreach (DataRow dr in dt.Rows) { if (dr[0].ToString().Equals(masp)) { dr[1] = tensanpham.ToString(); dr[3] = Int32.Parse(str1.ToString()); dr[4] = double.Parse(dr[2].ToString()) * Int32.Parse(dr[3].ToString()); }

} } Session["giohang"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); total(); } } catch (Exception ex) { Label1.Text = (ex.Message); } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { dt = (DataTable)Session["giohang"]; dt.Rows.RemoveAt(e.RowIndex); Session["giohang"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); total();

if (dt.Rows.Count == 0) {

Label1.Text = "Khng cn mt hng no trong gi";

} } protected void btn_mua_Click(object sender, EventArgs e) {

Response.Redirect("TrangChu.aspx"); } protected void btn_dathang_Click(object sender, EventArgs e) { if (BasicDatePicker1.Text == "") { Label1.Text = "Mi bn nhp ngy giao hng"; } else { if (Session["MaKH"] == null) Response.Redirect("DangNhap.aspx"); else { DataTable dt = new DataTable(); dt = (DataTable)Session["giohang"]; String ketnoi = WebConfigurationManager.ConnectionStrings["GiftShop"].ConnectionString; SqlConnection c = new SqlConnection(ketnoi);

c.Open(); if (dt.Rows.Count == 0) { WebMsgBox.Show("Bn cha chn sn phm no..."); } else { try { string sql = "insert into HoaDon(MaKH,NgayDat,NgayGiao,TongTien) values (@MaKH,@NgayDat,@NgayGiao,@TongTien)"; SqlCommand m = new SqlCommand(sql, c); m.Parameters.Add("@MaKH", Session["MaKH"].ToString()); m.Parameters.Add("@NgayDat", DateTime.Now.Date); m.Parameters.Add("@NgayGiao", BasicDatePicker1.SelectedDate); m.Parameters.Add("@TongTien", Session["tonggia"].ToString()); m.ExecuteNonQuery();

string identity_query = "Select @@Identity"; m.CommandText = identity_query; int id = Convert.ToInt32(m.ExecuteScalar());

try { string sql2 = "select MaHD from HoaDon where MaHD=" + id;

foreach (DataRow dw in dt.Rows) { string mahd = id.ToString(); string masp = dw[0].ToString(); string soluong = dw[3].ToString(); string chuoitruyvan = "insert into ChiTietHoaDon values(" + mahd + ",'" + masp + "'," + soluong + ")"; m.CommandText = chuoitruyvan; m.ExecuteNonQuery(); } } catch (Exception ex) { Label1.Text = (ex.Message); } } catch (Exception ex) { Label1.Text = (ex.Message); } WebMsgBox.Show("Bn t hng thnh cng..."); BasicDatePicker1.Visible = false; ((DataTable)Session["giohang"]).Clear(); GridView1.DataSource = (DataTable)Session["giohang"]; GridView1.DataBind();

} } } } }

Das könnte Ihnen auch gefallen