Can't find where my mistake is

jagrenet

New member
Joined
Nov 8, 2024
Messages
3
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.
 
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.
 
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.
 
Back
Top