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();