Click here to Skip to main content
15,896,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Please Help me with this . I have been working in the shopping cart project and displayed product details in datalist like
Product Name
Price Quantity
product Description
and the next Page is Add cart page if i click the add to cart button the values in productdetails should be binded in the gridview in the next page i tried some coding but it doesnt work it doesnt binded any values .please help me to solve this problem.
coding as follows
View productDetails.aspx.cs page
  protected void use(object sender, DataListItemEventArgs e)
    {
       Label prd = (Label)e.Item.FindControl("productNameLabel");
     ViewState["productName"] = prd.Text;
      Label des = (Label)e.Item.FindControl("Label1");
      ViewState["description"] = des.Text;
      Image img = (Image)e.Item.FindControl("Image1");
      ViewState["image"] = img.ImageUrl.ToString();
      Label prc = (Label)e.Item.FindControl("priceLabel");
      ViewState["price"] = prc.Text;
}

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        int i = int.Parse(TextBox1.Text);


        int p = int.Parse((string)ViewState["price"]);
        string img = ViewState["image"].ToString();
        string s2 = System.Web.HttpContext.Current.User.Identity.Name;
        string s1 = ViewState["productName"].ToString();
        con.Open();
        string cartCmd = "insert into completeCart(pName,quantity,uName,img,price) values('" + s1 + "','" + i + "','" + s2 + "','" + img + "','" + p + "') ";
        SqlCommand cmd = new SqlCommand(cartCmd, con);
        cmd.ExecuteNonQuery();
        con.Close();

        Response.Redirect("Add_cart.aspx?user=" + s2);

    }

Add to cart aspx.cs
public partial class Add_cart : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Integrated Security=true;Initial Catalog=rsaProducts");
    string url;
    decimal grandTotal = 0;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridData();
        string s2 = System.Web.HttpContext.Current.User.Identity.Name;
        Response.Cookies["uname"].Value = s2;
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        BindGridData();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price")) * Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "quantity"));
                grandTotal = grandTotal + rowTotal;


            }
            Response.Cookies["price"].Value = grandTotal.ToString();
            TextBox2.Text = grandTotal.ToString();
            HyperLink1.ImageUrl = "~/img/str/buttons/checkout.jpg";
    }
    private void BindGridData()
    {
        try
        {
            con.Open();
            string productid = Request.QueryString["id"].ToString();
            {
                string sql = "select * from completeCart where id=" + productid;
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        catch (Exception ex)
        {
        }
        finally
        {
            con.Close();
        }
    }
}

add to cart.aspx page

<asp:GridView ID="GridView1" CssClass="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id"
EmptyDataText="No Item in the Cart"
onrowdatabound="GridView1_RowDataBound"
onrowdeleted="GridView1_RowDeleted"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2">
<rowstyle forecolor="#8C4510" backcolor="#FFF7E7">
<columns> <asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName" HeaderText="PRODUCT" SortExpression="pName" />
<asp:TemplateField HeaderText="img" SortExpression="img">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("img") %>'>

<itemtemplate>
<asp:Image ID="Image1" runat="server" Width="250px" Height="300px" ImageUrl='<%# Bind("img") %>' />


<asp:BoundField DataField="quantity" HeaderText="QUANTITY"
SortExpression="quantity" />
<asp:BoundField DataField="price" HeaderText="COST" SortExpression="price" />
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510">
<pagerstyle forecolor="#8C4510" horizontalalign="Center">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<sortedascendingcellstyle backcolor="#FFF1D4">
<sortedascendingheaderstyle backcolor="#B95C30">
<sorteddescendingcellstyle backcolor="#F1E5CE">
<sorteddescendingheaderstyle backcolor="#93451F">



<!-- PayPal Logo -->


<img src="https://www.paypal.com/en_US/i/bnr/horizontal_solution_PPeCheck.gif" border="0"
alt="Solution Graphics">


<!-- PayPal Logo -->


Total Cost<asp:TextBox ID="TextBox2" runat="server"
BorderColor="#CC3300" Enabled="False"
Font-Bold="True" ForeColor="Red">
    

                  <%--<asp:ImageButton ID="ImageButton3" runat="server" AlternateText="check out"
onclick="ImageButton3_Click1" ToolTip="click for check out" ImageUrl="~/img/str/buttons/checkout.jpg" />--%>



<asp:HyperLink ID="HyperLink1" runat="server" >HyperLink


please help me to solve this problem
Posted
Comments
Richard Deeming 16-Jan-15 13:05pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
kwelpooh 16-Jan-15 13:10pm    
i will change that but whether coding is correct?

1 solution

please refer below article, it's demonstrating every thing for building a Shopping cart application.

http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/shopping-cart[^]

And please use some coding standard's and some design patters too, while building your application. just refer the above article, and take it as reference for building your application.

And also if possible, always use store procedures instead of inline queries.
 
Share this answer
 
v2

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