-4

Can anyone please explain why this is stuck in an infinite loop? I put this under a button and it worked fine. But when I put this in a class and instantiate the class, it gets stuck in an infinite loop. I assume it gets stuck in the loop because when I add breakpoints, it wont go further than inside the loop. It doesn't even get passed "End While", but if i put this code under a button click in ASP.NET it works just fine. What am I doing wrong?

Please don't criticize about "SQL injection" with the way I'm passing values to my database, because this isn't going to be a public thing and I'm well aware of that.

Imports System.Data
Imports System.Data.SqlClient


    'Step 1: Select the device ID
    Function SelectDevice(d As String, b As String, m As String, o As String) As String

        ' Try

        'Connect to database
        Dim xcitoDBConnection As SqlConnection
        xcitoDBConnection = New SqlConnection("Data Source=NOTDISPLAYED;" + "Initial Catalog=NOTDISPLAYED;" + "Integrated Security=True")
        Dim GetDeviceID As String = "SELECT DISTINCT(Identifier) FROM Support.Devices WHERE DeviceType='" & d & "' AND Brand = '" & b & "' AND Model = '" & m & "' AND OS = '" & o & "'"
        Dim command As SqlCommand = New SqlCommand(GetDeviceID, xcitoDBConnection)

        xcitoDBConnection.Open()
        'command.ExecuteScalar()

        Dim reader As SqlDataReader
        reader = command.ExecuteReader(CommandBehavior.CloseConnection)
        Dim GetID As String

        While reader.Read()
            GetID = reader("Identifier").ToString
            Return GetID
        End While

        xcitoDBConnection.Close()

        ' Catch ex As ApplicationException
        '  ex.Source = ("Selection Error")
        ' Throw ex

        ' Finally

        'End Try
    End Function
End Class
Blackwood
  • 4,504
  • 16
  • 32
  • 41
  • 2
    Aware of sql injection yet you still committed the sin. Consider yourself doubly [berated](http://stackoverflow.com/a/4018203/74015). – Sam Axe Aug 19 '15 at 01:03
  • I dont care if you berate or criticize my posts because i dont post here enough to care and most of the questions i've asked, i'm able to find on here... i've made the edits and removed the code like you asked. Thanks for helping. – TechGuyChris Aug 19 '15 at 01:15
  • Keep in mind that SO posts are for the Ages. A responsible Answerer will either not bother to post or will *have* to mention SQL injection in order to be complete and avoid DVs. If you only want one thing from the DBReader, why do you have it in a loop at all? Also (being responsible), you have some DBObjects not being disposed of... – Ňɏssa Pøngjǣrdenlarp Aug 19 '15 at 01:23
  • 1
    Replace While with IF and move Return to end of Function. – jdweng Aug 19 '15 at 01:32
  • 2
    Have you stepped through your code to verify it's in an infinite loop? Maybe your SQL just takes a long time? – Nick.Mc Aug 19 '15 at 01:33
  • Hey Plutonix good to hear from you i've seen your posts on other questions. That is because I attempted to do "GetID=reader("identifier").tostring and was greeted with errors. I even tried breaking it up to reader.read and then adding reader = read("identifier") . no success so I saw that loop on a different website. @Nick.McDermaid no i havent. jdweng I will give that a try. – TechGuyChris Aug 19 '15 at 01:35
  • 3
    Whenever you type the word **error**, your next task is to... _post the actual error_ – Nick.Mc Aug 19 '15 at 01:42
  • Nick, thank you. I will remember that next time. Jdweng was able to solve my issue. i simply need to use an if statement instead of a while. Also, when I had "return GetID" inside the loop, it would exit the function procedure due to return statement. I then placed the nex sub procedure inside my constructor and it is now working. Good job Jdweng. Much thanks!! :-) – TechGuyChris Aug 19 '15 at 01:58

1 Answers1

2

write

reader = command.ExecuteReader()

instead of

reader = command.ExecuteReader(CommandBehavior.CloseConnection)

as you closed the connection at before return. and return end of the function

follow this

Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
  Dim GetID As String

    Using connection As New SqlConnection(connectionString)
        connection.Open()

        Dim command As New SqlCommand(queryString, connection)
        Dim reader As SqlDataReader = command.ExecuteReader()
        While reader.Read()
            GetID = reader(0).ToString
        End While 
    End Using
    Return GetID 
End Sub
  • Excellent!!! Thank you so much!! looks like my return wasnt at the end. I noticed after posting here that "Return Get ID" caused the function to exit and it wouldnt execute anything beyond "Return GetID" including exiting the loop. This makes a lot of sense. thank you very much. – TechGuyChris Aug 19 '15 at 02:10