Can't find where my mistake is

jagrenet

Member
Joined
Nov 8, 2024
Messages
6
Programming Experience
10+
Hello Gurus,

With Visual Basic and SQL, I am trying to populate a Chart using a connection to my SQL Database. Each time I run it I get this error "Conversion from string to type integer is not valid".
I do not see anywhere in my code where it is attempting to convert any of the data types which leads to my confusion.

Would somebody mind taking a look please ?? - All variables have been declared previously in the Public Class -
=======================================================================================
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

Try
If con.State = ConnectionState.Open Then
con.Close()

con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\my_name\Documents\Visual Studio 2012\Projects\Expense Management Solutions\Expense Management Solutions\VBConnectionFinal.mdf;Integrated Security=True"
End If
con.Open()

Dim Query As VariantType
Query = "select * from Table_1"
cmd = New SqlCommand(Query, con)
Dim dr As SqlClient.SqlDataReader
dr = cmd.ExecuteReader()

While dr.Read

Chart1.Series("NAME_vs_AGE").Points.AddXY(dr.GetString("name"), dr.GetInt32("age"))

End While
con.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)

Finally
con.Dispose()

End Try
End Sub
=================================================================================

Thanks in advance !!
jagrenet
 
Dim Query As VariantType
Query = "select * from Table_1"

VariantType is an Enum (Integer), you can't assign a string to that. Do use type inference where you declare and assign value in same statement:
VB.NET:
Dim query = "select * from Table_1"
If you mouse over query variable here you'll see compiler has it's type set as String.
 
Hi John,
Thanks for responding.

I changed the declaration to;

Dim Query As String, saved the Project and ran it again.

I am still getting the exact same error.
 
Last edited by a moderator:
The debugger will also show you exactly which line faults.

I will run the debugger again later this evening when I have more time. I will report back once I have done so. The first 2 or 3 times through it didn't isolate the error for me as it normally does.
Thanks John.
 
I will run the debugger again later this evening when I have more time. I will report back once I have done so. The first 2 or 3 times through it didn't isolate the error for me as it normally does.
Thanks John.

John,

The Debugger stalls at Private Sub Form1_Load. I can click into the development environment (while the debugger is still running) but, the debugger will not progress past the Form1_Load routine. So, I cannot get to the "Load_Chart" "Button6 Click Event in order to see what line the problem is on.

When I get the message box "Conversion from string to type integer is not valid" ... it is simply just that - a system initiated message box ....... it does not have the capability to select or click on "Debug", it only has an "Ok" button

. VB.NET Error.JPG
 
Last edited:
You can add the stack trace to the message to tell you which line caused the error

VB.NET:
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace)

You can of course place a breakpoint on this line
VB.NET:
If con.State = ConnectionState.Open Then
and then step through the code to ensure what you expect should be happening, is.
 
Thank you jdelano !!! - I will definitely try this. I had no idea we could do this. To be honest, this is my very first time using a "Try Catch" statement so I do appreciate the guidance.
 
Last edited by a moderator:
Back
Top