protected void cboClientList_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtClient.Text = "";
        long ClientID = Convert.ToInt32(cboClientList.SelectedValue.ToString());

        PCSN.InvoiceSystem.BusinessLogicLayer.Client client = new PCSN.InvoiceSystem.BusinessLogicLayer.Client();
        dtClient = client.GetClientByID(ClientID);

        if (dtClient.Rows.Count <= 0)
        {
            txtClient.Text = "Our Client";
        }
        else
        {
            txtClientID.Text = dtClient.Rows[0]["ID"].ToString();
            txtClient.Text   = dtClient.Rows[0]["ClientName"].ToString() + Environment.NewLine;
            txtClient.Text   = txtClient.Text + dtClient.Rows[0]["CompanyName"].ToString() + Environment.NewLine;
            txtClient.Text   = txtClient.Text + dtClient.Rows[0]["Address"].ToString() + Environment.NewLine;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["OrderMasterID"] != null && Request.QueryString["OrderMasterID"].ToString() != "" && Request.QueryString["OrderDetailID"] != null && Request.QueryString["OrderDetailID"].ToString() != "" && !Page.IsPostBack)
        {
            // This is for Updating and ITEM in Order
            long OrdermasterID = Convert.ToInt32(Request.QueryString["OrderMasterID"].ToString());
            PCSN.InvoiceSystem.BusinessLogicLayer.Order Order = new PCSN.InvoiceSystem.BusinessLogicLayer.Order();
            txtOrderMasterID.Text = Request.QueryString["OrderMasterID"].ToString();
            lblTotalAmount.Text   = "0";
            dtOrderEdit           = Order.GetOrderByID(OrdermasterID);
            for (int a = 0; a < dtOrderEdit.Rows.Count; a++)
            {
                if (dtOrderEdit.Rows[a]["OrderDetailID"].ToString() == Request.QueryString["OrderDetailID"].ToString())
                {
                    lblDetailDate.Text          = Convert.ToDateTime(dtOrderEdit.Rows[a]["OrderDate"].ToString()).ToShortDateString();
                    txtItem.Text                = dtOrderEdit.Rows[a]["Item"].ToString();
                    txtItemID.Text              = dtOrderEdit.Rows[a]["OrderDetailID"].ToString();
                    txtDescription.Text         = dtOrderEdit.Rows[a]["Description"].ToString();
                    txtQuantity.Text            = dtOrderEdit.Rows[a]["Quantity"].ToString();
                    txtUnitPrice.Text           = dtOrderEdit.Rows[a]["UnitPrice"].ToString();
                    txtItemAmount.Text          = dtOrderEdit.Rows[a]["ItemAmount"].ToString();
                    cboClientList.SelectedValue = dtOrderEdit.Rows[a]["ClientID"].ToString();
                    txtDueDate.Text             = Convert.ToDateTime(dtOrderEdit.Rows[a]["DueDate"].ToString()).ToShortDateString();
                    //lblTotalAmount.Text = dtOrderEdit.Rows[a]["TotalAmount"].ToString();

                    if (dtOrderEdit.Rows[a]["ID"].ToString() != "")
                    {
                        PopulateOrders(Convert.ToInt32(dtOrderEdit.Rows[a]["ID"].ToString()));
                    }
                }

                lblTotalAmount.Text = Convert.ToString(Convert.ToInt32(lblTotalAmount.Text) + Convert.ToInt32(dtOrderEdit.Rows[a]["ItemAmount"].ToString()));
            }
        }

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["OrderMasterIDEdit"] != null && Request.QueryString["OrderMasterIDEdit"].ToString() != "")
            {
                txtOrderMasterID.Text = Request.QueryString["OrderMasterIDEdit"].ToString();
            }
