I have a simple program and I want to validate that some data exists before inserting it, that is, it throws a message telling me that this record exists.
The problem is that I insert data and it exists.
Debugging I realized that in the second if it should bring me in the variable with the same code
but it brings me the full query ie "SELECT * FROM ....."
ANNEX CODE
private void btnSave_Click(object sender, EventArgs e)
{
try
{
string codigo = txtCode.Text;
string nombre = txtName.Text;
string descripcion = txtDescription.Text;
double precio = double.Parse(txtPrice.Text);
int existencia = int.Parse(txtExistence.Text);
if (codigo != "" && nombre != "" && descripcion != "" && precio > 0 && existencia > 0)
{
string con = "SELECT * FROM PRODUCTO WHERE codigo = '" + codigo + "'";
string sql = "INSERT INTO PRODUCTO (codigo, nombre, descripcion, precio, existencia)" +
" VALUES ('" + codigo + "','" + nombre + "','" + descripcion + "','" + precio + "','" + existencia + "')";
SqlConnection conexionBD = Conexiona.conexion();
conexionBD.Open();
SqlCommand cmd = new SqlCommand(con, conexionBD);
cmd.Parameters.AddWithValue("codigo", codigo);
if (con == codigo)
{
MessageBox.Show("Registro Ya Existe");
}
else {
try
{
SqlCommand comando = new SqlCommand(sql, conexionBD);
comando.ExecuteNonQuery();
MessageBox.Show("Datos Guardado");
}
catch (SqlException ex)
{
MessageBox.Show("No Pudo Guardar Los Datos Debido A: " + ex.Message);
}
finally
{
conexionBD.Close();
}
}
clean();
}
else
{
MessageBox.Show("LLenar Cada Campo");
}
}
catch (FormatException fex)
{
MessageBox.Show("Datos Erroneo : " + fex.Message);
}
}
//Replace...
//By...