sqlParameters Array in VB.Net

Fabio Belz picture Fabio Belz · Apr 17, 2013 · Viewed 11.6k times · Source

I'm trying to create a typed-sized parameters array in VB.Net:

Dim parameters() As SqlParameter = New SqlParameter() _
        {
          New SqlParameter("@first_name", SqlDbType.VarChar, 50) {Value = "john"},
          New SqlParameter("@last_name", SqlDbType.VarChar, 50) {Value = "doe"},
          New SqlParameter("@age", SqlDbType.Int) {Value = 18},
          New SqlParameter("@id", SqlDbType.Int) {Value = 123}
        }

But VS says: Value' is not declared. It may be inaccessible due to its protection level

What's wrong with the code above?

Thanks!

Answer

Kenneth picture Kenneth · Apr 17, 2013

You need to use the VB syntax for object initializers:

Dim parameters() As SqlParameter = New SqlParameter() _
        {
          New SqlParameter("@first_name", SqlDbType.VarChar, 50) With { .Value = "john"},
          New SqlParameter("@last_name", SqlDbType.VarChar, 50) With { .Value = "doe"},
          New SqlParameter("@age", SqlDbType.Int) With { .Value = 18},
          New SqlParameter("@id", SqlDbType.Int) With { .Value = 123}
        }