My registry is not updated with ConfigurationManager

0

For days now I am trying to do a update but nothing that updates the record I want.

I have already tried two ways and none of them has worked for me. I do not know if I have a syntax error or what the problem might be.

This is the code:

// UPDATE CLIENT
protected void updatecliente_Click(object sender, EventArgs e)
{
    try
    {
        string s = ConfigurationManager.ConnectionStrings["cadenaconexion1"].ConnectionString;
        string query = "update cliente set cedula = @cedula, nombre = @nombre, telefono = @telefono, direccion = @direccion where cedula = @cedulaWhere and nombre = @nombreWhere and telefono = @telefonoWhere and direccion = @direccionWhere";
        SqlConnection conexion = new SqlConnection(s);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = query;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = conexion;




        //Vamos a agregar los valores como parámetros para evitar la concatenación en la consulta.
        //Valores con los que se van a actualizar los campos
        cmd.Parameters.AddWithValue("@cedula", ccliente.Text);
        cmd.Parameters.AddWithValue("@nombre", ncliente.Text);
        cmd.Parameters.AddWithValue("@telefono", ctelefono.Text);
        cmd.Parameters.AddWithValue("@direccion", cdireccion.Text);
        //Valores para las condiciones.
        cmd.Parameters.AddWithValue("@cedulaWhere", "'123456789'");
        cmd.Parameters.AddWithValue("@nombreWhere", "'Luis Ocampo'");
        cmd.Parameters.AddWithValue("@telefonoWhere", "'3116085275'");
        cmd.Parameters.AddWithValue("@direccionWhere", "'sddferr'");

        int result = cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        // aquí puedes manejar las excepciones
    }
}

And here's the code of how I started doing it:

string s = System.Configuration.ConfigurationManager.ConnectionStrings["cadenaconexion1"].ConnectionString;
            SqlConnection conexion = new SqlConnection(s);
            conexion.Open();
            SqlCommand comando = new SqlCommand("update cliente set " + 
                                 "cedula = '" + ccliente.Text + "', nombre = '" +
                                 ncliente.Text + "', telefono = '" + ctelefono.Text +
                                 "', direccion = '" + cdireccion.Text + "' where cedula = '" + 
                                 ccliente.Text + "' and nombre = '" +
                                 ncliente.Text + "' and telefono = '" + 
                                 ctelefono.Text + "' and direccion = '" + 
                                 cdireccion.Text + "'", conexion);

            ccliente.Text = string.Empty;
            ncliente.Text = string.Empty;
            ctelefono.Text = string.Empty;
            cdireccion.Text = string.Empty;

            msgerror.Text = "Información actualizada con exito";

            conexion.Close();

    
asked by Edward Sc 07.11.2018 в 18:36
source

1 answer

0

I think there may be 2 problems, the first is that the WHERE must be done by a primary key and not by all the data in the table, in your case you have the hardcoded values and in the table capture, no records appear with those values, then you never find which row to update.

Try as follows: (note that I changed the where to just the cedula)

// UPDATE CLIENT
        protected void updatecliente_Click(object sender, EventArgs e)
        {
            //para probar, pero en realidad se deberia tomar de una consulta anterior a seleccionar el usuario para actualizar
            string cedula = "0000";

            string query = "update cliente set cedula = @cedula, nombre = @nombre, telefono = @telefono, direccion = @direccion where cedula = @cedulaWhere";

            string s = ConfigurationManager.ConnectionStrings["cadenaconexion1"].ConnectionString;
            SqlConnection conexion = new SqlConnection(s);
            try
            {                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Connection = conexion;

                //Vamos a agregar los valores como parámetros para evitar la concatenación en la consulta.
                //Valores con los que se van a actualizar los campos
                cmd.Parameters.AddWithValue("@cedula", ccliente.Text);
                cmd.Parameters.AddWithValue("@nombre", ncliente.Text);
                cmd.Parameters.AddWithValue("@telefono", ctelefono.Text);
                cmd.Parameters.AddWithValue("@direccion", cdireccion.Text);
                //Valores para las condiciones.
                cmd.Parameters.AddWithValue("@cedulaWhere", cedula);

                conexion.Open();
                int result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                // aquí puedes manejar las excepciones
            }
            finally
            {
                conexion.Close();
            }
        }

Greetings!

    
answered by 07.11.2018 в 19:12