购物车代码

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

public partial class checkOut : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["UserName"] == null)
            {
                Response.Write("<script>alert('您还没有登陆,请登录!');location='login.aspx'</script> ");
            }
            else
            {
                if (Session["bus"] == null)
                {
                    Response.Write("<script>alert('您的购物车中还没有物品!无需结账!请继续购物!');location='Default.aspx'</script> ");
                }
                else
                {
                    if (!IsPostBack)
                    {
                        updata();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    private void updata()
    {
        try
        {
            Label1.Text = Session["UserName"].ToString();
            DataTable dt = new DataTable();
            dt.Columns.Add("PName");
            dt.Columns.Add("ProductId");
            dt.Columns.Add("PMemberPrice");
            dt.Columns.Add("PNumber");
            dt.Columns.Add("sum");
            double dSum = 0;

            if (Session["bus"] != null)
            {

                System.Collections.Hashtable ht = (Hashtable)Session["bus"];

                IDictionaryEnumerator idic = ht.GetEnumerator();
                DataRow dr;

                while (idic.MoveNext())
                {
                    dr = dt.NewRow();
                    SqlConnection cn = db.creatconn();
                    cn.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select PName,ProductId,PMemberPrice from Product where ProductId=" + idic.Key.ToString(), cn);
                    DataSet ds = new DataSet();
                    sda.Fill(ds, "Product");
                    dr["PName"] = ds.Tables[0].Rows[0].ItemArray[0].ToString();
                    dr["ProductId"] = ds.Tables[0].Rows[0].ItemArray[1].ToString();
                    dr["PMemberPrice"] = ds.Tables[0].Rows[0].ItemArray[2].ToString();
                    dr["PNumber"] = idic.Value.ToString();
                    dr["sum"] = double.Parse(dr["PMemberPrice"].ToString()) * int.Parse(dr["PNumber"].ToString());
                    dSum += double.Parse(dr["PMemberPrice"].ToString()) * int.Parse(dr["PNumber"].ToString());
                    dt.Rows.Add(dr);
                }

            }

            GridView1.DataSource = dt.DefaultView;
            GridView1.DataKeyNames = new string[] { "ProductId"};
            GridView1.DataBind();
            Label1.Text = Session["UserName"].ToString();

            Label2.Text = "合计:" + dSum.ToString() + " 元";
            showusers();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    private void showusers()
    {
        try
        {
            SqlConnection cn1 = db.creatconn();
            cn1.Open();
            SqlDataAdapter sda1 = new SqlDataAdapter("select Name,Email,Phone,Post,Address from Users where UserName='" + Session["UserName"].ToString() + "'", cn1);
            DataSet ds1 = new DataSet();
            sda1.Fill(ds1, "Users");
            DataRow dr1 = ds1.Tables[0].Rows[0];
            TextBox1.Text = dr1.ItemArray[0].ToString();
            TextBox2.Text = dr1.ItemArray[1].ToString();
            TextBox3.Text = dr1.ItemArray[2].ToString();
            TextBox4.Text = dr1.ItemArray[3].ToString();
            TextBox5.Text = dr1.ItemArray[4].ToString();
            cn1.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection cn = db.creatconn();
            cn.Open();
            SqlCommand cmd1 = new SqlCommand("select UserId from Users where UserName='" + Session["UserName"].ToString() + "'", cn);
            SqlCommand cmd = new SqlCommand("insert into Orders(UserId,OrderDate,LinkMan,Email,Phone,PostalCode,Address,ReMark,SongHuoQixian,SongHuoFangShi,FuKuanFangShi,YiWaiChuLi) values(@UserId,@OrderDate,@LinkMan,@Email,@Phone,@PostalCode,@Address,@ReMark,@SongHuoQixian,@SongHuoFangShi,@FuKuanFangShi,@YiWaiChuLi)", cn);
            SqlParameter para = new SqlParameter("@UserId", SqlDbType.Int);
            para.Value = cmd1.ExecuteScalar();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@OrderDate", SqlDbType.DateTime);
            para.Value = DateTime.Now.ToLocalTime();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@LinkMan", SqlDbType.NVarChar, 50);
            para.Value = TextBox1.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@Email", SqlDbType.NVarChar, 80);
            para.Value = TextBox2.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@Phone", SqlDbType.NVarChar, 50);
            para.Value = TextBox3.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@PostalCode", SqlDbType.NVarChar, 50);
            para.Value = TextBox4.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@Address", SqlDbType.NVarChar, 255);
            para.Value = TextBox5.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@ReMark", SqlDbType.NVarChar);
            para.Value = TextBox6.Text;
            cmd.Parameters.Add(para);
            para = new SqlParameter("@SongHuoQixian", SqlDbType.NVarChar, 50);
            para.Value = DropDownList1.SelectedItem.ToString();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@SongHuoFangShi", SqlDbType.NVarChar, 50);
            para.Value = DropDownList2.SelectedItem.ToString();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@FuKuanFangShi", SqlDbType.NVarChar, 50);
            para.Value = DropDownList3.SelectedItem.ToString();
            cmd.Parameters.Add(para);
            para = new SqlParameter("@YiWaiChuLi", SqlDbType.NVarChar, 255);
            para.Value = RadioButtonList1.SelectedItem.ToString();
            cmd.Parameters.Add(para);
            cmd.ExecuteNonQuery();

//这段代码是诸位高手要修改或重写的代码(
            SqlCommand cmdx = new SqlCommand("select top 1 OrderId from Orders order by OrderId desc",cn);
            string top1Id = cmdx.ExecuteScalar().ToString();
            string[] asqls=new string [GridView1.Rows.Count];
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                SqlCommand cmdy = new SqlCommand("insert into OrdersDetail (OrderId,ProductId,Quantity,UnitCost,ProductName) values ('" + top1Id + "','" +GridView1.Rows[i].RowIndex.ToString() + "','" + ((Label)GridView1.Rows[i].FindControl("PNumber")).Text + "','" + ((Label)GridView1.Rows[i].FindControl("PMemberPrice")).Text + "','" + ((Label)GridView1.Rows[i].FindControl("PName")).Text + "'",cn);
                cmdy.ExecuteNonQuery();
            }
               )
            Response.Write("<script>alert('订单提交成功!')</script>");
            Panel1.Visible = false;
            SqlCommand kcmd = new SqlCommand("select top 1 OrderId from Orders order by OrderId desc",cn);
            Label4.Visible = true;
            Label4.Text = Convert.ToString("您的订单编号是:" + kcmd.ExecuteScalar());
            cn.Close();
           
            baseclass email = new baseclass();
            email.sendemail(TextBox2.Text, "您在asp.net电子商务购物网站的订单提交成功!"+Label4.Text, TextBox1.Text + "您好!这是您在asp.net电子商务购物网站的购物清单!");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
       
      

    }
}
//http://hi.baidu.com/tieniu116/blog

posted @ 2010-08-09 11:47  midisong  阅读(247)  评论(0编辑  收藏  举报