Keep data in update aspx c #

0

Dear, I am doing a CRUD with entityframework in aspx c #, when consulting about the user to edit I show the data in several textbox. What I want to do is that if the user deletes a data (in the textbox) and updates the user, in the database the data already inserted is kept, but what I have so far is that it is stored in the sqlserver null Insert code

public int updateUsuario(string rut, string nombre, string apellido, string contrasena, string telefono, int cargo, string correo, int tienda)
{
    //Consulto si los datos ingresados son null, y en teoría, asigno los valores devueltos por el método select (SELECT * from usuario)
    if(nombre==null)
    {
        nombre = select(rut).nombre;
    }
    if(apellido==null)
    {
        apellido = select(rut).apellido;
    }
    if(contrasena==null)
    {
        contrasena = select(rut).contrasena;
    }
    if(telefono==null)
    {
        telefono = select(rut).telefono;
    }
    if(correo==null)
    {
        correo = select(rut).correo;
    }



    byte[] enc = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(contrasena));
    StringBuilder cadena = new StringBuilder();
    for (int i = 0; i < enc.Length; i++)
    {
        cadena.Append(enc[i].ToString("x2"));
    }
    string comp = cadena.ToString();
    int ret = 0;
    var seach = (from us in datos.usuario where us.rut == rut select us).Single();
    seach.nombre = nombre;
    seach.apellido = apellido;
    seach.contrasena = comp;
    seach.telefono = telefono;
    seach.cargo = cargo;
    seach.correo = correo;
    seach.tienda = tienda;
    ret = datos.SaveChanges();
    return ret;
}
    
asked by Claudio Scabbia Sepúlveda 25.08.2017 в 20:36
source

1 answer

1

Then do not unconditionally replace the value, but verify before each field is empty or null before replacing it

public int updateUsuario(string rut, string nombre, string apellido, string contrasena, string telefono, int cargo, string correo, int tienda)
{
    byte[] enc = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(contrasena));
    StringBuilder cadena = new StringBuilder();
    for (int i = 0; i < enc.Length; i++)
    {
        cadena.Append(enc[i].ToString("x2"));
    }
    string comp = cadena.ToString();
    int ret = 0;
    var seach = (from us in datos.usuario where us.rut == rut select us).Single();

    if (!String.IsNullOrEmpty(nombre)) {
        seach.nombre = nombre;
    }

    if (!String.IsNullOrEmpty(apellido)) {
        seach.apellido = apellido;
    }

    if (!String.IsNullOrEmpty(comp)) {
        seach.contrasena = comp;
    }

    if (!String.IsNullOrEmpty(telefono)) {
        seach.telefono = telefono;
    }

    if (!String.IsNullOrEmpty(cargo)) {
        seach.cargo = cargo;
    }

    if (!String.IsNullOrEmpty(correo)) {        
        seach.correo = correo;
    }        

    if (!String.IsNullOrEmpty(tienda)) {
        seach.tienda = tienda;
    }

    ret = datos.SaveChanges();
    return ret;
}
    
answered by 25.08.2017 / 20:49
source