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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With