shopping cart using Session Arraylist and dataset in asp.net :
BuyNow Button Click :
aspx:CS :
public class CartRow
{
public string ID;
public string Name;
public string Count;
};
public class CartList
{
public System.Collections.ArrayList list = new ArrayList(20);
};
CartList objCartList = new CartList();
- - Button Click
protected void btn_Click(object sender, EventArgs e)
{
if ((Session["Cart"] == null))
{
Session["Cart"] = objCartList;
}
else
{
objCartList = (CartList)(Session["cart"]);
}
string S_ID = lblID.Text;
CartRow objCartrow = new CartRow();
CartRow objTestCartRow = new CartRow();
objCartrow.ID = S_ID;
objCartrow.Name = lblName.Text;
objCartrow.Count = "1";
int listCount = objCartList.list.Count;
for (int i = 0; i < listCount; i++)
{
objTestCartRow = (CartRow)objCartList.list[i];
if (objTestCartRow.ID == S_ID)
{
int nItems =Int32.Parse(objTestCartRow.Count.ToString());
nItems += 1;
objTestCartRow.Count = nItems.ToString();
objCartList.list.RemoveAt(i);
objCartrow = objTestCartRow; ;
break;
}
}
objCartList.list.Add(objCartrow);
- -Array List Convert To DataSet
DataSet ds1 = new DataSet();
ds1 = bind();
-- Session Value DataSet
Session["CartDS"] = ds1;
-- Item Count
string[] arrID = new string[20];
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
arrID[i] = ds1.Tables[0].Rows[i]["items"].ToString();
bb += Convert.ToInt16(arrID[i]);
}
string cc = bb.ToString();
string Cou = ds1.Tables[0].Rows.Count.ToString();
Session["ItemCount"] = cc.ToString();
}
--Array List Convert to Dataset
public DataSet bind()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
ds.Tables[0].Columns.Add("id",
System.Type.GetType("System.Object"));
ds.Tables[0].Columns.Add("items",
System.Type.GetType("System.Object"));
foreach (object obj in objCartList.list)
{
for (int i = 0; i < objCartList.list.Count; i++)
{
CartRow ca = new CartRow();
DataRow rows = ds.Tables[0].NewRow();
rows[0] = ((CartRow)objCartList.list[i]).ID;
rows[1] = ((CartRow)objCartList.list[i]).Count;
ds.Tables[0].Rows.Add(rows);
}
break;
}
return ds;
}
--Use to another Page :
protected void Page_Load(object sender, EventArgs e)
{
--- Session
ds = (DataSet)Session["CartDS"];
}
No comments:
Post a Comment