⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 product_add.aspx.cs

📁 基于VS05和SQL05做的一个电子商务网站
💻 CS
字号:
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;
using System.IO;
public partial class product_add : System.Web.UI.Page
{
    public string productname;
    public string describe;//描述
    public int quantity; //数量
    public string validity;//有效期
    public string province; //省
    public string city;//城市
    public float post;//平邮
    public float fax;//快递
    public int payment;//付款方式
    public int invoice; //发票
    public int repair;//保修
    public string address;//所在地
    public float price;//价钱

    public string name;
    public string aa;

    public string date;
    public string hour;
    public string minute;
    public string second;
    public string time;
    public int n;

    public int j;
    public string img;

    DateTime beginTime;


    public string strID;
    public string strUserName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("User_login.aspx");
        }
        name = Request.QueryString["categoryname"].ToString();
        Label1.Text = Server.UrlDecode(name);

        strID = Request.QueryString["categoryid"].ToString();

        beginTime = DateTime.Now.Date;

        n = DateTime.Now.Year;
        j = DateTime.Now.Month;

        for (int i = 1; i <= 30; i++)
        {
            DropDownList1.Items.Add(new ListItem(n.ToString() + "-" + j.ToString() + "-" + i.ToString(), i.ToString()));
        }
        for (int i = 1; i <= 24; i++)
        {
            DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        for (int i = 1; i <= 60; i++)
        {
            DropDownList3.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        for (int i = 1; i <= 60; i++)
        {
            Dropdownlist4.Items.Add(new ListItem(i.ToString(), i.ToString()));

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

        if (Session["username"] == null)
        {
            Response.Redirect("User_login.aspx");
        }
        else
        {
            strUserName = Session["username"].ToString();
        }

        // 检查是否开了店铺 ----
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
        string sql2 = "select * from DianPuDetail where DianPuUser='" + strUserName + "'";
        SqlCommand command2 = new SqlCommand(sql2, conn);
        conn.Open();
        SqlDataReader dr2 = command2.ExecuteReader();

        if (!dr2.Read())
        {
            Response.Write("<script language='JavaScript'>");
            Response.Write("alert('你还没开店!');");
            Response.Write("</script>");
            //Response.Redirect("shop_manage.aspx");
            dr2.Close();
            conn.Close();
            return;
        }
        dr2.Close();
        conn.Close();

        //----

        string imgPath = uploadFile.PostedFile.FileName;
        System.Drawing.Image imgupload = null;
        try
        {
            imgupload = System.Drawing.Image.FromFile(imgPath);
        }
        catch
        {
            //Response.Write("请注意:因为系统桌面上以及我的文档里面的文件具有特殊权限,所以在选择图片时请选择其他地方的图片。");
        }

        if (TextBox1.Text.Equals("") || TextBox2.Text.Equals("") || TextBox3.Text.Equals("") || TextBox4.Text.Equals("") || TextBox5.Text.Equals("") || Select2.Value.Equals("--请选择所在省份--"))
        {
            Response.Write("<Script>alert('内容不能为空!')</Script>");
        }
        else if (uploadFile.PostedFile.FileName != "" && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
        {
            Response.Write("<Script>alert('上传的图片格式必须为gif或jpg!!')</Script>");
        }/*
			else if(uploadFile.PostedFile.FileName!="" && imgupload.Height>200 && imgupload.Width>200)
			{
				Response.Write("<script>alert('上传的图片的高不的超过200 宽不的超过200!!')</script>");
			}*/
        else
        {

            productname = TextBox1.Text;
            describe = TEXTAREA1.Value;
            quantity = Int32.Parse(TextBox2.Text);
            validity = Select1.Value;
            province = Select2.Value;
            city = Select3.Value;

            date = DropDownList1.SelectedValue;
            hour = DropDownList2.SelectedValue;
            minute = DropDownList3.SelectedValue;
            second = Dropdownlist4.SelectedValue;
            time = n + "-" + j + "-" + date + " " + hour + ":" + minute + ":" + second;

            price = float.Parse(TextBox5.Text);
            post = float.Parse(TextBox3.Text);
            fax = float.Parse(TextBox4.Text);
            address = province + "省" + city;

            if (RadioButton1.Checked)
            {
                payment = 1;
            }
            else if (RadioButton2.Checked)
            {
                payment = 0;
            }
            else
            {
                payment = 2;
            }
            if (Radiobutton5.Checked)
            {
                repair = 1;
            }
            else
            {
                repair = 0;
            }
            if (Radiobutton7.Checked)
            {
                invoice = 1;
            }
            else
            {
                invoice = 0;
            }

            SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
            string sql = "insert into Products(ProductName,ProductPrice,ProductType,ProductQuantity,ProductAddress,ProductPost,ProductFax," +
                "ProductBegin,ProductDetail,ProductState,ProductStart,ProductLeft,ProductInvoice,ProductRepair,ProductPayment,UserName)" +
                "values('" + productname + "'," + price + "," + strID + "," + quantity + ",'" + address + "'," + post + "," + fax + ",'" + beginTime + "','" + describe + "','1'," +
                "'" + time + "','" + validity + "','" + invoice + "','" + repair + "','" + payment + "','" + strUserName + "')";
            SqlCommand command = new SqlCommand(sql, connection);

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            if (uploadFile.PostedFile.FileName != "")
            {
                //----------- update图片
                Stream imagedatastream;
                SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
                imagedatastream = Request.Files["uploadFile"].InputStream;
                int imagedatalen = Request.Files["uploadFile"].ContentLength;
                string imagedatatype = Request.Files["uploadFile"].ContentType;

                byte[] image = new byte[imagedatalen];
                imagedatastream.Read(image, 0, imagedatalen);

                //String sql="insert into image(image) values(@imgdata)";
                String Psql = "update [Products] set [ProductPicture]=@imgdata where ProductId=" +
                    "(select top 1 ProductId from [Products] where UserName='" + strUserName + "' order by ProductId desc)";

                SqlCommand Pcommand = new SqlCommand(Psql, myConn);

                SqlParameter imgdata = new SqlParameter("@imgdata", SqlDbType.Image);
                imgdata.Value = image;
                Pcommand.Parameters.Add(imgdata);

                myConn.Open();
                Pcommand.ExecuteReader();
                myConn.Close();
                //-----------
            }

            Response.Write("<Script>alert('添加成功,确定请返回!')</Script>");

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -