Results 1 to 8 of 8

Thread: query string not working right

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    query string not working right

    I've got this string:
    VB Code:
    1. SQLCMD.CommandText = "select usoc_code, usoc_description " & _
    2.      "from features " & _
    3.      "WHERE telephone_number = '" & RemoveHyphen(telephone) & "' " & _
    4.       "and (usoc_code = 'eamntc' or " & _
    5.       "usoc_code = 'ramntc' or " & _
    6.       "usoc_code = 'ranmntc' or " & _
    7.       "usoc_code = 'bamntc' or " & _
    8.       "usoc_code = 'baiw' or " & _
    9.       "usoc_code = 'banmntc') "

    I can run that in Query Analyzer and it works BUT when used in my app, the dataReader doesn't get populated meaning (to me) the query doesn't pass any of the 'or's' . One thing to note is that when I take out the or's, I get back data.

    is there something wrong with the syntax or is there a limitation to a datareader I don't know about?

    also, the data in the reader is going to the statement here:
    VB Code:
    1. reader = SQLCMD.ExecuteReader
    2.         reader.Read()
    3.  
    4.         While reader.Read
    5.  
    6.             MainForm.Ticketing.tab_0_lstMaintOptions.Items.Add(reader.Item("usoc_code") & " - " & reader.Item("usoc_description"))
    7.  
    8.         End While
    9.  
    10.         MainForm.Ticketing.tab_0_lstMaintOptions.Update()
    Last edited by Andy; May 5th, 2004 at 10:22 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I dont use .Net so there might be an issue with it

    You could always use the alternative SQL:
    VB Code:
    1. ..
    2. "WHERE telephone_number = '" & RemoveHyphen(telephone) & "' " & _
    3.       "and usoc_code IN ('eamntc', 'ramntc', 'ranmntc', 'bamntc', 'baiw', 'banmntc') "

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Probably nothing, but try taking out the extra space before the last quote.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I'd forgotten about the IN keyword. Thanks, that saves some typing! unfortunately, the same behavior is working.

    I just don't under stand why a simple SELECT statement works but by adding some filtering, it doesn't.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Originally posted by salvelinus
    Probably nothing, but try taking out the extra space before the last quote.
    nope that doesn't work either.

    I should note that the 'while' block gets skipped. it hits the "while' and then goes straight to 'end while' and never gets to the code inside of it.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I use oledb, so maybe it's different, but I don't loop through the reader that way. Here's an example of how I do it (a generic function to fill a listbox)
    VB Code:
    1. Public Shared Sub FillListBox(ByVal strSQL As String, ByVal lb As ListBox, ByVal strItem As String, _
    2.                             ByRef cn As OleDbConnection, ByRef cmd As OleDbCommand, ByRef dr As OleDbDataReader)
    3.         Try
    4.             cn.Open()
    5.             cmd.CommandText = strSQL
    6.             dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    7.             lb.Items.Clear()
    8.  
    9.             Do While dr.Read
    10.                 lb.Items.Add(dr(strItem))
    11.             Loop
    12.             dr.Close()
    13.  
    14.         Catch ex As Exception
    15.             MessageBox.Show(ex.Message & Environment.NewLine & "Closing program.", "Error Finding Modules", MessageBoxButtons.OK, MessageBoxIcon.Error)
    16.             Application.Exit()
    17.         Finally
    18.             If cn.State = ConnectionState.Open Then
    19.                 cn.Close()
    20.             End If
    21.         End Try
    22.     End Sub

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Here's something interesting:
    The below code works. But, if I uncomment the commented sections and comment out the other parts (code is below), it doesn't. There must be something about the while statment. BUT, i have another function that has a 'while' statement...setup EXACTLY like these and it works fine!

    VB Code:
    1. 'Dim item As String
    2.  
    3.             SQLCMD.CommandText = "Select usoc_code, usoc_description " & _
    4.                                                             "From Features " & _
    5.                                                             "Where (member_number = '" & RepairTicket.MemberNumber & "') and " & _
    6.                                                             "USOC_CODE in ('dslntv' ,'dsl1' ,'dsl3' ,'dsl5', 'dsl3em')"
    7.  
    8.             reader = SQLCMD.ExecuteReader
    9.             reader.Read()
    10.  
    11.             'While reader.Read
    12.             '    item = reader.Item("usoc_code") & " - " & reader.Item("usoc_description")
    13.             '    MainForm.Ticketing.tab_11_lstDSL.Items.Add(item)
    14.             'End While
    15.  
    16.             'DSL Plan
    17.             Try
    18.                 RepairTicket.DSLplan = reader.Item("usoc_code") & " - " & reader.Item("usoc_description")
    19.             Catch ex As Exception
    20.  
    21.             End Try
    22.             reader.Close()
    now, the one that DOESNT work:
    VB Code:
    1. Dim item As String
    2.  
    3.             SQLCMD.CommandText = "Select usoc_code, usoc_description " & _
    4.                                                             "From Features " & _
    5.                                                             "Where (member_number = '" & RepairTicket.MemberNumber & "') and " & _
    6.                                                             "USOC_CODE in ('dslntv' ,'dsl1' ,'dsl3' ,'dsl5', 'dsl3em')"
    7.  
    8.             reader = SQLCMD.ExecuteReader
    9.             reader.Read()
    10.  
    11.             While reader.Read
    12.                 item = reader.Item("usoc_code") & " - " & reader.Item("usoc_description")
    13.                 MainForm.Ticketing.tab_11_lstDSL.Items.Add(item)
    14.             End While
    15.  
    16.             ''DSL Plan
    17.             'Try
    18.             '    RepairTicket.DSLplan = reader.Item("usoc_code") & " - " & reader.Item("usoc_description")
    19.             'Catch ex As Exception
    20.  
    21.             End Try
    22.             reader.Close()

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    ok, I figured out what is the problem:

    right above my While statement, I had reader.read. this was causing a conflict, I suppose. I took out the reader.read and it works fine now

    whew!,, 5 hours working on that small problem!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width