Pablo Tobar Pablo Tobar - 6 months ago 20
C# Question

LIKE doesn't seem to work

I┬┤working on a C# desktop App and want to search in a MYSQL database all the records that match with a specific name in order to display this records into a datagridview but got the error when executing the query with the value: pablo

enter image description here

this is my query

sentencia = "select * from registro where nombreParticipante LIKE '%' + @valor + '%'";
nombre = valor.ToUpper();
cmd.Parameters.AddWithValue("@valor", nombre);
cmd.CommandText = sentencia;


could you tell me what is wrong with the code please

Answer Source

Format the string for your parameter rather than in the query:

sentencia = "select * from registro where nombreParticipante LIKE @valor";
                    nombre = valor.ToUpper();
                    cmd.Parameters.AddWithValue("@valor", "%" + nombre + "%");
                    cmd.CommandText = sentencia;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download