protected void btnAddCart_Click(object sender, EventArgs e)
        {
            if (Session["UserId"] == null)
                Response.Redirect("Login.aspx");
            else
            {
                CartDetails objCart = new CartDetails();
                objCart.CreateUserCart(0, Convert.ToInt32(Session["UserId"].ToString()), Convert.ToInt32(strAdId));

                Response.Redirect("MyCartDetail.aspx?uid=" + Session["UserId"].ToString());
            }
        }
        protected void btnBuy_Click(object sender, EventArgs e)
        {
            if (Session["UserId"] == null)
                Response.Redirect("Login.aspx");
            else
            {
                if (ddlDeliType.SelectedIndex != 0)
                {
                #region .. GET HISTORY ID ..
                int intHistoryId = 0;
                    objCart = new CartDetails();
                DataTable dtMax = objCart.SelectMaxID();
                if (dtMax.Rows.Count > 0)
                {
                    string st = dtMax.Rows[0]["HistroyId"].ToString();
                    if (dtMax.Rows[0]["HistroyId"].ToString() == "")
                        intHistoryId = 0;
                    else
                        intHistoryId = Convert.ToInt32(dtMax.Rows[0]["HistroyId"].ToString());
                }
                intHistoryId++;
                #endregion
                    #region .. BUY DETAILS ..

                DataTable dtCart = objCart.SelectUserCartDetails(Convert.ToInt32(Session["UserId"].ToString()));
                    double delPrice = 0;
                    int intChargeId = int.Parse(ddlDeliType.SelectedItem.Value.ToString().Trim());

                    ChargeDetails chg = new ChargeDetails();
                    DataTable dt = chg.SelectChargeDetailsByID(intChargeId);
                    string strType = "";
                    string strAmount = "";
                    if (dt.Rows.Count > 0)
                    {
                        strType = dt.Rows[0]["ChargeType"].ToString();
                        strAmount = dt.Rows[0]["AmountOrPercent"].ToString();
                    }

                    double dblDeliAmount = Convert.ToDouble(strAmount);
                    if (strType == "0")
                        delPrice = Convert.ToDouble(decTotalPrice) + dblDeliAmount;
                    else if (strType == "1")
                        delPrice = Convert.ToDouble(decTotalPrice) + (Convert.ToDouble(decTotalPrice) * (dblDeliAmount / 100));

                    string strLocationId = "";
                    string strLocationName = "";
                    if (Session["Location"] != null)
                        strLocationName = Session["Location"].ToString();

                    City objCity = new City();
                    DataTable dtCity = objCity.SelectCityByName(strLocationName);
                    if (dtCity.Rows.Count > 0) strLocationId = dtCity.Rows[0]["CityId"].ToString(); else strLocationId = "0";

                for (int i = 0; i < dtCart.Rows.Count; i++)
                {

                    objCart.CreateBuyDetails(
                        Convert.ToInt32(dtCart.Rows[i]["AdPostId"].ToString()),
                        intHistoryId,
                        Convert.ToInt32(Session["UserId"].ToString()),
                            Convert.ToDecimal(delPrice),
                            int.Parse(ddlDeliType.SelectedItem.Value.ToString().Trim()),
                            Convert.ToInt32(strLocationId),
                        "BOUGHT",
                            1,
                            ddlDeliType.SelectedItem.Text.ToString().Trim(),
                            Convert.ToInt32(strType),
                            Convert.ToDouble(strAmount)
                        );

                    objCart.UpdateCartStatus(Convert.ToInt32(dtCart.Rows[i]["AdPostId"].ToString()), Convert.ToInt32(Session["UserId"].ToString()));
                }
                    decTotalPrice = 0;

                #endregion
                Response.Redirect("MyPurchases.aspx");
            }
                else
                {
                    lblError.Visible = true;
                    lblError.Text = "Select Delivery charges";
                }

            }
        }
        void BindAlbumGrid()
        {
            int k = GridView1.PageCount;
            GridView1.Controls.Clear();

            //gridpageIndex = 0;
            CartDetails cartbll = new CartDetails();
            dt=cartbll.SelectUserCartDetails(Convert.ToInt32(Session["UserId"].ToString()));
            lblCart.Text = dt.Rows.Count.ToString() + " Items in Your Cart";
            if (dt.Rows.Count > 0)
            {
                decTotalPrice = 0;
                tblAdpost.Visible = true;
                tblAmount.Visible = true;
                tblBuy.Visible = true;
            if (dt.Rows.Count > 4)
            {
                decimal dPageSize = (dt.Rows.Count / 4M);
                //GridView1.PagerSettings.PageButtonCount = Convert.ToInt32(Math.Floor(dPageSize));
                //GridView1.PageSize = Convert.ToInt32(Math.Ceiling(dPageSize));
            }
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
            }
            else
            {
                tblAdpost.Visible = false;
                tblAmount.Visible = false;
                tblBuy.Visible = false;
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CartDetails objCart = new CartDetails();
            string sCartId = GridView1.DataKeys[e.RowIndex].Value.ToString();

            objCart.CartDetailDelete(Convert.ToInt32(sCartId));

            BindAlbumGrid();

            if (ddlDeliType.SelectedIndex != 0)
                ddlDeliType_SelectedIndexChanged(sender, e);
        }
        protected void ddlDeliType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlDeliType.SelectedIndex != 0)
            {
                objCart = new CartDetails();
                DataTable dtCart = objCart.SelectUserCartDetails(Convert.ToInt32(Session["UserId"].ToString()));
                double delPrice = 0;
                int intChargeId = int.Parse(ddlDeliType.SelectedItem.Value.ToString().Trim());

                ChargeDetails chg = new ChargeDetails();
                DataTable dt = chg.SelectChargeDetailsByID(intChargeId);
                string strType = "";
                string strAmount = "";
                if (dt.Rows.Count > 0)
                {
                    strType = dt.Rows[0]["ChargeType"].ToString();
                    strAmount = dt.Rows[0]["AmountOrPercent"].ToString();
                }

                double dblDeliAmount = Convert.ToDouble(strAmount);
                if (strType == "0")
                    delPrice = Convert.ToDouble(decTotalPrice) + dblDeliAmount;
                else if (strType == "1")
                {
                    dblDeliAmount = (Convert.ToDouble(decTotalPrice) * (dblDeliAmount / 100));
                    delPrice = Convert.ToDouble(decTotalPrice) + dblDeliAmount;
                }
                //decTotalPrice = 0;

                trDeliAmt.Visible = true;
                trTotalAmt.Visible = true;
                lblError.Visible = false;
                lblDeliAmt.Text = "$ " +String.Format("{0:0.00}", dblDeliAmount);
                lblTotalAmt.Text = "$ " + String.Format("{0:0.00}", delPrice);
            }
            else
            {
                trDeliAmt.Visible = false;
                lblError.Visible = true;
                trTotalAmt.Visible = false;
            }
        }