datatable...
-
hii all m working on shopping cart using csharp. m retaining the products in datatable and finally then m showing this product in datalist(cart). but the problem is that the datatable only keeps single row. whenever i use to add new product to cart the old one is not shown only recently added product is shown. plz guide where m i wrong.how to retain multiple rows in datatable.i used session but no change. thnks...
-
hii all m working on shopping cart using csharp. m retaining the products in datatable and finally then m showing this product in datalist(cart). but the problem is that the datatable only keeps single row. whenever i use to add new product to cart the old one is not shown only recently added product is shown. plz guide where m i wrong.how to retain multiple rows in datatable.i used session but no change. thnks...
Hi ...... I believe that somewhere in your code you are overwriting the DataTable without preserving the older records . Most possible place is where you are adding a new row, just debug your code or post relevent pieces here ,
-
Hi ...... I believe that somewhere in your code you are overwriting the DataTable without preserving the older records . Most possible place is where you are adding a new row, just debug your code or post relevent pieces here ,
this is the page wr my cart is shown name cart.aspx....
public partial class mycart : System.Web.UI.Page
{
DataTable dt =new DataTable();DataView cartview; protected void Page\_Load(object sender, EventArgs e) { Getsource(); if(!IsPostBack) { Bindlist(); } //DataList1.DataSource = dt; //DataList1.DataBind(); //Session\["cart"\] = dt; //cartview.Sort = "ProductName"; } void Bindlist() { DataList1.DataSource = cartview; DataList1.DataBind(); Session\["cart"\] = dt; } void Getsource() { dt = (DataTable)Session\["cart"\]; cartview = new DataView(dt); return; //cartview.Sort = "ProductName"; //Session\["cart"\] = dt; } protected void Update(object source, DataListCommandEventArgs e) { int ProductID = Convert.ToInt32(DataList1.DataKeys\[e.Item.ItemIndex\]); TextBox txtqty; txtqty = ((TextBox)e.Item.FindControl("txtqty")); string connectionstring = ConfigurationManager.ConnectionStrings\["SilverOnline"\].ConnectionString; SqlConnection conn = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("Update Home Set ProductQty=@productqty Where ProductID=@productid", conn); cmd.Parameters.Add("@productqty", SqlDbType.VarChar).Value = txtqty.Text; cmd.Parameters.Add("@productid", SqlDbType.Int).Value = ProductID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.ExecuteNonQuery(); if (conn.State == ConnectionState.Open) conn.Close(); DataList1.EditItemIndex = -1; Bindlist(); } protected void Delete(object source, DataListCommandEventArgs e) { string name = ((Label)e.Item.FindControl("Label1")).Text; cartview.RowFilter = "ProductName='" + name + "'"; if (cartview.Count > 0) { cartview.Delete(0); } cartview.RowFilter = ""; DataList1.EditItemIndex = -1; Bindlist(); }
}
on my addcart.aspx page m adding that particular product to cart....the code is...
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.Visible = false;
Button1.Visible = false;
GridView1.Visible = true;string str = Request.QueryString\["ProductID"\];
-
this is the page wr my cart is shown name cart.aspx....
public partial class mycart : System.Web.UI.Page
{
DataTable dt =new DataTable();DataView cartview; protected void Page\_Load(object sender, EventArgs e) { Getsource(); if(!IsPostBack) { Bindlist(); } //DataList1.DataSource = dt; //DataList1.DataBind(); //Session\["cart"\] = dt; //cartview.Sort = "ProductName"; } void Bindlist() { DataList1.DataSource = cartview; DataList1.DataBind(); Session\["cart"\] = dt; } void Getsource() { dt = (DataTable)Session\["cart"\]; cartview = new DataView(dt); return; //cartview.Sort = "ProductName"; //Session\["cart"\] = dt; } protected void Update(object source, DataListCommandEventArgs e) { int ProductID = Convert.ToInt32(DataList1.DataKeys\[e.Item.ItemIndex\]); TextBox txtqty; txtqty = ((TextBox)e.Item.FindControl("txtqty")); string connectionstring = ConfigurationManager.ConnectionStrings\["SilverOnline"\].ConnectionString; SqlConnection conn = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("Update Home Set ProductQty=@productqty Where ProductID=@productid", conn); cmd.Parameters.Add("@productqty", SqlDbType.VarChar).Value = txtqty.Text; cmd.Parameters.Add("@productid", SqlDbType.Int).Value = ProductID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.ExecuteNonQuery(); if (conn.State == ConnectionState.Open) conn.Close(); DataList1.EditItemIndex = -1; Bindlist(); } protected void Delete(object source, DataListCommandEventArgs e) { string name = ((Label)e.Item.FindControl("Label1")).Text; cartview.RowFilter = "ProductName='" + name + "'"; if (cartview.Count > 0) { cartview.Delete(0); } cartview.RowFilter = ""; DataList1.EditItemIndex = -1; Bindlist(); }
}
on my addcart.aspx page m adding that particular product to cart....the code is...
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.Visible = false;
Button1.Visible = false;
GridView1.Visible = true;string str = Request.QueryString\["ProductID"\];
Where are you adding a new row to the DataTable???
-
this is the page wr my cart is shown name cart.aspx....
public partial class mycart : System.Web.UI.Page
{
DataTable dt =new DataTable();DataView cartview; protected void Page\_Load(object sender, EventArgs e) { Getsource(); if(!IsPostBack) { Bindlist(); } //DataList1.DataSource = dt; //DataList1.DataBind(); //Session\["cart"\] = dt; //cartview.Sort = "ProductName"; } void Bindlist() { DataList1.DataSource = cartview; DataList1.DataBind(); Session\["cart"\] = dt; } void Getsource() { dt = (DataTable)Session\["cart"\]; cartview = new DataView(dt); return; //cartview.Sort = "ProductName"; //Session\["cart"\] = dt; } protected void Update(object source, DataListCommandEventArgs e) { int ProductID = Convert.ToInt32(DataList1.DataKeys\[e.Item.ItemIndex\]); TextBox txtqty; txtqty = ((TextBox)e.Item.FindControl("txtqty")); string connectionstring = ConfigurationManager.ConnectionStrings\["SilverOnline"\].ConnectionString; SqlConnection conn = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("Update Home Set ProductQty=@productqty Where ProductID=@productid", conn); cmd.Parameters.Add("@productqty", SqlDbType.VarChar).Value = txtqty.Text; cmd.Parameters.Add("@productid", SqlDbType.Int).Value = ProductID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.ExecuteNonQuery(); if (conn.State == ConnectionState.Open) conn.Close(); DataList1.EditItemIndex = -1; Bindlist(); } protected void Delete(object source, DataListCommandEventArgs e) { string name = ((Label)e.Item.FindControl("Label1")).Text; cartview.RowFilter = "ProductName='" + name + "'"; if (cartview.Count > 0) { cartview.Delete(0); } cartview.RowFilter = ""; DataList1.EditItemIndex = -1; Bindlist(); }
}
on my addcart.aspx page m adding that particular product to cart....the code is...
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.Visible = false;
Button1.Visible = false;
GridView1.Visible = true;string str = Request.QueryString\["ProductID"\];
Sir , On Button1_Click you are creating a new DataTable .....no where you are retrieving DaTatable from Session, So How is it suppose to Take the older rows ???
-
this is the page wr my cart is shown name cart.aspx....
public partial class mycart : System.Web.UI.Page
{
DataTable dt =new DataTable();DataView cartview; protected void Page\_Load(object sender, EventArgs e) { Getsource(); if(!IsPostBack) { Bindlist(); } //DataList1.DataSource = dt; //DataList1.DataBind(); //Session\["cart"\] = dt; //cartview.Sort = "ProductName"; } void Bindlist() { DataList1.DataSource = cartview; DataList1.DataBind(); Session\["cart"\] = dt; } void Getsource() { dt = (DataTable)Session\["cart"\]; cartview = new DataView(dt); return; //cartview.Sort = "ProductName"; //Session\["cart"\] = dt; } protected void Update(object source, DataListCommandEventArgs e) { int ProductID = Convert.ToInt32(DataList1.DataKeys\[e.Item.ItemIndex\]); TextBox txtqty; txtqty = ((TextBox)e.Item.FindControl("txtqty")); string connectionstring = ConfigurationManager.ConnectionStrings\["SilverOnline"\].ConnectionString; SqlConnection conn = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("Update Home Set ProductQty=@productqty Where ProductID=@productid", conn); cmd.Parameters.Add("@productqty", SqlDbType.VarChar).Value = txtqty.Text; cmd.Parameters.Add("@productid", SqlDbType.Int).Value = ProductID; if (conn.State == ConnectionState.Closed) conn.Open(); cmd.ExecuteNonQuery(); if (conn.State == ConnectionState.Open) conn.Close(); DataList1.EditItemIndex = -1; Bindlist(); } protected void Delete(object source, DataListCommandEventArgs e) { string name = ((Label)e.Item.FindControl("Label1")).Text; cartview.RowFilter = "ProductName='" + name + "'"; if (cartview.Count > 0) { cartview.Delete(0); } cartview.RowFilter = ""; DataList1.EditItemIndex = -1; Bindlist(); }
}
on my addcart.aspx page m adding that particular product to cart....the code is...
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.Visible = false;
Button1.Visible = false;
GridView1.Visible = true;string str = Request.QueryString\["ProductID"\];
"Technically", the bug is here (your assigning a whole new datatable as your Session["cart"] object).
mylogics wrote:
dt.Rows.Add(row); Session["cart"] = dt;
If your Session["cart"] schema is the same as dt just do:
((DataTable)Session["cart"]).Rows.Add(row);
But the whole thing is veerrry messsssyy.... Come on man, ever heard of Sql injection? I see you use parameters in your Update method, good - use them all round your code, better yet, turn those sql strings into stored procedures.
var question = (_2b || !(_2b));