protected void Button1_Click1(object sender, ImageClickEventArgs e)
 {
     try
     {
         Response.ClearContent();
         Response.Buffer = true;
         Response.AddHeader("content-disposition", string.Format("attatchment;filename={0}", "payment_details.xls"));
         Response.Charset     = "";
         Response.ContentType = "application/ms-excel";
         System.IO.StringWriter sw  = new System.IO.StringWriter();
         HtmlTextWriter         htw = new HtmlTextWriter(sw);
         GridPayment.AllowPaging        = false;
         GridPayment.Columns[7].Visible = false;
         for (int a = 0; a < GridPayment.Rows.Count; a++)
         {
             GridViewRow row = GridPayment.Rows[a];
         }
         GridPayment.RenderControl(htw);
         Response.Output.Write(sw.ToString());
         Response.End();
     }
     catch
     {
     }
 }
    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
        {
        }
    }