Skip to content
Advertisement

Taking same column but different values from database in C#

I am trying to get top 3 safety data row according to equipment and plant selection from database. Now I could get top 3 safety value from database with equipment and plant selection and insert to textbox.

When I wrote "' or Safety '" + textbox.text + it is getting other plant and equipment selections

Advertisement

Answer

Keep your sql been readable with a help of verbatim strings and string interpolation and many an error will be evident. Here you should either wrap Safety = ... or Safety = ... in parenthesis (Safety = ... or Safety = ... ) or use in construction Safety in (...).

Quick but dirty amendment is

However, this implementation has at least 3 flaws:

  1. It’s prone to SQL Injection
  2. It will crash on equipmenttype_combobox.Text = "Browns' equipment" (note apostroph)
  3. For different plants, you have different queries which should be parsed, optimized etc.

Much better aproach is parametrized query:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement