This is code to add items in the cart it add all the items but problem is it doesn't add only one item i couldn't find out the exception but still catch block executes.Please Help me to solve this.My code as follows:
try
{
if (e.CommandName == "Cart" && Session.Count > 0)
{
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["users"].ToString()) + " and tatc.UserId=" + Convert.ToInt32(Session["users"].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= @CommandID and tl.UserId= @UID ", con);
da.SelectCommand.Parameters.Add("@UID", Convert.ToInt32(Session["users"]));
da.SelectCommand.Parameters.Add("@CommandID", Convert.ToInt32(e.CommandArgument.ToString()));
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=@UId,productid=@productid,productname=@prodname,ProductImage=@prodImage,price=@price,cdate=getdate() where productid=@productid and cartId=@cartId and UserId=@UId ", con);
da.SelectCommand.Parameters.AddWithValue("@UId", Convert.ToInt32(Session["users"].ToString()));
da.SelectCommand.Parameters.AddWithValue("@productid", Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()));
da.SelectCommand.Parameters.AddWithValue("@prodname", ds.Tables[0].Rows[0][1].ToString());
da.SelectCommand.Parameters.AddWithValue("@prodImage", ds.Tables[0].Rows[0][3]);
da.SelectCommand.Parameters.AddWithValue("@price", Convert.ToDecimal(ds.Tables[0].Rows[0][2].ToString()));
da.SelectCommand.Parameters.AddWithValue("@cartId", Convert.ToInt32(ds.Tables[0].Rows[0][5].ToString()));
da.SelectCommand.ExecuteNonQuery();
}
}
da = new SqlDataAdapter("select tpd.ProductID,tpd.ProductName,tpd.Price,tpd.ProductImage from rsa_ProductItemTable tpd where tpd.ProductID=@commandId", con);
da.SelectCommand.Parameters.AddWithValue("@commandId", Convert.ToInt32(e.CommandArgument.ToString()));
ds = new DataSet();
da.Fill(ds, "tbl_tpd");
if (ds.Tables.Count > 0 && ds.Tables["tbl_tpd"].Rows.Count > 0)
{
using (var command = new SqlCommand("rsa_products_sp_Insertaddcart", con))
{
int value = 1;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@UserId", Session["users"]);
command.Parameters.AddWithValue("@productid", ds.Tables[0].Rows[0][0]);
command.Parameters.AddWithValue("@productname", ds.Tables[0].Rows[0][1]);
command.Parameters.AddWithValue("@ProductImage", ds.Tables[0].Rows[0][3]);
command.Parameters.AddWithValue("@price", ds.Tables[0].Rows[0][2]);
command.Parameters.AddWithValue("@qty", value);
command.Parameters.AddWithValue("@totalcost", ds.Tables[0].Rows[0][2]);
if (con.State != ConnectionState.Open)
{
con.Open();
try
{
command.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
else
{
command.ExecuteNonQuery();
}
}
if (Session.Count > 0)
{
Response.Redirect("AddCart.aspx?ProductID=" + e.CommandArgument.ToString());
}
else
{
Response.Redirect("UserLogin.aspx");
}
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert('"+ Server.HtmlEncode(ex.Message) + "')</script>");
}
finally
{
con.Close();
}
Thnks in advance.