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
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.