0

I have a problem in mi code, when show the error message appears the message of the title

This is the code of my form Register

     <div class="form-group col-md-6">
                                                @if (!Model.MODEL_PACIENTE.PAC_ID.Equals(0))
                                                {
                                                    <label>Comuna:</label>
                                                    @Html.DropDownList("MODEL_PACIENTE.PAC_COMUNA", new SelectList(Model.MODEL_PACIENTE.Comuna_Lista, "Text", "Text"),
                                 new { @class = "form-control" })
                                                }
                                                else
                                                {
                                                    <label>Comuna:</label>
                                                    @Html.DropDownList("MODEL_PACIENTE.PAC_COMUNA", new SelectList(Model.MODEL_PACIENTE.Comuna_Lista, "Text", "Text"),
"ESCOGA UNA COMUNA", new { @class = "form-control" }) // HERE IS THE ERROR WHEN MY DNI IS REGISTER IN THE BD                          }
                                                <span asp-validation-for="MODEL_PACIENTE.PAC_COMUNA" class="text-danger"></span>
                                            </div>

Code of my class GetOdontologo

 public List<SelectListItem> GetOdontologo(ApplicationDbContext context)
    {
        List<SelectListItem> selectListItems = null;
        try
        {
            selectListItems = new List<SelectListItem>();
            context.TBL_ODONTOLOGO.ToList().ForEach(item =>
            {
                selectListItems.Add(new SelectListItem
                {
                    Value = item.ODONT_ID.ToString(),
                   // Text = item.ODONT_CODIGO

                    Text = item.ODONT_CODIGO + '-' + item.ODONT_APELLIDO + ' ' + item.ODONT_NOMBRE
                });
            });
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: '{ex}'");
        }
        return selectListItems;

    }

This is the class GetComuna

  public List<SelectListItem> GetComuna(ApplicationDbContext context)
    {
        List<SelectListItem> selectListItems = null;
        try
        {
            selectListItems = new List<SelectListItem>();
            context.TBL_PROVINCIA.ToList().ForEach(item =>
            {
                selectListItems.Add(new SelectListItem
                {
                    Value = item.PROV_ID.ToString(),
                    Text = item.PROV_NOMBRE
                });
            });
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: '{ex}'");
        }
        return selectListItems;

    }

And this the code of my method "onget"

public void OnGet(int idActPac)
    {
        _DataPac2 = null;
        if(idActPac.Equals(0))
        {
            _DataPac2 = null;
        }
        if (_dataInput != null || _DataPac1 != null || _DataPac2 != null)
        {
            if (_dataInput != null)
            {
                MODEL_PACIENTE = _dataInput;
                MODEL_PACIENTE.AvatarImage = null;
            }
            else
            {
                if (_DataPac1 != null || _DataPac2 != null)
                {
                    if (_DataPac2 != null)
                    
                        _DataPac1 = _DataPac2;
                        MODEL_PACIENTE = new PACIENTE
                        {
                            PAC_ID = _DataPac1.PAC_ID,
                            PAC_NOMBRE = _DataPac1.PAC_NOMBRE,
                            PAC_APELLIDO = _DataPac1.PAC_APELLIDO,
                            PAC_CODIGO = _DataPac1.PAC_CODIGO,
                            PAC_EDAD = _DataPac1.PAC_EDAD,
                            PAC_COD_ODONT = _DataPac1.PAC_COD_ODONT,
                            PAC_COMUNA = _DataPac1.PAC_COMUNA,
                            PAC_CORREO = _DataPac1.PAC_CORREO,
                            PAC_DIRECCION = _DataPac1.PAC_DIRECCION,
                            PAC_OBSERVACIONES = _DataPac1.PAC_OBSERVACIONES,
                            PAC_OTRAS_COMUNAS = _DataPac1.PAC_OTRAS_COMUNAS,
                            PAC_CONVENIO = _DataPac1.PAC_CONVENIO,
                            PAC_PREVISIONES = _DataPac1.PAC_PREVISIONES,
                            PAC_REPRESENTANTE = _DataPac1.PAC_REPRESENTANTE,
                            PAC_RUT = _DataPac1.PAC_RUT,
                            PAC_SEXO = _DataPac1.PAC_SEXO,
                            PAC_TELEFONO = _DataPac1.PAC_TELEFONO,
                            PAC_FECHA_NAC = _DataPac1.PAC_FECHA_NAC,
                            PAC_FEC_ACT = _DataPac1.PAC_FEC_ACT,
                            PAC_FEC_REG = _DataPac1.PAC_FEC_REG,
                            PAC_IMAGEN = _DataPac1.PAC_IMAGEN,
                            //AL USAR EL METODO ACTUALIZAR AL CARGAR LOS DATOS EN LOS INPUT NO OLVIDAR QUE SE DEBE CARGAR
                            //NUEVAMENTE LOS DropDownList LLAMANDOLO NUEVAMENTE AL FINAL COMO SE VE EN EL CODIGO A CONTINUACION
                            Genero_Lista = _lPacienteGen.GetGenero(_context),
                            Comuna_Lista = _lComuna.GetComuna(_context),
                            Odontologo_Lista = _lOdontologo.GetOdontologo(_context)

                        };
                    if (_dataInput != null)
                    {
                        MODEL_PACIENTE.ErrorMessage = _dataInput.ErrorMessage;
                    }
                }
            }
        }
        else
        {
            var Cod_Pac = 0;
            String TempCodPac = null;
            var Ultimo_Paciente = (from t in _context.TBL_PACIENTE
                                   orderby t.PAC_CODIGO
                                   select t).LastOrDefault();
            if (Ultimo_Paciente != null)
            {
                Cod_Pac = (Ultimo_Paciente.PAC_CODIGO != null) ?
                       Convert.ToInt32(Ultimo_Paciente.PAC_CODIGO) + 1 :
                       1;
                if (Cod_Pac < 10)
                {
                    TempCodPac = String.Concat("0000", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac >= 10 && Cod_Pac <= 11)
                {
                    TempCodPac = String.Concat("000", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac > 10 && Cod_Pac < 100)
                {
                    TempCodPac = String.Concat("000", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac >= 100 && Cod_Pac <=101)
                {
                    TempCodPac = String.Concat("00", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac > 100 && Cod_Pac < 1000)
                {
                    TempCodPac = String.Concat("00", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac >= 1000 && Cod_Pac <= 1001)
                {
                    TempCodPac = String.Concat("0", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac > 1000 && Cod_Pac <= 9999)
                {
                    TempCodPac = String.Concat("0", Convert.ToString(Cod_Pac));
                }
                else if (Cod_Pac > 9999 && Cod_Pac < 99999)
                {
                    TempCodPac = String.Concat("", Convert.ToString(Cod_Pac));
                }
            }
            MODEL_PACIENTE = new PACIENTE
            {
                PAC_CODIGO = TempCodPac,
                Genero_Lista = _lPacienteGen.GetGenero(_context),
                Comuna_Lista = _lComuna.GetComuna(_context),
                Odontologo_Lista = _lOdontologo.GetOdontologo(_context),
                PAC_COD_ODONT = "00000"
            };
        }

        _DataPac2 = _DataPac1;
        _DataPac1 = null;
    }

The code of my method Save_Patient

    private async Task<bool> Guardar_Paciente_Async()
        {
            _dataInput = MODEL_PACIENTE;
            var valor = false;
            if (ModelState.IsValid)
            {
                var PacLista = _context.TBL_PACIENTE.Where(u => u.PAC_RUT.Equals(MODEL_PACIENTE.PAC_RUT)).ToList();
                if (PacLista.Count.Equals(0))
                {
                    var strategy = _context.Database.CreateExecutionStrategy();
                    await strategy.ExecuteAsync(async () =>
                    {
                        using (var transaction = _context.Database.BeginTransaction())
                        {
                            try
                            {
                                var imagenByte = await _lCargarImagen.ByteAvatarImageAsync(MODEL_PACIENTE.AvatarImage, _environment, "images/user_icon.png");
                                string TmpCodPac = _dataInput.PAC_COD_ODONT;
                                String vfCodPac = TmpCodPac.Substring(0, 4);
                                var Nuevo_Paciente = new MODELO_PACIENTE
                                {
                                    PAC_CODIGO = _dataInput.PAC_CODIGO,
                                    PAC_NOMBRE = _dataInput.PAC_NOMBRE.ToUpper(),
                                    PAC_APELLIDO = _dataInput.PAC_APELLIDO.ToUpper(),
                                    PAC_SEXO = _dataInput.PAC_SEXO,
                                    PAC_RUT = _dataInput.PAC_RUT,
                                    PAC_FECHA_NAC = _dataInput.PAC_FECHA_NAC,
                                    PAC_EDAD = _dataInput.PAC_EDAD,
                                    PAC_REPRESENTANTE = _dataInput.PAC_REPRESENTANTE?.ToUpper(),
                                    PAC_DIRECCION = _dataInput.PAC_DIRECCION?.ToUpper(),
                                    PAC_COMUNA = _dataInput.PAC_COMUNA?.ToUpper(),
                                    PAC_OTRAS_COMUNAS = _dataInput.PAC_OTRAS_COMUNAS?.ToUpper(),
                                    PAC_TELEFONO = _dataInput.PAC_TELEFONO,
                                    PAC_CORREO = _dataInput.PAC_CORREO,
                                    PAC_CONVENIO = _dataInput.PAC_CONVENIO?.ToUpper(),
                                    PAC_PREVISIONES = _dataInput.PAC_PREVISIONES?.ToUpper(),
                                    PAC_OBSERVACIONES = _dataInput.PAC_OBSERVACIONES?.ToUpper(),
                                    PAC_COD_ODONT = vfCodPac,
                                    PAC_IMAGEN = imagenByte,
                                    PAC_FEC_REG = DateTime.Now,
                                    PAC_FEC_ACT = DateTime.Now

                                };
                                await _context.AddAsync(Nuevo_Paciente);
                                _context.SaveChanges();
                                transaction.Commit();
                                _dataInput = null;
                                valor = true;
                            }
                            catch (Exception ex)
                            {
                                _dataInput.ErrorMessage = ex.Message;
                                transaction.Rollback();
                                valor = false;
                            }
                        }
                    });
                }
                else
                {
                    _dataInput.ErrorMessage = $"El RUT {MODEL_PACIENTE.PAC_RUT} ya se encuentra Registrado"; 
// It should show this error in the respective "input", but the error message you see below in the image appears
                }
            }
            else
            {
                foreach (var modelState in ModelState.Values)
                {
                    foreach (var error in modelState.Errors)
                    {
                        _dataInput.ErrorMessage += error.ErrorMessage;
                    }
                }
                valor = false;
            }
            return valor;
        }

If I debug, when I start it loads the 2 records from the dentist table, but when I put the existing ID at the time of appearing the error message appears what is seen in the image and when trying to load the list again it comes out null

enter image description here

After checking the ID error, the "Odontologia Lista" method returns null and the title error appears

enter image description here

enter image description here

where is the problem ?, I appreciate your help or guide to solve this problem.

2
  • Can you post _lOdontologo.GetOdontologo pls? Commented Sep 12, 2021 at 20:53
  • Ok, check again the question. Commented Sep 12, 2021 at 21:11

1 Answer 1

1

You can try to check the following code:

MODEL_PACIENTE = new PACIENTE
            {
                PAC_CODIGO = TempCodPac,
                //Genero_Lista = _lPacienteGen.GetGenero(_context),
                Comuna_Lista = _lComuna.GetComuna(_context),
                //Odontologo_Lista = _lOdontologo.GetOdontologo(_context),
                PAC_COD_ODONT = "00000"
            };

Odontologo_Lista = _lOdontologo.GetOdontologo(_context), is commented.If _dataInput == null && _DataPac1 == null && _DataPac2 == null,the Odontologo_Lista will not be set,and you will get the error Value cannot be null.

Sign up to request clarification or add additional context in comments.

5 Comments

Is the same error with lPacienteGen.GetGenero(_context) and _lOdontologo.GetOdontologo(_context)
Can you debug and check if Odontologo_Lista is null before return?
Exactly , the function return null, i upload a imagen the after and the before
but when I put the existing ID at the time of appearing the error message,What do you mean put the existing ID,idActPac
IdActPac is when I try to update the form in another instance, if the ID is 0 I am in registration mode, but if it is different from 0 I am in update mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.