//            txtOrderMasterID.Text = "16";
            if (txtOrderMasterID.Text == "")
            {
                // This is when a Fresh Order is About to GeneUnitPrice
                txtClient.Text = "Please Select a Client from Above List";

                PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany ourCompany = new PCSN.InvoiceSystem.BusinessLogicLayer.OurCompany();
                dtOurCompany = ourCompany.GetOurCompanyByID(Convert.ToInt32("1"));

                if (dtOurCompany.Rows.Count <= 0)
                {
                    txtOurCompany.Text = "PC.Solutions.NET";
                }
                else
                {
                    txtOurCompanyID.Text = dtOurCompany.Rows[0]["ID"].ToString();
                    txtOurCompany.Text   = dtOurCompany.Rows[0]["CompanyName"].ToString() + Environment.NewLine;
                    txtOurCompany.Text   = txtOurCompany.Text + dtOurCompany.Rows[0]["CompanyAddress"].ToString() + Environment.NewLine;
                }


                lblHeaderDate.Text = Convert.ToString(DateTime.Now.ToShortDateString());
                lblDetailDate.Text = Convert.ToString(DateTime.Now.ToShortDateString());

                PCSN.InvoiceSystem.BusinessLogicLayer.Order Order = new PCSN.InvoiceSystem.BusinessLogicLayer.Order();

                dtOrder = Order.GetAllOrderMAX();
                if (dtOrder.Rows.Count == 0 || dtOrder.Rows[0]["ID"].ToString() == "")
                {
                    lblOrderNumber.Text = "1";
                }
                else
                {
                    lblOrderNumber.Text = Convert.ToInt32(Convert.ToInt32(dtOrder.Rows[0]["ID"].ToString()) + 1).ToString();
                }
                if (txtOrderMasterID.Text != "")
                {
                    PopulateOrders(Convert.ToInt32(txtOrderMasterID.Text));
                }
                PopulateClients();
            }
            else
            {
                // This is when the Order ID is getting from somewhere
                // Like from Query Strings
                long OrdermasterID = Convert.ToInt32(txtOrderMasterID.Text.ToString());
                lblOrderNumber.Text = OrdermasterID.ToString();
                PopulateClients();
                PopulateOrders(OrdermasterID);
                PCSN.InvoiceSystem.BusinessLogicLayer.Order Order = new PCSN.InvoiceSystem.BusinessLogicLayer.Order();

                lblTotalAmount.Text = "0";
                dtOrderEdit         = Order.GetOrderByID(OrdermasterID);
                for (int a = 0; a < dtOrderEdit.Rows.Count; a++)
                {
                    if (dtOrderEdit.Rows[a]["ItemAmount"].ToString() == null || dtOrderEdit.Rows[a]["ItemAmount"].ToString() == "")
                    {
                    }
                    else
                    {
                        lblTotalAmount.Text = Convert.ToString(Convert.ToInt32(lblTotalAmount.Text) + Convert.ToInt32(dtOrderEdit.Rows[a]["ItemAmount"].ToString()));
                    }
                }
                if (dtOrderEdit.Rows.Count > 0)
                {
                    //PopulateOrders(Convert.ToInt32(dtOrderEdit.Rows[0]["ID"].ToString()));

                    PCSN.InvoiceSystem.BusinessLogicLayer.Client client = new PCSN.InvoiceSystem.BusinessLogicLayer.Client();
                    dtClient = client.GetClientByID(Convert.ToInt32(dtOrderEdit.Rows[0]["ClientID"].ToString()));

                    if (dtClient.Rows.Count <= 0)
                    {
                        txtClient.Text = "Our Client";
                    }
                    else
                    {
                        cboClientList.SelectedValue = dtClient.Rows[0]["ID"].ToString();
                        txtClientID.Text            = dtClient.Rows[0]["ID"].ToString();
                        txtClient.Text = dtClient.Rows[0]["ClientName"].ToString() + Environment.NewLine;
                        txtClient.Text = txtClient.Text + dtClient.Rows[0]["CompanyName"].ToString() + Environment.NewLine;
                        txtClient.Text = txtClient.Text + dtClient.Rows[0]["Address"].ToString() + Environment.NewLine;
                    }

                    txtOurCompanyID.Text = "1";
                    txtOurCompany.Text   = "Power Protection Services" + Environment.NewLine;
                    txtOurCompany.Text   = txtOurCompany.Text + "XYZ Address" + Environment.NewLine;



                    lblHeaderDate.Text = Convert.ToString(Convert.ToDateTime(dtOrderEdit.Rows[0]["OrderDate"].ToString()).ToShortDateString());
                    txtDueDate.Text    = Convert.ToString(Convert.ToDateTime(dtOrderEdit.Rows[0]["DueDate"].ToString()).ToShortDateString());
                    lblDetailDate.Text = Convert.ToString(Convert.ToDateTime(dtOrderEdit.Rows[0]["OrderDate"].ToString()).ToShortDateString());
                }
            }
        }
    }