Results 1 to 5 of 5

Thread: [RESOLVED] SQL Works In Debug Doesn't Work In Release Mode

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Resolved [RESOLVED] SQL Works In Debug Doesn't Work In Release Mode

    Hi, I wrote a program that can read data from an SQL database and it works great while I'm in DEBUG mode. I tried to switch it to RELEASE mode to distribute it to other computers and it just doesn't work. The error messages, shown below, says that there is no data present. The code is also shown below. Line 139 is shown in red. I have no idea what I'm doing wrong since the program works in debug mode. Any help is appreciated.

    System.InvalidOperationException: Invalid attempt to read when no data is present.
    at System.Data.SqlClient.SqlDataReader.CheckDataIsReady(Int32 columnIndex, Boolean allowPartiallyReadColumn, Boolean permitAsync, String methodName)
    at System.Data.SqlClient.SqlDataReader.TryReadColumn(Int32 i, Boolean setTimeout, Boolean allowPartiallyReadColumn)
    at System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i)
    at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)
    at HC_05_Terminal.Form1.ReadData() in C:\Users\user\Desktop\VbNet COM3 Terminal For HC-05\HC-05 Terminal\Form1.vb:line 139
    Code:
        Private Sub btnSql_Click(sender As Object, e As EventArgs) Handles btnSql.Click
    
            ReadData()
    
        End Sub
    
    
        Public Sub ReadData()
            Dim connectionString As String = "Data Source=(localdb)\ProjectsV13;Initial Catalog=MySerialNumbersDb;Integrated Security=True;"
    
            Using connection As SqlConnection = New SqlConnection(connectionString)
                connection.Open()
                Dim sql As String = "SELECT * FROM Authors"
                Dim cmd As SqlCommand = New SqlCommand(sql, connection)
                Dim dreader As SqlDataReader = cmd.ExecuteReader()
    
                For IVar = 1 To My.Settings.ModulesNumber
                    dreader.Read()
                Next
    
                txbMessages.Text = "Module Number = " & dreader.GetValue(0) & vbCrLf & "Module Serial Number = " + dreader.GetValue(1) & vbCrLf & vbCrLf & txbMessages.Text
    
            End Using
    
        End Sub

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: SQL Works In Debug Doesn't Work In Release Mode

    This doesn't make sense:
    Code:
                For IVar = 1 To My.Settings.ModulesNumber
                    dreader.Read()
                Next
    You're looping to a number, and reading from the reader ... but not doing anything with the data... essentially skipping it. If your ModulesNumber exceeds the number of rows returned, that could also be an issue.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: SQL Works In Debug Doesn't Work In Release Mode

    Also, you may want to print out, or use a MessageBox to display what your ModulesNumber is before you start the loop. If it's 0, it'll never execute the loop, and never read from the reader... which would also be an issue.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: SQL Works In Debug Doesn't Work In Release Mode

    Quote Originally Posted by techgnome View Post
    This doesn't make sense:
    Code:
                For IVar = 1 To My.Settings.ModulesNumber
                    dreader.Read()
                Next
    You're looping to a number, and reading from the reader ... but not doing anything with the data... essentially skipping it. If your ModulesNumber exceeds the number of rows returned, that could also be an issue.

    -tg
    That part works in debug mode. The whole code works in debug mode. The variable ModulesNumber value is displayed in a textbox already. The reason I put that loop there is because I didn't know how to jump to a specific line in SQL, but that is not the issue now.
    Last edited by VB-MCU-User; May 9th, 2022 at 04:44 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: SQL Works In Debug Doesn't Work In Release Mode

    Quote Originally Posted by techgnome View Post
    Also, you may want to print out, or use a MessageBox to display what your ModulesNumber is before you start the loop. If it's 0, it'll never execute the loop, and never read from the reader... which would also be an issue.

    -tg
    Hmm, somehow the value of ModulesNumber in debug mode was 189 and in release mode was 0. Thanks, you found the 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