注册 登录
编程论坛 ASP.NET技术论坛

菜鸟求助,关于asp数据添加的问题,做一个网上商城的新增问题 ...

chensike0603 发布于 2008-05-11 15:30, 692 次点击
求源码,
只有本站会员才能查看附件,请 登录
就是下面的小部分
只有本站会员才能查看附件,请 登录
,其他的搜索,分页,删除,修改都做了,就差新增了,请高手帮忙
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;

namespace lab17
{
    public partial class goodsInsert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getGoods();
            }

        }
        private SqlConnection GreateConnection()
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["OnlineShopConnectionString"].ConnectionString;
            return cn;
        }



        private DataSet GetGoodsByName(string strGoodsName)
        {
            SqlConnection cn = GreateConnection();
            cn.Open();

            SqlCommand cm = new SqlCommand();
             = CommandType.StoredProcedure;
             = "GoodsGetByName";
            cm.Parameters.AddWithValue("@goodsName", strGoodsName);
            cm.Connection = cn;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cm;

            DataSet ds = new DataSet();
            da.Fill(ds, "tb_Goods");

            cn.Close();
            return ds;

        }
        private void getGoods()
        {
            if (ViewState["goodsname"] == null)
                ViewState["goodsname"] = "";
            string GoodsName = ViewState["goodsname"].ToString();
            DataSet ds = GetGoodsByName(GoodsName);
            gdvGoods.DataSource = ds.Tables["tb_Goods"];
            gdvGoods.DataBind();

        }

        protected void gdvGoods_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvGoods.PageIndex = e.NewPageIndex;
            getGoods();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["goodsname"] = txtSearch.Text;
            getGoods();
        }

        protected void gdvGoods_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection cn = GreateConnection();
            cn.Open();

            SqlCommand cm = new SqlCommand();
             = CommandType.StoredProcedure;
             = "GoodsDelete";
            cm.Parameters.AddWithValue("@goodsID", gdvGoods.DataKeys[e.RowIndex].Value);
            cm.Connection = cn;
            cm.ExecuteNonQuery();
            cn.Close();
            getGoods();

        }

        protected void gdvGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == 0)
            {
                LinkButton lbtn = (LinkButton)e.Row.Cells[6].Controls[0];
                lbtn.Attributes.Add("onclick", "javascript:return confirm('您确定要删除吗?')");
            }
        }

        protected void gdvGoods_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvGoods.EditIndex = e.NewEditIndex;
            getGoods();
        }

        protected void gdvGoods_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvGoods.EditIndex = -1;
            getGoods();
        }

        protected void gdvGoods_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection cn = GreateConnection();
            cn.Open();
            SqlCommand cm = new SqlCommand();

             = CommandType.StoredProcedure;
             = "GoodsUpdata";

            cm.Parameters.AddWithValue("@goodsID", gdvGoods.DataKeys[e.RowIndex].Value);
            cm.Parameters.AddWithValue("@goodsName", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[0].FindControl("TextBox1")).Text.Trim());
            cm.Parameters.AddWithValue("@goodsType", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim());
            cm.Parameters.AddWithValue("@goodsUnitPrice", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim());
            cm.Parameters.AddWithValue("@goodsImageName", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim());
            cm.Parameters.AddWithValue("@sellCount", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim());
            cm.Parameters.AddWithValue("@goodsDate", ((TextBox)gdvGoods.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim());

            cm.Connection = cn;
            cm.ExecuteNonQuery();
            gdvGoods.EditIndex = -1;
            getGoods();
        }
5 回复
#2
hebingbing2008-05-11 15:37
lz既然能做出来修改那么做新增就简单多了啊……
在你的新增的按钮的click事件中直接将修改的代码复制过来,再加上一些判断是否为空,是否已经存在等的操作,然后将修改的sql语句改成insert into就ok了啊……
#3
chensike06032008-05-11 20:05
其实这都是老师教的,老师叫我们自己做'新增'的,不会啊,还有图片上传的
#4
hebingbing2008-05-11 20:26
lz还是自己做吧,其实不难的,具体的方法我都在上面说的很清楚了啊……
努力,相信你能做出来的……
#5
chensike06032008-05-12 12:21
努力
#6
jielig12008-05-12 15:58
看看书就可以了,努力,就是在新增的事件中,插入新数据和上传图片就行了。
1