Hello Everybody,I am working on the shopping website and I got a problem in my project is that when I am trying to multiply both Cart_Item and price, then it can't give the total_Price and don't be update in the Cart Page and I using here "ObjectDataSource" to bind the GridView in the project and when I am use "QueryString" Parameter Source, then my selecting product go to the CartPage otherwise not,but If I used there "Session", it does not works well and even though the product does not save into the session.So please help me.Here's below is the code of both ShopCart.aspx and ShopCart.aspx.cs respectively:
This code is about the ShopCart.aspx:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" EmptyDataText="There are no data records to display."
DataSourceID="ObjectDataSource1" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="name" />
<asp:TemplateField HeaderText="pic">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ImageUrl") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="63px"
ImageUrl='<%# Eval("ImageUrl") %>' Width="75px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:TemplateField HeaderText="Item">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBoxCount" runat="server" Height="19px"
Text='<%#Bind ("Item") %>' Width="35px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="total" HeaderText="totalprice" />
<asp:HyperLinkField DataNavigateUrlFields="Id"
DataNavigateUrlFormatString="?DelID={0}" HeaderText="delete"
Text="delete" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetDetail"
TypeName="BusinessLogic.ProductsBL">
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="ProductId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" Text="Change" onclick="Button1_Click" />
<asp:Button ID="save" runat="server" Text="Save To Database" onclick="save_Click" />
This code about ShopCart.aspx.cs:
public partial class Default2 : S
ystem.Web.UI.Page
{
DataTable Basket_DataTable = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["basket"] != null)
{
Basket_DataTable = (DataTable)Session["basket"];
}
else
{
Basket_DataTable = new DataTable();
Basket_DataTable.Columns.Add("Id");
Basket_DataTable.Columns.Add("Name");
Basket_DataTable.Columns.Add("ImageUrl");
Basket_DataTable.Columns.Add("Price");
Basket_DataTable.Columns.Add("Item");
Basket_DataTable.Columns.Add("total");
}
if (Request["DelId"] != null)
{
for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
{
if (Basket_DataTable.Rows[i][0].ToString() == Request["DelId"].ToString())
Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);
}
}
if (Request["Id"] != null)
{
bool Found = false;
for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
{
if (Basket_DataTable.Rows[i][0].ToString() == Request["Id"].ToString())
Found = true;
}
if (Found == false)
{
DataAccess.TestDA data = new DataAccess.TestDA();
string sql = "Select * from Products where Id=" + Request["Id"];
DataTable ret = data.exe_select(sql);
if (ret != null && ret.Rows.Count == 1)
{
Basket_DataTable.Rows.Add(new object[]{Request["Id"],ret.Rows[0]["Name"].ToString(),
ret.Rows[0]["Price"].ToString(),ret.Rows[0]["ImageUrl"].ToString()
,"1",ret.Rows[0]["Price"].ToString()});
}
}
}
if (IsPostBack == false)
{
GridView2.DataBind();
}
Session["basket"] = Basket_DataTable;
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < GridView2.Rows.Count; i++)
{
TextBox Tb = (TextBox)GridView2.Rows[i].FindControl("TextBoxCount");
Basket_DataTable.Rows[i]["Item"] = Tb.Text;
Basket_DataTable.Rows[i]["total"] = Convert.ToInt32(Tb.Text) * Convert.ToInt32(Basket_DataTable.Rows[i]["Price"]);
}
}
catch
{
}
}
protected void save_Click(object sender, EventArgs e)
{
DataAccess.TestDA data = new DataAccess.TestDA();
for (int i = 0; i < GridView2.Rows.Count; i++)
{
String Id = GridView2.Rows[i].Cells[0].Text.ToString();
TextBox Tb = (TextBox)GridView2.Rows[i].FindControl("TextBoxCount");
string sql = "Insert into Mycart(ProductsId) values({0})";
sql = string.Format(sql, Tb.Text, Id);
data.exe_cmd(sql);
}
}
}