Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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:

XML
<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.DataSource = Basket_DataTable;
            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
        {

        }
       // Response.Redirect("TestShopcart.aspx");
    }
    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);
        }


    }
}
Posted
Comments
Sinisa Hajnal 6-Oct-14 6:23am    
Can you clarify this:
"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..."

What do you mean by can't give total price? Do you get an error? It doesn't show? What?
AnoopGharu 6-Oct-14 6:37am    
You are right Sir, but here's it doesn't showing the proper result and still showing the same price which I had enter the price in database.Here's below this code:

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
{

}
Response.Redirect("TestShopcart.aspx");
}
Sinisa Hajnal 6-Oct-14 6:58am    
Are you sure your price should be an integer and not decimal / double?

Try converting Tb.Text to integer before assigning to the database field (which is presumably also an integer) - just to avoid implicit conversions (which might result in silent conversion to zero (0) instead of failing)

Finally, you're not showing us TestShopcart code - there is no way to know how you handle the data.
AnoopGharu 6-Oct-14 8:25am    
Thank you sir for your best concern,actually these both above coding pages are TestShopcart.aspx and TestShopcart.aspx.cs pages,I forgot to mention it.Even it seems me my this "Response.Redirect("TestShopcart.aspx");" coding line is not appropriate to convert the data and even it don't come in my mind that what I need to mention there to go on the TestShopcart.aspx.cs page because in the last page where I has to getting the data by using QueryString.So it directly showing the nothing on the page and it just showing the same result.About the datatype of price and total keywords were varchar and int respectively in my database, which I had created but now I change both of them into the Int datatype.See this is the coding of TestShopcart.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") %>'>

<itemtemplate>
<asp:Image ID="Image1" runat="server" Height="63px"
ImageUrl='<%# Eval("ImageUrl") %>' Width="75px" />


<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />

<asp:TemplateField HeaderText="Item">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server">

<itemtemplate>
<asp:TextBox ID="TextBoxCount" runat="server" Height="19px"
Text='<%#Bind ("Item") %>' Width="35px">


<asp:BoundField DataField="total" HeaderText="totalprice" />
<asp:HyperLinkField DataNavigateUrlFields="Id"
DataNavigateUrlFormatString="?DelID={0}" HeaderText="delete"
Text="delete" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetDetail"
TypeName="BusinessLogic.ProductsBL">
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="ProductId" Type="Int32" />
</SelectParameters>

<asp:Button ID="Button1" runat="server" Text="Change" onclick="Button1_Click" />
<asp:Button ID="save" runat="server" Text="Save To Database" onclick="save_Click" />
ZurdoDev 6-Oct-14 8:35am    
Please debug the code and find where the issue is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900