protected void addUnit_Click(object sender, EventArgs e)
        {
            string Name = UnitName.Text;
            string Symbol = UnitSymbol.Text;

            if (Name != "" & Symbol != "")
            {
                ParameterService parameterService = new ParameterService();
                UnitsInsertView unit = new UnitsInsertView();
                unit.UnitName = Name;
                unit.UnitSymbol = Symbol;

                CUDView crud = parameterService.insertUnits(unit);

                if (crud.insert == false)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not insert the Value')", true);
                }
                else
                {
                    UnitName.Text = "";
                    UnitSymbol.Text = "";

                    fillUnitGroupTable();
                    updateUnitModal.Update();
                }

            }
        }
        /*
        Metodo que inserta las unidades desde la ventana de add units
        */
        protected void addUnit_Click(object sender, EventArgs e)
        {
            string Name = UnitName.Text;
            string Symbol = UnitSymbol.Text;

            if (Name != "" & Symbol != "")
            {
                ParameterService parameterService = new ParameterService();
                UnitsInsertView unit = new UnitsInsertView();
                unit.UnitName = Name;
                unit.UnitSymbol = Symbol;

                String user = Context.User.Identity.Name;
                AuditDataFromWeb audit = new AuditDataFromWeb();
                audit.Reason = "N/A";
                audit.StationIP = General.getIp(this.Page);
                audit.UserName = user;

                CUDView crud = parameterService.insertUnits(unit, audit);

                if (crud.insert == false)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not insert the Value')", true);
                }
                else
                {
                    UnitName.Text = "";
                    UnitSymbol.Text = "";

                    fillUnitGroupTable();
                    updateUnitModal.Update();
                }

            }
        }
        /*
        Metodo que utiliza para realizar actualizaciones
        */
        private void updateAuxiliar()
        {
            //Se verifica que se haya seleccionado alguna fila de la tabla
            if (HttpContext.Current.Session["ProcessId"] != null)
            {
                int ProcessIdNew = Int32.Parse(HttpContext.Current.Session["ProcessId"].ToString());
                //Se verifica que se hayan añadido parametros
                if (verifyParameterTable())
                {
                    //Se crea el servicio
                    ParameterService parameterService = new ParameterService();
                    String reason = Session["reason"].ToString();
                    String user = Context.User.Identity.Name;
                    //Se llenan los datos de la auditoria
                    AuditDataFromWeb audit = new AuditDataFromWeb();
                    audit.Reason = reason;
                    audit.StationIP = General.getIp(this.Page);
                    audit.UserName = user;

                    //Insert Parameters
                    foreach (GridViewRow gvrow in GridView1.Rows)
                    {
                        ParameterUpdateView parameter = new ParameterUpdateView();
                        TextBox lblParameter = (TextBox)gvrow.FindControl("Parameter");
                        TextBox lblMinValue = (TextBox)gvrow.FindControl("MinValue");
                        DropDownList lblUnit = (DropDownList)gvrow.FindControl("Unit");
                        CheckBox lblxPlot = (CheckBox)gvrow.FindControl("xPlot");
                        CheckBox lblyPlot = (CheckBox)gvrow.FindControl("yPlot");
                        TextBox lblIncer = (TextBox)gvrow.FindControl("Incer");

                        Label lblParameterId = (Label)gvrow.FindControl("ParameterId");

                        if (lblParameter.Text != "")
                        {
                            parameter.ParameterMinValue = Double.Parse(lblMinValue.Text);
                            parameter.ParameterName = lblParameter.Text;
                            parameter.ParameterXPlot = lblxPlot.Checked;
                            parameter.ParameterYPlot = lblyPlot.Checked;
                            parameter.ParameterYPlotRange = Int32.Parse(lblIncer.Text);

                            parameter.ProcessId = ProcessIdNew;
                            parameter.UnitId = Int32.Parse(lblUnit.SelectedItem.Value); //aqui se cae
                            parameter.ParameterId = Int32.Parse(lblParameterId.Text);
                            //Se realiza la actualizacion
                            CUDView crud = parameterService.updateParameter(parameter, audit);

                            if (crud.update == false)
                            {
                                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not update the Parameters')", true);
                            }
                            else
                            {
                                clearFields();
                                fillTable();
                            }
                        }
                    }
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Some parameters in Parameter Table has some incorrrect data format.')", true);
                }

            }
        }
        /*
        Metodo que se utiliza para realizar inserciones
        */
        private void saveAuxiliar()
        {
            //Se verifica que se haya  el producto y el proceso no sean nulos
            if (ProductText.SelectedItem != null & ProcessText.SelectedItem != null)
            {
                //Se verifica que se haya completado el producto y el proceso, es decir que no esten en blanco
                if (ProductText.SelectedItem.Value != "" & ProcessText.SelectedItem.Value != "")
                {
                    //Se verifica que se haya añadido al menos un parametro
                    if (validateParameter())
                    {
                        int ProcessIdNew = Int32.Parse(HttpContext.Current.Session["ProcessId"].ToString());

                        if (verifyParameterTable())
                        {
                            //Se crea el servicio
                            ParameterService parameterService = new ParameterService();

                            //Insert Parameters
                            foreach (GridViewRow gvrow in GridView1.Rows)
                            {
                                ParameterInsertView parameter = new ParameterInsertView();

                                TextBox lblParameter = (TextBox)gvrow.FindControl("Parameter");
                                TextBox lblMinValue = (TextBox)gvrow.FindControl("MinValue");
                                DropDownList lblUnit = (DropDownList)gvrow.FindControl("Unit");
                                CheckBox lblxPlot = (CheckBox)gvrow.FindControl("xPlot");
                                CheckBox lblyPlot = (CheckBox)gvrow.FindControl("yPlot");
                                TextBox lblIncer = (TextBox)gvrow.FindControl("Incer");

                                if (lblParameter.Text != "")
                                {
                                    parameter.ParameterMinValue = Double.Parse(lblMinValue.Text);
                                    parameter.ParameterName = lblParameter.Text;
                                    parameter.ParameterXPlot = lblxPlot.Checked;
                                    parameter.ParameterYPlot = lblyPlot.Checked;
                                    parameter.ParameterYPlotRange = Int32.Parse(lblIncer.Text);
                                    parameter.ProcessId = ProcessIdNew;
                                    parameter.UnitId = Int32.Parse(lblUnit.SelectedItem.Value);
                                    //Se llenan los datos de la auditoria
                                    String user = Context.User.Identity.Name;
                                    AuditDataFromWeb audit = new AuditDataFromWeb();
                                    audit.Reason = "N/A";
                                    audit.StationIP = General.getIp(this.Page);
                                    audit.UserName = user;

                                    //Se realiza la insercion
                                    CUDView crud = parameterService.insertParameter(parameter, audit);
                                    if (crud.insert == false)
                                    {
                                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not insert the Parameters')", true);
                                    }
                                }
                            }
                            clearFields();
                            fillTable();
                        }
                        else
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Some parameters in Parameter Table has some incorrrect data format.')", true);
                        }
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Add at least one parameter.')", true);

                    }
                }
            }
        }
 /*
 Metodo para llenar la tabla de unidades
 */
 private void fillUnitGroupTable()
 {
     ParameterService parameterService = new ParameterService();
     List<UnitView> parameterData = parameterService.getUnits();
     GridView2.DataSource = parameterData;
     GridView2.DataBind();
 }
 /*
 Metodo que rellena el unit group
 */
 private void fillUnitGroup()
 {
     ParameterService parameterService = new ParameterService();
     List<UnitView> parameterData = parameterService.getUnits();
     UnitText.DataSource = parameterData;
     UnitText.DataBind();
 }
 /*
 Metodo que llena la tabla de parametros
 */
 private void fillTable()
 {
     ParameterService parameterService = new ParameterService();
     List<ParameterView> parameterData = parameterService.getParameter();
     GridView3.DataSource = parameterData;
     GridView3.PageIndex = 0;
     GridView3.DataBind();
 }
        /*
        Metodo que carga los productos en el dropdown list
        */
        private void fillProducts()
        {
            ParameterService parameterService = new ParameterService();
            List<ProductsView> products = parameterService.getProducts();
            ProductText.DataSource = products;
            ProductText.DataBind();

            ListItem l = new ListItem("", "", true);
            l.Selected = true;
            ProductText.Items.Insert(0, l);
        }
 /*
 Metodo que llena la tabla de parametros, a partir del id del proceso
 */
 private void fillParameterTable(string processId)
 {
     ParameterService parameterService = new ParameterService();
     List<ParameterProcessView> parameters = parameterService.getParameterByProcess(processId);
     GridView1.DataSource = parameters;
     GridView1.DataBind();
 }
        protected void ProcessText_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProcessText.SelectedItem.Value != "")
            {
                string ProcessSelected = ProcessText.SelectedItem.Value;
                HttpContext.Current.Session["ProcessId"] = ProcessSelected;

                ParameterService parameterService = new ParameterService();
                bool response = parameterService.hadProcessParameter(ProcessSelected);

                if (response == true)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "AddParameter", "AddParameter();", true);
                }
                else
                {
                    fillParameterTableNone(Int32.Parse(ProcessSelected));
                }
            }
            else
            {
                clearParameterTable();
            }
        }
 /*
 Metodo que llena la tabla de parametros con los datos filtrados por la busqueda
 */
 protected void searchButton_Click(object sender, ImageClickEventArgs e)
 {
     ParameterService parameterService = new ParameterService();
     List<ParameterView> parameters = parameterService.searchProcess(searchText.Text);
     GridView3.DataSource = parameters;
     GridView3.DataBind();
 }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //
                Label lblParameterId = (e.Row.FindControl("ParameterId") as Label);

                //Find the DropDownList in the Row
                DropDownList ddlCountries = (e.Row.FindControl("Unit") as DropDownList);

                ParameterService parameterService = new ParameterService();
                List<UnitView> parameterData = parameterService.getUnits();

                ddlCountries.DataSource = parameterData;
                ddlCountries.DataTextField = "UnitSymbol";
                ddlCountries.DataValueField = "UnitId";
                ddlCountries.DataBind();

                //Add Default Item in the DropDownList
                // ddlCountries.Items.Insert(0, new ListItem("Please select"));

                UnitView unit = parameterService.getUnitFromParameter(Int32.Parse(lblParameterId.Text));

                ListItem l = new ListItem(unit.UnitSymbol, unit.UnitId + "", true);
                int indexUnit = ddlCountries.Items.IndexOf(l);

                ddlCountries.SelectedIndex = indexUnit;

            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find the DropDownList in the Row
                DropDownList ddlCountries = (e.Row.FindControl("Unit") as DropDownList);

                ParameterService parameterService = new ParameterService();
                List<UnitView> parameterData = parameterService.getUnits();

                ddlCountries.DataSource = parameterData;
                ddlCountries.DataTextField = "UnitName";
                ddlCountries.DataValueField = "UnitId";
                ddlCountries.DataBind();

                //Add Default Item in the DropDownList
                ddlCountries.Items.Insert(0, new ListItem("Please select"));

            }
        }
        private void updateAuxiliar()
        {
            if (HttpContext.Current.Session["ProcessId"] != null)
            {
                int ProcessIdNew = Int32.Parse(HttpContext.Current.Session["ProcessId"].ToString());

                if (verifyParameterTable())
                {
                    ParameterService parameterService = new ParameterService();

                    //Insert Parameters
                    foreach (GridViewRow gvrow in GridView1.Rows)
                    {
                        ParameterUpdateView parameter = new ParameterUpdateView();
                        TextBox lblParameter = (TextBox)gvrow.FindControl("Parameter");
                        TextBox lblMinValue = (TextBox)gvrow.FindControl("MinValue");
                        DropDownList lblUnit = (DropDownList)gvrow.FindControl("Unit");
                        CheckBox lblxPlot = (CheckBox)gvrow.FindControl("xPlot");
                        CheckBox lblyPlot = (CheckBox)gvrow.FindControl("yPlot");
                        TextBox lblIncer = (TextBox)gvrow.FindControl("Incer");

                        Label lblParameterId = (Label)gvrow.FindControl("ParameterId");

                        if (lblParameter.Text != "")
                        {
                            parameter.ParameterMinValue = Double.Parse(lblMinValue.Text);
                            parameter.ParameterName = lblParameter.Text;
                            parameter.ParameterXPlot = lblxPlot.Checked;
                            parameter.ParameterYPlot = lblyPlot.Checked;
                            parameter.ParameterYPlotRange = Int32.Parse(lblIncer.Text);

                            parameter.ProcessId = ProcessIdNew;
                            parameter.UnitId = Int32.Parse(lblUnit.SelectedItem.Value); //aqui se cae
                            parameter.ParameterId = Int32.Parse(lblParameterId.Text);

                            CUDView crud = parameterService.updateParameter(parameter);

                            if (crud.update == false)
                            {
                                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not update the Parameters')", true);
                            }
                            else
                            {
                                clearFields();
                                fillTable();
                            }
                        }
                    }
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Some parameters in Parameter Table has some incorrrect data format.')", true);
                }

            }
        }
        /*
        Metodo que se utiliza para actualizar la unidad
        */
        private void updateUnitAux()
        {
            if (HttpContext.Current.Session["UnitId"] != null)
            {
                string Name = UnitName.Text;
                string Symbol = UnitSymbol.Text;

                if (Name != "" & Symbol != "")
                {
                    ParameterService parameterService = new ParameterService();

                    String reason = Session["reason"].ToString();
                    String user = Context.User.Identity.Name;
                    AuditDataFromWeb audit = new AuditDataFromWeb();
                    audit.Reason = reason;
                    audit.StationIP = General.getIp(this.Page);
                    audit.UserName = user;

                    UnitView unit = new UnitView();
                    unit.UnitName = Name;
                    unit.UnitSymbol = Symbol;
                    unit.UnitId = Int32.Parse(HttpContext.Current.Session["UnitId"].ToString());
                    CUDView crud = parameterService.updateUnits(unit, audit);

                    if (crud.update == false)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not update the Value')", true);
                    }
                    else
                    {
                        UnitName.Text = "";
                        UnitSymbol.Text = "";

                        fillUnitGroupTable();
                        updateUnitModal.Update();
                    }
                    HttpContext.Current.Session["operation"] = "create";
                    showUnitModal();
                }
            }
        }
        /*
        Metodo para llenar el dropdown list de process, cuando un producto es seleccionado
        */
        private void fillProcess(int ProductSelected)
        {
            ParameterService parameterService = new ParameterService();
            List<RecipeProcessView> products = parameterService.getProcessForProduct(ProductSelected);
            ProcessText.DataSource = products;
            ProcessText.DataBind();

            ListItem l = new ListItem("", "", true);
            l.Selected = true;
            ProcessText.Items.Insert(0, l);
        }
 /*Funcion: Obtiene una lista de parametros de un proceso
   Param:  processID
   Return: Lista ParameterProcessView
   */
 public List<ParameterProcessView> getParameterByProcess(String processID)
 {
     ParameterService service = new ParameterService();
     return service.getParameterByProcess(processID);
 }
        protected void updateUnit_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["UnitId"] != null)
            {
                string Name = UnitName.Text;
                string Symbol = UnitSymbol.Text;

                if (Name != "" & Symbol != "")
                {
                    ParameterService parameterService = new ParameterService();

                    UnitView unit = new UnitView();
                    unit.UnitName = Name;
                    unit.UnitSymbol = Symbol;
                    unit.UnitId = Int32.Parse(HttpContext.Current.Session["UnitId"].ToString());
                    CUDView crud = parameterService.updateUnits(unit);

                    if (crud.update == false)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Could not update the Value')", true);
                    }
                    else
                    {
                        UnitName.Text = "";
                        UnitSymbol.Text = "";

                        fillUnitGroupTable();
                        updateUnitModal.Update();
                    }
                }
            }
        }