Hi all,
I created a shopping cart page , used a gridview and added a textbox (to set quantity) into the grid in tamplate mode.
when i hit the "update" button i read the value of the textbox for each rows and update the table in the database.
it works perfectly.
I also added two more fucntion to remove a line and add new lines to the grid.
I used querystring to sent item numbers to the shopping cart
after I added those functions, update button never worked. if i remove the funcs button works
can anyone tell me how i can add or remove items to a shopping cart
thanks
code is :
1using System;2using System.Data;3//using System.Data.SqlClient;4using System.Data.OleDb;5using System.Configuration;6using System.Collections;7using System.Web;8using System.Web.Security;9using System.Web.UI;10using System.Web.UI.WebControls;11using System.Web.UI.WebControls.WebParts;12using System.Web.UI.HtmlControls;13using eSatis;1415public partialclass Controls_ShoppingCart : System.Web.UI.UserControl16{17public eSatis.DB dbtool;18private string getfromForm(string key) {19try20 {21return Request.Form[key].ToString();22 }23catch {return""; }24 }2526private string getfromQuery(string key) {27try28 {29return Request.QueryString[key].ToString();30 }31catch {return""; }32 }3334protected void Page_Load(object sender, EventArgs e)35 {36 dbtool =new DB();37/* if (!IsPostBack)38 {39 if (core.s2i(getfromQuery("remove")) > 0)40 {41 delete(core.s2i(getfromQuery("remove")));42 }43 else if (core.s2i(getfromQuery("add")) > 0)44 {45 int amount = core.s2i(getfromQuery("qty"));46 if (!(amount > 0)) amount = 1;47 add2basket(core.s2i(getfromQuery("add")), amount);48 }49 }*/50 GridView1.DataSource = dbtool.sql2reader("select * from v_Basket");51 GridView1.DataBind();52 OleDbDataReader ttl = dbtool.sql2reader("select sum(LineTotal) from v_Basket");53 ttl.Read();54 lbl_total.Text = ttl.GetValue(0).ToString();55 ttl.Dispose();56 ttl =null;5758 }5960private void add2basket(int pid,int qty) {61// if there is a62 dbtool.execSQL(String.Format("insert into Basket (Product,Quantity) values ({0},{1})",pid,qty));63 }6465private void delete(int bpid) {66 dbtool.execSQL(string.Format("delete from basket where bpid={0}", bpid));67 }6869private void update() {70int pid, qty, bpid;71string sql_txt;72foreach (GridViewRow rowin GridView1.Rows){73 qty = core.s2i((((TextBox)row.FindControl("txt_qty")).Text));74 pid = core.s2i((((Label)row.FindControl("lbl_pid")).Text));75 bpid = core.s2i((((Label)row.FindControl("lbl_bpid")).Text));76if (pid > 0) {77if (qty < 1) sql_txt ="delete from basket where product=" + pid.ToString();78else sql_txt ="update Basket set Quantity=" + qty.ToString() +" where BPID=" + bpid.ToString();7980 dbtool.execSQL(sql_txt);81 }82 }83 GridView1.DataSource = dbtool.sql2reader("select * from v_Basket");84 GridView1.DataBind();85 }86protected void ImageButton1_Click(object sender, ImageClickEventArgs e)87 {88 update();89 }what do you mean by error? What actually is hapenning when u click on the button after you added those 2 methods?
Put a breakpoint in the page load and image button1 click event. Now step through it, let us know what you see.
Not sure if this will solve the problem but you need to rebind the grid after you add a new item (add2basket) ore remove an item (delete)
thanks guys
I just noticed that I put the binding code out of the ispostback check block![]()
I have just put it into right place and it's working
i had concentrated on wrong side until your advise
thanks for helping
0 comments:
Post a Comment