protected void BtnExportLedgerVoucher_Click(object sender, EventArgs e)
 {
     try
     {
         if (ViewState["LedgerVoucher"] != null)
         {
             DataTable dtLedgerVoucher = (DataTable)ViewState["LedgerVoucher"];
             if (dtLedgerVoucher.Rows.Count > 0)
             {
                 //========Call Register
                 GridView GridExp = new GridView();
                 dtLedgerVoucher.Columns.Remove("VoucherID");
                 GridExp.DataSource = dtLedgerVoucher;
                 GridExp.DataBind();
                 obj_Comman.Export("ListOfSundryCreditorsLedgerVoucher.xls", GridExp);
             }
             else
             {
                 obj_Comman.ShowPopUpMsg("No Data Found To Export..!", this.Page);
                 GrdLedgersummary.DataSource = null;
                 GrdLedgersummary.DataBind();
             }
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
    public void SetInitialRowLedgerSummary()
    {
        try
        {
            DataTable dt = new DataTable();
            DataRow   dr = null;
            dt.Columns.Add(new DataColumn("#", typeof(int)));
            dt.Columns.Add(new DataColumn("Particulars", typeof(string)));
            dt.Columns.Add(new DataColumn("Debit", typeof(string)));
            dt.Columns.Add(new DataColumn("Credit", typeof(string)));
            dt.Columns.Add(new DataColumn("Closing", typeof(string)));
            dt.Columns.Add(new DataColumn("Closing1", typeof(string)));
            dt.Columns.Add(new DataColumn("LedgerID", typeof(string)));
            dt.Columns.Add(new DataColumn("ForMonth", typeof(string)));

            dr = dt.NewRow();

            dr["#"]           = 0;
            dr["Particulars"] = "";
            dr["Debit"]       = "";
            dr["Credit"]      = "";
            dr["Closing"]     = "";
            dr["Closing1"]    = "";
            dr["LedgerID"]    = "";
            dr["ForMonth"]    = "";
            dt.Rows.Add(dr);
            ViewState["CurrentTable"]   = dt;
            GrdLedgersummary.DataSource = dt;
            GrdLedgersummary.DataBind();
            dt = null;
            dr = null;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    protected void ImgBtnExport_Click(object sender, ImageClickEventArgs e)
    {
        DataSet DsGrd = new DataSet();

        StrCondition = string.Empty;
        try
        {
            if (TR0.Visible)
            {
                if (ChkDate.Checked == true)
                {
                    ToDate       = Convert.ToDateTime(txtToDate.Text).ToString("MM-dd-yyyy");
                    FromDate     = Convert.ToDateTime(txtFromDate.Text).ToString("MM-dd-yyyy");
                    StrCondition = StrCondition + " AND VoucherDate<='" + Convert.ToDateTime(txtToDate.Text).ToString("MM-dd-yyyy") + "'";
                }
                else
                {
                    ToDate   = Convert.ToDateTime(txtToDate.Text).ToString("01-01-1975");
                    FromDate = Convert.ToDateTime(txtFromDate.Text).ToString("01-01-1975");
                    if (DateTime.Now > Convert.ToDateTime("03/31/" + DateTime.Now.ToString("yyyy")))
                    {
                        StrCondition = StrCondition + " AND VoucherDate<= '03/31/" + DateTime.Now.AddYears(1).ToString("yyyy") + "'";
                    }
                    else
                    {
                        StrCondition = StrCondition + " AND VoucherDate<= '04/01" + DateTime.Now.ToString() + "'";
                    }
                }

                //DsGrd = Obj_Call.Getdetails("25", StrCondition, out StrError);

                if (DsGrd.Tables[0].Rows.Count > 0)
                {
                    //========Call Register
                    GridView GridExp = new GridView();
                    DsGrd.Tables[0].Columns.Remove("Closing");
                    DsGrd.Tables[0].Columns.Remove("#");
                    DsGrd.Tables[0].Columns.Remove("LedgerID");
                    GridExp.DataSource = DsGrd.Tables[0];
                    GridExp.DataBind();
                    obj_Comman.Export("ListOfSundryCreditors.xls", GridExp);
                }
                else
                {
                    obj_Comman.ShowPopUpMsg("No Data Found To Export..!", this.Page);
                    DsGrd.Dispose();
                    GrdReport.DataSource = null;
                    GrdReport.DataBind();
                }
            }
            else if (TR1.Visible)
            {
                DataTable dtLedgerDetail = (DataTable)ViewState["LedgerSummary"];
                if (dtLedgerDetail.Rows.Count > 0)
                {
                    //========Call Register
                    GridView GridExp = new GridView();
                    dtLedgerDetail.Columns.Remove("Closing1");
                    dtLedgerDetail.Columns.Remove("#");
                    dtLedgerDetail.Columns.Remove("LedgerID");
                    GridExp.DataSource = dtLedgerDetail;
                    GridExp.DataBind();
                    obj_Comman.Export("ListOfSundryCreditorsLedgerDetail.xls", GridExp);
                }
                else
                {
                    obj_Comman.ShowPopUpMsg("No Data Found To Export..!", this.Page);
                    DsGrd.Dispose();
                    GrdLedgersummary.DataSource = null;
                    GrdLedgersummary.DataBind();
                }
            }
            else if (TR2.Visible)
            {
                DataTable dtLedgerVoucher = (DataTable)ViewState["LedgerVoucher"];
                if (dtLedgerVoucher.Rows.Count > 0)
                {
                    //========Call Register
                    GridView GridExp = new GridView();

                    GridExp.DataSource = dtLedgerVoucher;
                    GridExp.DataBind();
                    obj_Comman.Export("ListOfSundryCreditorsLedgerVoucher.xls", GridExp);
                }
                else
                {
                    obj_Comman.ShowPopUpMsg("No Data Found To Export..!", this.Page);
                    DsGrd.Dispose();
                    GrdLedgersummary.DataSource = null;
                    GrdLedgersummary.DataBind();
                }
            }
        }
        catch (ThreadAbortException ex)
        {
        }
        catch (Exception ex)
        {
        }
    }
    private void FindOpeningBalance(int LedgerId)
    {
        DataTable DTLedger = new DataTable();
        DataRow   dr       = null;

        DTLedger.Columns.Add(new DataColumn("#", typeof(int)));
        DTLedger.Columns.Add(new DataColumn("Particulars", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("Debit", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("Credit", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("Closing1", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("Closing", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("ForMonth", typeof(string)));
        DTLedger.Columns.Add(new DataColumn("LedgerID", typeof(string)));
        dr = DTLedger.NewRow();
        try
        {
            StrCondition = string.Empty;
            RepCond      = string.Empty;
            if (ChkDate.Checked == true)
            {
                ToDate       = Convert.ToDateTime(txtToDate.Text).ToString("dd-MM-yyyy");
                FromDate     = Convert.ToDateTime(txtFromDate.Text).ToString("dd-MM-yyyy");
                StrCondition = StrCondition + "AND (CAST(FLOOR(CAST(VoucherDate as FLOAT)) AS DateTime))<= '" + Convert.ToDateTime(txtFromDate.Text).ToString("MM-dd-yyyy") + "' ";
            }
            else
            {
                ToDate   = Convert.ToDateTime(Session["FinEndDate"].ToString()).ToString("dd-MM-yyyy");
                FromDate = Convert.ToDateTime(Session["FinStartDate"].ToString()).ToString("dd-MM-yyyy");
                if (DateTime.Now > Convert.ToDateTime("31/03/" + DateTime.Now.ToString("yyyy")))
                {
                    StrCondition = StrCondition + " AND  (CAST(FLOOR(CAST(VoucherDate as FLOAT)) AS DateTime))<= '04/01/" + DateTime.Now.ToString("yyyy") + "'";
                }
                else
                {
                    StrCondition = StrCondition + " AND (CAST(FLOOR(CAST(VoucherDate as FLOAT)) AS DateTime))<= '04/01" + DateTime.Now.AddYears(-1).ToString() + "'";
                }
            }
            RepCond = LedgerId.ToString();
            DS      = obj_Ledger.FindOpeningBal(RepCond, StrCondition, out StrError);
            //-----Set Opening Balance-----
            if (DS.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0)
            {
                GrdLedgersummary.Rows[0].Cells[2].Text = "Opening balance";
                dr["#"]           = 0;
                dr["Particulars"] = "Opening balance";
                dr["Debit"]       = DS.Tables[0].Rows[0]["Debit"].ToString();
                dr["Credit"]      = DS.Tables[0].Rows[0]["Credit"].ToString();
                dr["Closing1"]    = DS.Tables[0].Rows[0]["Closing1"].ToString();
                dr["Closing"]     = DS.Tables[0].Rows[0]["Closing"].ToString();
                dr["ForMonth"]    = DS.Tables[0].Rows[0]["ForMonth"].ToString();
                dr["LedgerID"]    = DS.Tables[0].Rows[0]["LedgerID"].ToString();
                DTLedger.Rows.Add(dr);
            }
            //-----For No. Of Months-----
            //NoOfMonths = 12 * ((Convert.ToDateTime(txtToDate.Text)).Year - (Convert.ToDateTime(txtFromDate.Text)).Year) + (Convert.ToDateTime(txtToDate.Text)).Month - (Convert.ToDateTime(txtFromDate.Text)).Month;
            NoOfMonths = 12 * ((Convert.ToDateTime(ToDate)).Year - (Convert.ToDateTime(FromDate)).Year) + (Convert.ToDateTime(ToDate)).Month - (Convert.ToDateTime(FromDate)).Month;
            for (int i = 0; i <= NoOfMonths; i++)
            {
                string Condition = string.Empty;
                //StDate = Convert.ToDateTime(txtFromDate.Text).AddMonths(i).ToString("dd-MM-yyyy");
                //EdDate = (Convert.ToDateTime(txtFromDate.Text).AddMonths(i + 1)).AddDays(-1).ToString("dd-MM-yyyy");

                StDate    = Convert.ToDateTime(FromDate).AddMonths(i).ToString("dd-MM-yyyy");
                EdDate    = (Convert.ToDateTime(FromDate).AddMonths(i + 1)).AddDays(-1).ToString("dd-MM-yyyy");
                Condition = Condition + "AND (CAST(FLOOR(CAST(VoucherDate as FLOAT)) AS DateTime)) >= '" + Convert.ToDateTime(StDate).ToString("MM-dd-yyyy") + "' And (CAST(FLOOR(CAST(VoucherDate as FLOAT)) AS DateTime)) <= '" + Convert.ToDateTime(EdDate).ToString("MM-dd-yyyy") + "' ";
                DS        = obj_Ledger.DisplayMonthDetails(Convert.ToString(LedgerId), Condition, out StrError);
                dr        = DTLedger.NewRow();
                if (DS.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0)
                {
                    dr["#"]           = LedgerId;
                    dr["Particulars"] = Convert.ToDateTime(StDate).ToString("MMM");
                    dr["Debit"]       = DS.Tables[0].Rows[0]["Debit"].ToString();
                    dr["Credit"]      = DS.Tables[0].Rows[0]["Credit"].ToString();
                    dr["Closing1"]    = DS.Tables[0].Rows[0]["Closing1"].ToString();
                    dr["Closing"]     = DS.Tables[0].Rows[0]["Closing"].ToString();
                    dr["ForMonth"]    = Convert.ToDateTime(StDate).ToString("dd-MM-yyyy");
                    dr["LedgerID"]    = DS.Tables[0].Rows[0]["LedgerID"].ToString();
                }
                else
                {
                    dr["#"]           = LedgerId;
                    dr["Particulars"] = Convert.ToDateTime(StDate).ToString("MMM");
                    dr["Debit"]       = "0.00";
                    dr["Credit"]      = "0.00";
                    dr["Closing"]     = "0.00";
                    dr["Closing1"]    = "0.00";
                    dr["ForMonth"]    = Convert.ToDateTime(StDate).ToString("dd-MM-yyyy");
                    dr["LedgerID"]    = "0";
                }

                DTLedger.Rows.Add(dr);

                //----Calculation of Debit,Credit n Closing for each Month in 2nd grid--
                int    PreviousRow = DTLedger.Rows.Count - 2;
                int    CurrentRow  = DTLedger.Rows.Count - 1;
                string temp;
                string Debit   = DTLedger.Rows[DTLedger.Rows.Count - 1]["Debit"].ToString();
                string Credit  = DTLedger.Rows[DTLedger.Rows.Count - 1]["Credit"].ToString();
                string closing = DTLedger.Rows[DTLedger.Rows.Count - 2]["Closing1"].ToString();
                temp = closing.Substring(closing.Length - 2, 2);
                string closingAmnt = closing.Substring(0, closing.Length - 3);
                //string debitAmnt = Debit.Substring(0, Debit.Length - 2);
                //string creditAmnt = Credit.Substring(0, Credit.Length - 2);
                if (temp.Equals("Cr"))
                {
                    DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"] = ((Convert.ToDecimal(Debit) - Convert.ToDecimal(Credit)) - Convert.ToDecimal(closingAmnt)).ToString("0.00");
                    if (Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) > 0)
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) + " Dr";
                    }
                    else if (Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) < 0)
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = Math.Abs(Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"])) + " Cr";
                    }
                    else
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = "0.00" + " Cr";
                    }
                }
                else
                {
                    DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"] = ((Convert.ToDecimal(closingAmnt) + (Convert.ToDecimal(Debit) - Convert.ToDecimal(Credit)))).ToString("0.00");
                    if (Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) > 0)
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) + " Dr";
                    }
                    else if (Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"]) < 0)
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = Math.Abs(Convert.ToDecimal(DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing1"])) + " Cr";
                    }
                    else
                    {
                        DTLedger.Rows[DTLedger.Rows.Count - 1]["Closing"] = "0.00" + " Cr";
                    }
                }
            }
            GrdLedgersummary.DataSource = DTLedger;
            ViewState["LedgerSummary"]  = DTLedger;
            GrdLedgersummary.DataBind();
            GrdLedgersummary.Columns[6].Visible = false;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }