Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. datatable...

datatable...

Scheduled Pinned Locked Moved ASP.NET
tutorialcsharphelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mylogics
    wrote on last edited by
    #1

    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...

    N 1 Reply Last reply
    0
    • M mylogics

      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...

      N Offline
      N Offline
      Nishant Singh
      wrote on last edited by
      #2

      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 ,

      M 1 Reply Last reply
      0
      • N Nishant Singh

        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 ,

        M Offline
        M Offline
        mylogics
        wrote on last edited by
        #3

        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"\];
        
        N G 3 Replies Last reply
        0
        • M mylogics

          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"\];
          
          N Offline
          N Offline
          Nishant Singh
          wrote on last edited by
          #4

          Where are you adding a new row to the DataTable???

          1 Reply Last reply
          0
          • M mylogics

            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"\];
            
            N Offline
            N Offline
            Nishant Singh
            wrote on last edited by
            #5

            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 ???

            1 Reply Last reply
            0
            • M mylogics

              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"\];
              
              G Offline
              G Offline
              Greg Chelstowski
              wrote on last edited by
              #6

              "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));

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups