Trying to input a field variable in paramaterized query

  • Hello,

    I am relatively new to asp.net. I have a search form that I am trying to enable the user to enter search terms in text box and then select the fieldname with a dropdown. I have been able to accompish this in class asp but I have had no luck so far. This is the code I am using

    strSelect = "Select * From DocTable1 Where @fieldname = @text"

     

     

  • Try

    strSelect = "Select * From DocTable1 Where " & @fieldname & " = @text"

  • Ray,

    Thanks for responding. I am still having no luck. Can table and field names be set as variables in a select statement in asp.net? When I try to set the field name as a variable there isn't an error, just no records are returned. When I take out the variable and just specify a field name in the select statement the search returns results. I am very frustrated. I can get my application to work in asp but I wanted to be able to use declared variables. I tried the code below and I am still have no luck.

     

    strSelect = "Select * From DocTable1 Where @fieldname like @text"

      cmdSelect = New SqlCommand( strSelect, conPubs )

     

       

       cmdSelect.Parameters.Add("@text", "%" & txtLastname.text & "%")

       cmdSelect.Parameters.Add("@fieldname", "%" & txtDropname.text & "%")

  • Jim - if I was in your shoes I would take the easy way out and create a stored procedure that'd execute various queries based on table & field names - erland sommarskog however, has some excellent articles on use of dynamic sql that should definitely point you in the right direction...

    Dynamic search conditions in t-sql

    the curse & blessings of dynamic sql







    **ASCII stupid question, get a stupid ANSI !!!**

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply