protected void OnBtnSaveClicked(object sender, EventArgs e)
    {
        InvoiceDetailsRepository repo = new InvoiceDetailsRepository();
        InvoicesRepository repoInv = new InvoicesRepository();
        if (!string.IsNullOrEmpty(Request.QueryString["InvoiceDetailId"]))
        {
            string[] key = Request.QueryString["InvoiceDetailId"].Split('-');
            int idFactNumber = int.Parse(key[0]);
            string type = key[1];
            int idYear = int.Parse(key[2]);
            int idLigneNumber = int.Parse(key[3]);
            IList<InvoiceDetails> invDetailList = repo.GetInvoiceDetailsOfInvoice(
                idFactNumber, type, idYear, idLigneNumber);
            if (invDetailList.Count == 1)
            {
                InvoiceDetails invDetail = invDetailList[0];
                invDetail.Description = txtDescription.Text;
                invDetail.Quantity = txtQuantity.Value;
                invDetail.UnitPriceEuro = txtUnitPrice.Value;
                invDetail.AmountEuro = (invDetail.Quantity.HasValue ? invDetail.Quantity.Value : 0) *
                    (invDetail.UnitPriceEuro.HasValue ? invDetail.UnitPriceEuro.Value : 0);
                invDetail.VatCode = int.Parse(ddlVAT.SelectedValue);
                repo.UpdateInvoiceDetails(invDetail);
                repoInv.ReComputeAmountOfInvoice(idFactNumber, type, idYear);
            }
        }
        else if (!string.IsNullOrEmpty(Request.QueryString["InvoiceIdPK"]))
        {
            string[] key = Request.QueryString["InvoiceIdPK"].Split('-');
            int idFactNumber = int.Parse(key[0]);
            string type = key[1];
            int idYear = int.Parse(key[2]);
            int? idLigneNumber = repo.GetMaxInvoiceDetailOrderNumber(idFactNumber, type, idYear);
            if (!idLigneNumber.HasValue)
                idLigneNumber = 1;
            else
                idLigneNumber += 1;
            InvoiceDetails invDetail = new InvoiceDetails();
            invDetail.IdFactNumber = idFactNumber;
            invDetail.IdTypeInvoice = type;
            invDetail.IdYear = idYear;
            invDetail.IdLigneNumber = idLigneNumber.Value;

            invDetail.Description = txtDescription.Text;
            invDetail.Quantity = txtQuantity.Value;
            invDetail.UnitPriceEuro = txtUnitPrice.Value;
            invDetail.AmountEuro = (invDetail.Quantity.HasValue ? invDetail.Quantity.Value : 0) *
                    (invDetail.UnitPriceEuro.HasValue ? invDetail.UnitPriceEuro.Value : 0);
            invDetail.VatCode = int.Parse(ddlVAT.SelectedValue);
            repo.InserNewInvoiceDetails(invDetail);
            repoInv.ReComputeAmountOfInvoice(idFactNumber, type, idYear);
        }

        string script = "<script type=\"text/javascript\">";
        script += " OnBtnSaveClientClicked();";
        script += " </script>";

        if (!ClientScript.IsClientScriptBlockRegistered("saveAndCloseWindow"))
            ClientScript.RegisterStartupScript(this.GetType(), "saveAndCloseWindow", script);
    }