How can I fix the vb.net error "array bounds cannot appear in type specifiers" in the following code?
Dim con As New SqlConnection("Data Source=localhost;Initial Catalog=WebAssignment1;Integrated Security=True")
con.Open()
Dim command As SqlCommand ("Insert into List_Assignment values ('"& tid &"','"& task &"','"& customer &"','"& mandays &"','"& startDate &"','"& addDate &"','"& estimateDate &"','"& status &"','"& completionDate &"','"& remark &"','"& lastUpdate &"')", con)
Command.ExecuteNonQuery()
MessageBox.Show("Successfully Insert.")
2
Answers
You wrote
instead of
therefore, the VB compiler thinks that
SqlCommand (x, y, z ..)
is an array type. When you add theNew
keyword, VB knows that you are calling a constructor with arguments.If you convert the string expression to string interpolation, the statement becomes more readable
But this is problematic anyway, because possible SQL Injection.
Another problem is that you are obviously inserting everything as text (because of the single quotes). Either all the columns are really text columns, then your database design is bad, because dates and number must be typed as such, or some columns are not text columns and your insert command is wrong.
See also: How to use parameters "@" in an SQL command in VB
Well, too bad you did not post the code as text – I would have cut + pasted more working code.
But, your code should be setup something like this:
Above is air code, since I can’t re-type all of your code in a picture – that would cause world poverty and take too much time.
However, the above code is "close" and a good working example of how you should and want to approach this.
So, as a general rule, in the SQL, you want to specify BOTH the columns, and then the values to insert.