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. Database & SysAdmin
  3. Database
  4. Why I can't search SQL database with WHERE with a text variable?

Why I can't search SQL database with WHERE with a text variable?

Scheduled Pinned Locked Moved Database
databasecsharphelpquestion
5 Posts 2 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.
  • C Offline
    C Offline
    Curious 2009
    wrote on last edited by
    #1

    I am using VB.net and I have a SQL database table GuestInfo with 1 column Col1 defined as Text with allow NULL & another column Col2 defined as Int with allow NULL If I use command:

    cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col2 = 12345"
    Dim lrd As SqlDataReader = cmd.ExecuteReader()

    - There is no error happen But if I use:

    cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = 'ABC'"
    Dim lrd As SqlDataReader = cmd.ExecuteReader()

    - There is an error pop-up said that text and varchar are incompatible operator Then I tried the following:

    Dim str As String

    str = "ABC"

    cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = '" & str & "'"
    Dim lrd As SqlDataReader = cmd.ExecuteReader()

    - There is a same error display What can I do to search a Text value in column 1?

    M 1 Reply Last reply
    0
    • C Curious 2009

      I am using VB.net and I have a SQL database table GuestInfo with 1 column Col1 defined as Text with allow NULL & another column Col2 defined as Int with allow NULL If I use command:

      cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col2 = 12345"
      Dim lrd As SqlDataReader = cmd.ExecuteReader()

      - There is no error happen But if I use:

      cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = 'ABC'"
      Dim lrd As SqlDataReader = cmd.ExecuteReader()

      - There is an error pop-up said that text and varchar are incompatible operator Then I tried the following:

      Dim str As String

      str = "ABC"

      cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = '" & str & "'"
      Dim lrd As SqlDataReader = cmd.ExecuteReader()

      - There is a same error display What can I do to search a Text value in column 1?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I rarely use TEXT data type so have not run across this problem but you might try

      cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = Convert(text,'ABC')"

      It is probable that the command object passes the string data type as varchar, as it is the default I think. Do some research on stored procedures and parameters, that will solve all these problems and move your sql development and testing to Management Studio which will speed up your delivery time.

      Never underestimate the power of human stupidity RAH

      C 1 Reply Last reply
      0
      • M Mycroft Holmes

        I rarely use TEXT data type so have not run across this problem but you might try

        cmd.CommandText = "SELECT * FROM GuestInfo WHERE Col1 = Convert(text,'ABC')"

        It is probable that the command object passes the string data type as varchar, as it is the default I think. Do some research on stored procedures and parameters, that will solve all these problems and move your sql development and testing to Management Studio which will speed up your delivery time.

        Never underestimate the power of human stupidity RAH

        C Offline
        C Offline
        Curious 2009
        wrote on last edited by
        #3

        I did try as you suggested but now it is weird, the error now shows: Connect to SQL Server, data types text and text are incompatible in the equal to operator - Might be in SQL database, I shouldn't defined Col1 as Text? If YES, what I should define to with Dim str as String in VB.net (str = "ABC"): . varchar(50) . varchar(MAX) . char(10) . nchar(10) . ntext Any suggestion? Meanwhile, I will also search for more solutions :doh:

        M 1 Reply Last reply
        0
        • C Curious 2009

          I did try as you suggested but now it is weird, the error now shows: Connect to SQL Server, data types text and text are incompatible in the equal to operator - Might be in SQL database, I shouldn't defined Col1 as Text? If YES, what I should define to with Dim str as String in VB.net (str = "ABC"): . varchar(50) . varchar(MAX) . char(10) . nchar(10) . ntext Any suggestion? Meanwhile, I will also search for more solutions :doh:

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          The default of nvarchar is for unicode and should not be used unless you intend to store non english characters. I would recommend using varchar(##), the length should be slightly larger than the max characters you expect. varchar can be used for up to 8000 characters, this generally meets most requirements. varchar(max) I would only use if you need to store a serious amount of text in a field, ie. a book or at least a story.

          Never underestimate the power of human stupidity RAH

          C 1 Reply Last reply
          0
          • M Mycroft Holmes

            The default of nvarchar is for unicode and should not be used unless you intend to store non english characters. I would recommend using varchar(##), the length should be slightly larger than the max characters you expect. varchar can be used for up to 8000 characters, this generally meets most requirements. varchar(max) I would only use if you need to store a serious amount of text in a field, ie. a book or at least a story.

            Never underestimate the power of human stupidity RAH

            C Offline
            C Offline
            Curious 2009
            wrote on last edited by
            #5

            I re-define Col1 to varchar(50) then use: cmd3.CommandText = "SELECT * FROM GuestInfo WHERE Trial = 'ABC' " Dim lrd As SqlDataReader = cmd3.ExecuteReader() Working as a charm! Thanks :) :) :)

            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