-
May 9th, 2022, 04:20 PM
#1
Thread Starter
Addicted Member
[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
-
May 9th, 2022, 04:33 PM
#2
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
-
May 9th, 2022, 04:34 PM
#3
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
-
May 9th, 2022, 04:40 PM
#4
Thread Starter
Addicted Member
Re: SQL Works In Debug Doesn't Work In Release Mode
Originally Posted by techgnome
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.
-
May 9th, 2022, 04:47 PM
#5
Thread Starter
Addicted Member
Re: SQL Works In Debug Doesn't Work In Release Mode
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|