Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have been working in the shopping cart project and i have been stuck in add to cart module if condition is not working and values are not inserting into the add cart table it displays empty grid @code as follows

ViewProductDetails.aspx.cs
C#
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Cart")
        {
            Response.Redirect("Addtocart.aspx?ProductID=" + e.CommandArgument.ToString());
            da = new SqlDataAdapter("select count(*) from rsa_addtocart tatc inner join rsa_Users tl on tatc.UserId=tl.UserId where tatc.productid=" + Convert.ToInt32(e.CommandArgument.ToString()) + " and tl.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " and tatc.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
            int n = Convert.ToInt32(da.SelectCommand.ExecuteScalar());
            if (n == 1)
            {
                da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage,tl.UserId,tatc.cartId from rsa_ProductItemTable tpd inner join rsa_addtocart tatc on tpd.ProductID=tatc.productid inner join rsa_Users tl on tl.UserId=tatc.UserId where tatc.productid=" + Convert.ToInt32(e.CommandArgument.ToString()) + " and tl.UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
                ds = new DataSet();
                da.Fill(ds, "tbl_tpd");
                if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
                {
                    da = new SqlDataAdapter("update rsa_addtocart set UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + ",productid=" + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + ",productname='" + ds.Tables[0].Rows[0][1].ToString() + "',ProductImage='" + ds.Tables[0].Rows[0][3].ToString() + "',price=" + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",cdate=getdate() where productid=" + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + " and cartId=" + Convert.ToInt32(ds.Tables[0].Rows[0][5].ToString()) + " and UserId=" + Convert.ToInt32(Session["UserId"].ToString()) + " ", con);
                    da.SelectCommand.ExecuteNonQuery();
                }
            }
            else
            {
                da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage from rsa_ProductItemTable  tpd where tpd.ProductID=" + Convert.ToInt32(e.CommandArgument.ToString()) + " ", con);
                ds = new DataSet();
                da.Fill(ds, "tbl_tpd");
            }
            if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
            {
                da = new SqlDataAdapter("insert into rsa_addtocart(UserId,productid,productname,ProductImage,price,totalcost,cdate)values(" + Convert.ToInt32(Session["UserId"].ToString()) + "," + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + ",'" + ds.Tables[0].Rows[0][1].ToString() + "','" + ds.Tables[0].Rows[0][3].ToString() + "'," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",1," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",getdate())", con);
                da.SelectCommand.ExecuteNonQuery();
            }

        }
Addtocart.aspx

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="productid"
        Style="position: static" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True" PageSize="5">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
        
        <asp:TemplateField HeaderText="CartId">
        <ItemTemplate>
        <asp:Label ID="cartid" runat="server" Text='<%#Eval("cartId") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Image">
        <ItemTemplate>
        <asp:Image ID="imgpath" runat="server" Width="100" Height="100" ImageUrl='<%#Eval("ProductImage") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="ProductName">
        <ItemTemplate>
        <asp:Label ID="bname" runat="server" Text='<%#Eval("productname") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Price">
        <ItemTemplate>
        <asp:Label ID="cost" runat="server" Text='<%#Eval("price") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
        <asp:TextBox ID="qty" runat="server" Width="20" Text='<%#Eval("qty") %>' AutoPostBack="True" OnTextChanged="qty_TextChanged"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Total">
        <ItemTemplate>
        <asp:Label ID="tcost" runat="server" Width="100" Text='<%#Eval("totalcost") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Delete">
        <ItemTemplate>
        <asp:LinkButton ID="delete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("cartId") %>'></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
        
        </Columns>
        <EmptyDataTemplate>
            <strong>There are no items in your cart.</strong>
        </EmptyDataTemplate>
    </asp:GridView>
Addtocart.aspx.cs
<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
  {
      if (Page.IsPostBack != true)
      {

          GetUserSelectedproducts(Convert.ToInt32(Session["UserId"].ToString()));
          GetTotal();
      }
  }
<pre>private void GetUserSelectedproducts(int uid)
    {
        da = new SqlDataAdapter("select tatc.cartId,tatc.productname,tatc.ProductImage,tatc.price,tatc.totalcost from rsa_addtocart tatc inner join rsa_Users tl on tl.UserId=tatc.UserId where tatc.UserId=" + uid + " ", con);
        ds = new DataSet();
        da.Fill(ds, "tbl_tatc");
        GridView1.DataSource = ds.Tables["tbl_tatc"].DefaultView;
        GridView1.DataBind();
    }
</pre>

please me help me with this thanks in advance.
Posted
Updated 22-Feb-15 22:47pm
v3
Comments
[no name] 23-Feb-15 7:29am    
Where you have mentioned the command name as "Cart" for your rows in the gridview???
kwelpooh 23-Feb-15 7:33am    
It's datalist item command not griedivew I have mentioned it in datalist wen I click the link button in datalist it will bind the values in gridview

use
if(e.Item.ID == "ItemId")
{
//put here your Code
}

and in data list set Comand Id =ItemId
 
Share this answer
 
v2
Please debug this line and see what this is returning. There might be some row, which is showing on Grid or just a blank row. Debug and see the count of Rows.
C#
GridView1.DataSource = ds.Tables["tbl_tatc"].DefaultView;
 
Share this answer
 
Comments
kwelpooh 23-Feb-15 10:47am    
have to first add items in the cart while debugging its not coming inside the if loop
da = new SqlDataAdapter("insert into rsa_addtocart(UserId,productid,productname,ProductImage,price,totalcost,cdate)values(" + Convert.ToInt32(Session["UserId"].ToString()) + "," + Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()) + ",'" + ds.Tables[0].Rows[0][1].ToString() + "','" + ds.Tables[0].Rows[0][3].ToString() + "'," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",1," + Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()) + ",getdate())", con);
da.SelectCommand.ExecuteNonQuery();
That means the condition inside the if clause is not satisfied. Also, you are redirecting to a new page at the first line.

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