Declan Tiff Declan Tiff - 5 months ago 25
Vb.net Question

Overload resolution failed because no accessible 'ExecuteScalar' accepts this number of arguments

Im pretty new to VB.NET and was wondering how I'd fix this error im getting.

CODE

Dim sql As String = "SELECT (USER) FROM USER_CONSTANT"
Dim conn As New SqlConnection(strConn)
Dim objDR As SqlDataReader
Dim Cmd As New SqlCommand(sql, conn)
conn.Open()
objDR = Cmd.ExecuteScalar(CommandBehavior.CloseConnection)
While objDR.Read()
Label7.Text = objDR("USER")

Answer

The ExecuteScalar method doesn't expect any parameter. The CommandBehavior enum could be passed to the ExecuteReader method, so I suppose that your code should be written as

Dim sql As String = "SELECT (USER) FROM USER_CONSTANT"
Using conn = New SqlConnection(strConn)
Using Cmd = New SqlCommand(sql, conn)
    conn.Open()

    Using objDR = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
       While objDR.Read()
          Label7.Text = objDR("USER")
       End While
    End Using
 End Using
 End Using

It is not clear if you expect to receive more than one record from this code. In case of multiple records returned then note that at every loop you change the content of the Label7.Text with the current USER and your loop ends with the last name displayed