Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Declaration error

I am having some trouble in some Visual Basic code where although I have declared a variable, when I try to give it a value, Visual Studio returns an error saying that the variable hasn't been declared. Here is the block of code:

Private Sub chkbox_ta_CheckedChanged(sender As Object, e As EventArgs) Handles chkbox_ta.CheckedChanged
    Dim query As String = "SELECT * FROM [Hiragana List] WHERE Pronunciation='Ta';"
    Dim instruction As SqlCommand (query, connection)
    Dim da As New SqlDataAdapter
    da.SelectCommand = instruction
    da.Fill(HiraganaList)
End Sub

The error is thrown up by the 'instruction' variable and Visual Studio hasn't provided any solutions. In addition to this, the query argument within the instruction variable returns the error 'Array bounds cannot appear in type specifiers'. I am still getting used to working with SQL in VB and any explanation which would teach me how to avoid these errors would be very helpful.

like image 905
Marcus Eagle Avatar asked May 07 '26 04:05

Marcus Eagle


1 Answers

Wrong syntax in declaration and initialization of the SqlCommand.
The right syntax is one of the following:

Dim instruction As SqlCommand = new SqlCommand(query, connection)

or

Dim instruction As New SqlCommand (query, connection)

or just

Dim instruction = new SqlCommand(query, connection)

The Dim Statement has numerous variations the should be studied carefully (especially in the early days with the language)

like image 100
Steve Avatar answered May 09 '26 19:05

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!