using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using RKLib.ExportData;

public partial class AccountSummery : System.Web.UI.Page
{
    decimal BalanceAmount;
    string date = DateTime.Now.ToLongDateString();
    SqlDataReader dr = null;
    DataSet ds = new DataSet();
    DataSet dsMergedContract = new DataSet();
    int loginvariable;
    static string courentlevel;
    string loginid;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
            {
                Response.Redirect("../login.aspx");
            }
            if (!IsPostBack)
            {
                btndownload.Visible = false;
                //getopeningbalance();
                //BindDataAccSummery();
            }
            lbluserid.Visible = false;
            ddl_partner_id.Visible = false;
            //lblopeningbalance.Visible = false;
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    private void getopeningbalance()
    {
        string date = DateTime.Now.ToShortDateString();
        SqlParameter[] checktransaction = new SqlParameter[]
        {
            new SqlParameter ("@Loginid",courentlevel),            
        };
        dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_checktransactionisavailable", checktransaction);
        if (dr.Read())
        {
            try
            {
                if (txtFrmDate.Text == "")
                {
                    string[] datefrom = date.Split('/');
                    string tomonth = Convert.ToString(datefrom.GetValue(0));
                    string todate1 = Convert.ToString(datefrom.GetValue(1));
                    string year1 = Convert.ToString(datefrom.GetValue(2));
                    int datenow2 = Convert.ToInt32(todate1);
                    int tomonth2 = Convert.ToInt32(tomonth);
                    int year = Convert.ToInt32(year1);
                    try
                    {
                        SqlParameter[] gettransactionbeforetextdate = new SqlParameter[]
                        {
                       
                           new SqlParameter ("@Loginid",courentlevel),
                           new SqlParameter ("@datetxt_from",date), 
                           new SqlParameter ("@Result",SqlDbType.Int),
                        };
                        gettransactionbeforetextdate[2].Direction = ParameterDirection.Output;
                        dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_checkFirstTransactionDate", gettransactionbeforetextdate);
                        loginvariable = Convert.ToInt32(gettransactionbeforetextdate[2].Value);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.writeToLogFile(Convert.ToString(ex));
                        lblerror.Text = "There is some technical error, Please try after some time";
                        lblerror.ForeColor = Color.Red;
                    }
                    if (loginvariable != 1)
                    {
                        datenow2 = datenow2 - 1;
                        string nowdate;
                        if (datenow2 == 0)
                        {
                            if (tomonth2 == 1)
                            {
                                tomonth2 = 12;
                                year = year - 1;
                                datenow2 = 31;
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                            else if (tomonth2 == 5 || tomonth2 == 7 || tomonth2 == 8 || tomonth2 == 10 || tomonth2 == 12)
                            {
                                tomonth2 = tomonth2 - 1;
                                datenow2 = 30;
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                            else if (tomonth2 == 2 || tomonth2 == 4 || tomonth2 == 6 || tomonth2 == 9 || tomonth2 == 11)
                            {
                                tomonth2 = tomonth2 - 1;
                                datenow2 = 31;
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                            else
                            {
                                tomonth2 = tomonth2 - 1;
                                datenow2 = 28;
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                        }
                        else
                        {
                            nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                        }
                        nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                        try
                        {
                            SqlParameter[] openingbal = new SqlParameter[]
                            {
                                new SqlParameter("@Agentid",courentlevel),
                                new SqlParameter("@Fromdate", nowdate),
                            };
                            dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE", openingbal);
                        }
                        catch (Exception ex)
                        {
                            lblerror.Text = "There is some technical error, Please try after some time";
                            lblerror.ForeColor = Color.Red;
                        }
                        while (dr.Read() != true)
                        {
                            datenow2 = datenow2 - 1;
                            if (datenow2 == 0)
                            {
                                if (tomonth2 == 1)
                                {
                                    tomonth2 = 12;
                                    year = year - 1;
                                    datenow2 = 31;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                                else if (tomonth2 == 5 || tomonth2 == 7 || tomonth2 == 8 || tomonth2 == 10 || tomonth2 == 12)
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 30;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                                else if (tomonth2 == 2 || tomonth2 == 4 || tomonth2 == 6 || tomonth2 == 9 || tomonth2 == 11)
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 31;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                                else
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 28;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                            }
                            else
                            {
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                            try
                            {
                                SqlParameter[] openingbal1 = new SqlParameter[]
                                {
                                    new SqlParameter("@Agentid",courentlevel.ToString()),
                                    new SqlParameter("@Fromdate", nowdate),
                                };
                                dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE", openingbal1);
                            }
                            catch(Exception ex)
                            {

                            }
                        }
                        lblopeningbalance.Text = Convert.ToString(dr["openingbalance"]);
                        dr.Dispose();
                        dr.Dispose();
                    }
                    else
                    {
                        lblopeningbalance.Text = "0.00";
                    }
                    dr.Dispose();
                }
                else
                {
                    string datefrom1 = txtFrmDate.Text.Trim();
                    string[] datefrom2 = datefrom1.Split('/');
                    string tomonth = Convert.ToString(datefrom2.GetValue(0));
                    string todate1 = Convert.ToString(datefrom2.GetValue(1));
                    string year1 = Convert.ToString(datefrom2.GetValue(2));
                    int datenow2 = Convert.ToInt32(todate1);
                    int tomonth2 = Convert.ToInt32(tomonth);
                    int DATEADD = Convert.ToInt32(todate1);
                    int MONTHADD = Convert.ToInt32(tomonth);
                    int year = Convert.ToInt32(year1);
                    string nowdate;
                    if (datenow2 == 1)
                    {
                        if (tomonth2 == 1)
                        {
                            tomonth2 = 12;
                            year = year - 1;
                            datenow2 = 31;
                            nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                        }
                        else if (tomonth2 == 5 || tomonth2 == 7 || tomonth2 == 8 || tomonth2 == 10 || tomonth2 == 12)
                        {
                            tomonth2 = tomonth2 - 1;
                            datenow2 = 30;
                            //nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                        }
                        else if (tomonth2 == 2 || tomonth2 == 4 || tomonth2 == 6 || tomonth2 == 9 || tomonth2 == 11)
                        {
                            tomonth2 = tomonth2 - 1;
                            datenow2 = 31;
                        }
                        else
                        {
                            tomonth2 = tomonth2 - 1;
                            datenow2 = 28;
                            // nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                        }
                    }
                    else
                    {
                        datenow2 = datenow2 - 1;
                    }

                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                    try
                    {
                        SqlParameter[] gettransactionbeforetextdate = new SqlParameter[]
                        {
                       
                           new SqlParameter ("@Loginid",courentlevel),
                           new SqlParameter ("@datetxt_from",nowdate), 
                           new SqlParameter ("@Result",SqlDbType.Int),
                        };
                        gettransactionbeforetextdate[2].Direction = ParameterDirection.Output;
                        dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_checkjoiningdate", gettransactionbeforetextdate);
                        loginvariable = Convert.ToInt32(gettransactionbeforetextdate[2].Value);
                    }
                    catch (Exception ex)
                    {
                        CommonFunction.writeToLogFile(Convert.ToString(ex));
                        lblerror.Text = "There is some technical error, Please try after some time";
                        lblerror.ForeColor = Color.Red;
                    }
                    if (loginvariable != 1)
                    {
                        try
                        {
                            SqlParameter[] openingbal = new SqlParameter[]
                            {
                                new SqlParameter("@Agentid",courentlevel),
                                new SqlParameter("@Fromdate",nowdate),                
                            };
                            dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE", openingbal);
                        }
                        catch(Exception ex)
                        {
                            lblerror.Text = "There is some technical error, Please try after some time";
                            lblerror.ForeColor = Color.Red;
                        }
                        while (dr.Read() != true)
                        {
                            datenow2 = datenow2 - 1;
                            if (datenow2 == 0)
                            {
                                if (tomonth2 == 1)
                                {
                                    tomonth2 = 12;
                                    year = year - 1;
                                    datenow2 = 31;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                                else if (tomonth2 == 5 || tomonth2 == 7 || tomonth2 == 8 || tomonth2 == 10 || tomonth2 == 12)
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 30;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                                else if (tomonth2 == 2 || tomonth2 == 4 || tomonth2 == 6 || tomonth2 == 9 || tomonth2 == 11)
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 31;
                                }
                                else
                                {
                                    tomonth2 = tomonth2 - 1;
                                    datenow2 = 28;
                                    nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                                }
                            }
                            else
                            {
                                nowdate = (tomonth2 + "-" + datenow2 + "-" + year);
                            }
                            SqlParameter[] openingbal1 = new SqlParameter[]
                            {
                                new SqlParameter("@Agentid",courentlevel),
                                new SqlParameter("@Fromdate", nowdate),
                            };
                            dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE", openingbal1);
                        }
                        lblopeningbalance.Text = Convert.ToString(dr["openingbalance"]);
                        dr.Dispose();
                        dr.Dispose();
                    }
                    else
                    {
                        DATEADD = DATEADD + 1;
                        nowdate = (MONTHADD + "-" + DATEADD + "-" + year);
                        SqlParameter[] openingbal = new SqlParameter[]
                        {
                            new SqlParameter("@Agentid",courentlevel),
                            new SqlParameter("@Fromdate",nowdate),                
                        };
                        dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE", openingbal);

                        while (dr.Read() != true)
                        {
                            DATEADD = DATEADD + 1;
                            nowdate = (MONTHADD + "-" + DATEADD + "-" + year);
                            SqlParameter[] openingbal1 = new SqlParameter[]
                        {
                            new SqlParameter("@Agentid",courentlevel),
                            new SqlParameter("@Fromdate", nowdate),
                        };
                            dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "SP_GETOPENINGBALANCE_ZERO", openingbal1);
                        }
                        lblopeningbalance.Text = Convert.ToString(dr["openingbalance"]);
                        dr.Dispose();
                        dr.Dispose();
                    }
                    dr.Dispose();
                }
            }
            catch (Exception ex)
            {
                lblerror.Text = "There is some technical error, Please try after some time";
                lblerror.ForeColor = Color.Red;
            }
        }
        else
        {
            lblopeningbalance.Text = "0.00";
        }
        dr.Dispose();
    }
    private void BindDataAccSummery()
    {
        //string todaysdate = DateTime.Now.ToShortDateString();
        //string[] datetoday = todaysdate.Split('/');
        string tomonth = Convert.ToString(DateTime.Now.Month);
         if (tomonth.Length < 2)
        {
            tomonth = "0" + tomonth;
        }
        string todate1 = Convert.ToString(DateTime.Now.Day);
        string year = Convert.ToString(DateTime.Now.Year);
        string nowdate = (tomonth + "-" + todate1 + "-" + year); 
        try
        {
            if (txtFrmDate.Text == "" )
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",nowdate),
                    new SqlParameter("@Todate",nowdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONMD", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                DataTable dt=ds.Tables["Table"].Copy();
                DataView dv = new DataView(dt);
                dv.Sort = "TRANSACTION_DATETIME ASC";
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReport.DataSource = dv;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    //lblopeningbalance.Visible = true;
                    //lblBalance.Visible = true;
                    btndownload.Visible = true;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    //new SqlParameter("@ServiceType",ddlSelectService.SelectedItem.Text),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONMD", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                } 
                if (ds.Tables[0].Rows.Count != 0)
                {
                    DataTable dt = ds.Tables["Table"].Copy();
                    DataView dv = new DataView(dt);
                    dv.Sort = "TRANSACTION_DATETIME ASC";
                    gvAccountReport.DataSource = dv;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    //lblopeningbalance.Visible = true;
                    //lblBalance.Visible = true;
                    gvAccountReportAGDS.Visible = false;
                    gvAccountReport.Visible = true;
                    gvAccountReportAGDS.Visible = true;
                }               
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvAccountReport.Visible = false;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                    gvAccountReportAGDS.Visible = false;
                }
            }

        }
        catch (Exception ex)
        {
            lblerror.Text = "There is some technical error, Please try after some time";
            lblerror.ForeColor = Color.Red;
        }
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtFrmDate.Text).Date > Convert.ToDateTime(date))
            {
                lblerror.Text = "Date must be less then  current date";
                lblerror.ForeColor = Color.Red;
            }
            else
            {
                if (DDL_UserType.SelectedItem.Text.Equals("Self"))
                {
                    loginid = Session["LOGINID"].ToString();
                    courentlevel = loginid;
                    BindDataAccSummery();
                }
                else
                {
                    loginid = ddl_partner_id.SelectedValue.ToString();
                    courentlevel = loginid;
                    AccountSummary();
                }
            }
        }
        catch(Exception ex)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    protected void btndownload_Click(object sender, EventArgs e)
    {
        DateTime todaysdate = DateTime.Now;
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure,"USP_ALLTRANSACTIONMD", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    lblerror.Visible = false;
                    Session["Dataset"] = ds;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 2, 3, 1, 11, 7, 9, 12, 8, 6, 13 };
                    RKLib.ExportData.Export objExport = new
                    RKLib.ExportData.Export("Web");
                    objExport.ExportDetails(dtEmployee,
                    iColumns, Export.ExportFormat.CSV,
                    "C:\\TxnAccountSummery.csv");
                }               
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    btndownload.Visible = true;
                    lblerror.Visible = false;
                    Session["Dataset"] = ds;
                    DataTable dtEmployee = ds.Tables["Table"].Copy();
                    int[] iColumns = { 3, 4, 2, 12, 6, 7, 8, 9, 10, };
                    RKLib.ExportData.Export objExport = new
                    RKLib.ExportData.Export("Web");
                    objExport.ExportDetails(dtEmployee,
                    iColumns, Export.ExportFormat.CSV,
                    "C:\\TxnAccountSummery.csv");
                } 
            }
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
            throw (ex);
        }
    }
    protected void gvAccountReport_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAccountReport.PageIndex = e.NewPageIndex;
        BindDataAccSummery();
    }

    protected void gvAccountReportAGDS_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            gvAccountReportAGDS.PageIndex = e.NewPageIndex;
            AccountSummary();
        }
        catch (Exception ex)
        {
            throw (ex);
        }  
    }
    protected void DDL_UserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (DDL_UserType.SelectedItem.Text.Equals("Self"))
            {
                lbluserid.Visible = false;
                ddl_partner_id.Visible = false;
            }
            if (DDL_UserType.SelectedItem.Text.Equals("Distributor"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Distributor"), 
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributorAgentOfMDS", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
            if (DDL_UserType.SelectedItem.Text.Equals("Agent"))
            {
                SqlParameter[] GetAgent = new SqlParameter[]
                {
                    new SqlParameter("@Loginid",Session["LOGINID"].ToString()),
                    new SqlParameter("@Usertype","Agent"),
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_GetDistributorAgentOfMDS", GetAgent);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddl_partner_id.DataSource = ds;
                    ddl_partner_id.DataTextField = "USERNAME";
                    ddl_partner_id.DataValueField = "LOGINID";
                    ddl_partner_id.DataBind();
                    lbluserid.Visible = true;
                    ddl_partner_id.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    private void BindDataAccSummeryAGDS()
    {
        string todaysdate = DateTime.Now.ToShortDateString();
        string[] datetoday = todaysdate.Split('/');
        string tomonth = Convert.ToString(datetoday.GetValue(0));
        string todate1 = Convert.ToString(datetoday.GetValue(1));
        string year = Convert.ToString(datetoday.GetValue(2));

        string nowdate = (tomonth + "-" + todate1 + "-" + year);
        try
        {
            if (txtFrmDate.Text == "" )
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",nowdate),
                    new SqlParameter("@Todate",nowdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                DataTable dt = ds.Tables["Table"].Copy();
                DataView dv = new DataView(dt);
                dv.Sort = "TRANSACTION_DATETIME ASC";
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReportAGDS.DataSource = dv;
                    gvAccountReportAGDS.DataBind();
                    lblerror.Visible = false;
                    lblopeningbalance.Visible = true;
                    lblBalance.Visible = true;
                    btndownload.Visible = true;
                    gvAccountReportAGDS.Visible = true;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTION", param);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                if (ds.Tables[0].Rows.Count != 0)
                {
                    DataTable dt = ds.Tables["Table"].Copy();
                    DataView dv = new DataView(dt);
                    dv.Sort = "TRANSACTION_DATETIME ASC";
                    gvAccountReportAGDS.DataSource = dv;
                    gvAccountReportAGDS.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    lblopeningbalance.Visible = true;
                    lblBalance.Visible = true;
                    gvAccountReport.Visible = false;
                    gvAccountReportAGDS.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvAccountReport.Visible = false;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                    gvAccountReport.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    private void AccountSummary()
    {
        string tomonth = Convert.ToString(DateTime.Now.Month);
        if (tomonth.Length < 2)
        {
            tomonth = "0" + tomonth;
        }
        string todate1 = Convert.ToString(DateTime.Now.Day);
        string year = Convert.ToString(DateTime.Now.Year);
        string nowdate = (tomonth + "-" + todate1 + "-" + year); 
        try
        {
            if (txtFrmDate.Text == "")
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",nowdate),
                    new SqlParameter("@Todate",nowdate)
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReport.DataSource = ds;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    gvAccountReport.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvAccountReport.Visible = false;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                }
            }
            else
            {
                SqlParameter[] param = new SqlParameter[]
                {
                    new SqlParameter("@loginid",courentlevel),
                    new SqlParameter("@Fromdate",txtFrmDate.Text.Trim()),
                    new SqlParameter("@Todate",txtToDate.Text.Trim())
                };
                DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_ALLTRANSACTIONNew", param);
                if (ds.Tables[0].Rows.Count != 0)
                {
                    gvAccountReport.DataSource = ds;
                    gvAccountReport.DataBind();
                    lblerror.Visible = false;
                    btndownload.Visible = true;
                    gvAccountReport.Visible = true;
                }
                else
                {
                    lblerror.Visible = true;
                    lblerror.Text = "No transaction available";
                    lblerror.ForeColor = Color.Red;
                    gvAccountReport.Visible = false;
                    lblopeningbalance.Visible = false;
                    lblBalance.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some technical problem. Please try again');"), true);
        }
    }
}
