views:

17

answers:

1

I'm am not quite sure why I am getting this error.

Dim numUsers as Integer
Using myConnection as New System.Data.SqlClient.SqlConnection("Data Source=(local);InitialCatalog=dbtest;Integrated Security=True")
   Dim queryString As String = "SELECT COUNT(*) AS Num_Of_User FROM tblusers WHERE username=@username AND password=@password"
   Using myCommand as New System.Data.SqlClient.SqlCommand(queryString, myConnection)
      myConnection.Open
      myCommand.Parameters.AddWithValue("@username", requestName)
      myCommand.Parameters.AddWithValue("@password", requestPass) 
      numUsers = myCommand.ExecuteScalar()
   End Using
End Using 

This error occurs on the first using statment. Can anyone help resolve this?

A: 

the variable myConnection is declared at a higher level of scope above the Using statement. The Using statement is trying to create myConnection with the scope of the Using block but that would conflict with myConnection which has scope above that.

DaveWilliamson