protected void gvPayment_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortingDirection = string.Empty;

        if (direction == SortDirection.Ascending)
        {
            direction        = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            direction        = SortDirection.Ascending;
            sortingDirection = "Asc";
        }
        con.Open();
        SqlDataAdapter adp = new SqlDataAdapter("select ReqestId,AffiliateId,  (select SUBSTRING(name, 1, CHARINDEX(' ', name) - 1) from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'AffiliateName', (select SUBSTRING(name, CHARINDEX(' ', name) + 1, 8000)  from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'SurName'  ,(select mail  from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'affimail'   ,case when RequestedAmt<500 then 'paypal' else 'Wire Transfer' end   as 'paytype'   ,RequestedAmt   ,Status=case PayingStatus when 'True' then 'Paid' else 'Unpaid' end    , DaysRemain=case when PayingStatus=1 then 0 else  (select (30- (select DATEDIFF( day, tbl1.RequestDate,getdate()  )))) end , RequestedAmt  , bankInfo =case when RequestedAmt>500 then   'Bank Name : '+ (select BankAcName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId) +'   Account Number : '   +(select BankAcName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)   else    'Paypal Id : '+ (select paypal_mail_id from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)+'AcNo : '+   (select PaypalAccountName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)  end    from  tblPaymentRequest  as tbl1", con);
        DataSet        ds  = new DataSet();

        adp.Fill(ds);
        con.Close();
        DataView sortedView = new DataView(ds.Tables[0]);

        sortedView.Sort        = e.SortExpression + " " + sortingDirection;
        Session["SortedView"]  = sortedView;
        GridPayment.DataSource = sortedView;
        GridPayment.DataBind();
    }
    protected void load_data()
    {
        try
        {
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select ReqestId,AffiliateId,  (select SUBSTRING(name, 1, CHARINDEX(' ', name) - 1) from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'AffiliateName', (select SUBSTRING(name, CHARINDEX(' ', name) + 1, 8000)  from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'SurName'  ,(select mail  from Affiliate  where  Affiliate_user_name=tbl1.AffiliateId )  as 'affimail'   ,case when RequestedAmt<500 then 'paypal' else 'Wire Transfer' end   as 'paytype'   ,RequestedAmt   ,Status=case PayingStatus when 'True' then 'Paid' else 'Unpaid' end    , DaysRemain=case when PayingStatus=1 then 0 else  (select (30- (select DATEDIFF( day, tbl1.RequestDate,getdate()  )))) end , RequestedAmt  , bankInfo =case when RequestedAmt>500 then   'Bank Name : '+ (select BankAcName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId) +'   Account Number : '   +(select BankAcName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)   else    'Paypal Id : '+ (select paypal_mail_id from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)+'AcNo : '+   (select PaypalAccountName from dbo.Affiliate where Affiliate_user_name=tbl1.AffiliateId)  end    from  tblPaymentRequest  as tbl1", con);
            DataSet        ds  = new DataSet();
            adp.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                ViewState["affiliate_data"] = ds.Tables[0];//for searching purpose
                GridPayment.DataSource      = ds.Tables[0];
                GridPayment.DataBind();
                decimal d = 0;
                for (int count = 0; count < GridPayment.Rows.Count; count++)
                {
                    CheckBox chk      = (CheckBox)GridPayment.Rows[count].Cells[8].FindControl("cbcheck");
                    Label    lbreqAmt = (Label)GridPayment.Rows[count].Cells[7].FindControl("lbreqAmt");
                    Label    lbstatus = (Label)GridPayment.Rows[count].Cells[5].FindControl("lbstatus");

                    if (chk.Checked == false)
                    {
                        d = d + Convert.ToDecimal(lbreqAmt.Text);
                    }

                    if (lbstatus.Text == "Unpaid")
                    {
                        chk.Enabled = true;
                    }
                    else
                    {
                        chk.Enabled = false;
                    }
                }
                TotalUnpaid.Text = d.ToString();
            }
        }
        catch
        {
        }
    }