skip to Main Content

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.")

enter image description here

2

Answers


  1. You wrote

    Dim command As SqlCommand (...)
    

    instead of

    Dim command As New SqlCommand (...)
    

    therefore, the VB compiler thinks that SqlCommand (x, y, z ..) is an array type. When you add the New 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

    Dim command As New SqlCommand($"Insert into List_Assignment values ('{tid}','{Task}','{customer}','{mandays}','{startDate}','{addDate}','{estimateDate}','{status}','{completionDate}','{remark}','{lastUpdate}')", con)
    

    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

    Login or Signup to reply.
  2. 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:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim status As String
        If RadioAktif.Checked = True Then
            status = "Aktif"
        Else
            status = "Non Aktif"
        End If
    
        Dim strSQL As String =
            "INSERT ListAssigmment (id, task, customer, mandays, Status)
            VALUES (@id, @task, @customer, @mandays, @Status)"
    
        Using conn As New SqlConnection(My.Settings.TEST4)
            Using cmdSQL As New SqlCommand(strSQL, conn)
    
                cmdSQL.Parameters.Add("@id", SqlDbType.Int).Value = 0 ' textCustID.Text
                cmdSQL.Parameters.Add("@task", SqlDbType.NVarChar).Value = TextTask.Text
                cmdSQL.Parameters.Add("@customer", SqlDbType.NVarChar).Value = TextCustomer.Text
                cmdSQL.Parameters.Add("@mandays", SqlDbType.NVarChar).Value = TextManDays.Text
                cmdSQL.Parameters.Add("@Status", SqlDbType.NVarChar).Value =  Status
    
                conn.Open()
                cmdSQL.ExecuteNonQuery()
    
            End Using
        End Using
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search