Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Inserting a field name into query

Inserting a field name into query

Scheduled Pinned Locked Moved Visual Basic
databasemysqlsql-servertutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nakins
    wrote on last edited by
    #1

    Hello, I have what I think is a simple task, but I have yet to find the answer. I'm trying to insert a table field into a query string from a dropdown list. I'm using MySql for a database, and if I use a control parameter, it inserts the selected value from the dropdown into the query with single quotes, like 'xxx'. MySql wants field name to be unquoted, like xxx. My query is like SELECT name, number, city FROM mytable WHERE xxx LIKE 'something';. And I can't figure out how to insert the xxx into the query without the quotes. I was wondering if any of you have ever done something like this where you entered a field name into a query string. I'm sure this is done somehow. I can't imagine not being able to do this. Any suggestion, and I mean ANY suggestion will be welcomed. How would you do it with MSsql or access?

    A 1 Reply Last reply
    0
    • N nakins

      Hello, I have what I think is a simple task, but I have yet to find the answer. I'm trying to insert a table field into a query string from a dropdown list. I'm using MySql for a database, and if I use a control parameter, it inserts the selected value from the dropdown into the query with single quotes, like 'xxx'. MySql wants field name to be unquoted, like xxx. My query is like SELECT name, number, city FROM mytable WHERE xxx LIKE 'something';. And I can't figure out how to insert the xxx into the query without the quotes. I was wondering if any of you have ever done something like this where you entered a field name into a query string. I'm sure this is done somehow. I can't imagine not being able to do this. Any suggestion, and I mean ANY suggestion will be welcomed. How would you do it with MSsql or access?

      A Offline
      A Offline
      aseef
      wrote on last edited by
      #2

      SELECT name,number,city FROM mytable WHERE xxx like '%" & str & "%'" Asif

      L 1 Reply Last reply
      0
      • A aseef

        SELECT name,number,city FROM mytable WHERE xxx like '%" & str & "%'" Asif

        L Offline
        L Offline
        Leather99
        wrote on last edited by
        #3

        Yes -as Asif says, without the quotes. Dim sFieldName as string = cmbField.Text Dim sSQL as string = "SELECT * FROM MyTable WHERE " & sFieldName & " = 'A Value'" Note that you need to be wary of SQL Injection attacks etc in this case... Can the user modify the combo text?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups