Statement is not valid in a namespace

11.4k views Asked by At

The code below returned Statement is not valid in a namespace using VS-2010. Please review the code and recommend any reasonable fix, Research indicates that I should have the bases covered by importing the presiding namespaces.

What more should I do to to make it valid?

The rest of these comments are only here to get past the mandate to be verbose.

Imports System
Imports System.Web
Imports System.Web.Security
' MembershipProvider.CreateUser
Public Overrides Function CreateUser(ByVal username As String, _
                                 ByVal password As String, _
                                 ByVal email As String, _
                                 ByVal passwordQuestion As String, _
                                 ByVal passwordAnswer As String, _
                                 ByVal isApproved As Boolean, _
                                 ByVal providerUserKey As Object, _
                                 ByRef status As MembershipCreateStatus) 
                      As MembershipUser
Return Me.CreateUser(username, password, email, _
                     passwordQuestion, passwordAnswer, _
                     isApproved, providerUserKey, False, "", status)
End Function ' 
' OdbcMembershipProvider.CreateUser -- returns OdbcMembershipUser 
' 
Public Overloads Function CreateUser(ByVal username As String, _
                                 ByVal password As String, _
                                 ByVal email As String, _
                                 ByVal passwordQuestion As String, _
                                 ByVal passwordAnswer As String, _
                                 ByVal isApproved As Boolean, _
                                 ByVal providerUserKey As Object, _
                                 ByVal isSubscriber As Boolean, _
                                 ByVal customerID As String, _
                                 ByRef status As MembershipCreateStatus) _
                      As OdbcMembershipUser
Dim Args As ValidatePasswordEventArgs = _
  New ValidatePasswordEventArgs(username, password, True)
OnValidatingPassword(Args)
If Args.Cancel Then
    status = MembershipCreateStatus.InvalidPassword
    Return Nothing 
End If 
If RequiresUniqueEmail AndAlso GetUserNameByEmail(email) <> "" Then
    status = MembershipCreateStatus.DuplicateEmail
    Return Nothing 
End If 
Dim u As MembershipUser = GetUser(username, False)
If u Is Nothing Then 
    Dim createDate As DateTime = DateTime.Now
    If providerUserKey Is Nothing Then
        providerUserKey = Guid.NewGuid()
    Else 
        If Not TypeOf providerUserKey Is Guid Then
            status = MembershipCreateStatus.InvalidProviderUserKey
            Return Nothing 
        End If 
    End If 
    Dim conn As OdbcConnection = New OdbcConnection(connectionString)
    Dim cmd As OdbcCommand = New OdbcCommand("INSERT INTO Users " & _
           " (PKID, Username, Password, Email, PasswordQuestion, " & _
           " PasswordAnswer, IsApproved," & _
           " Comment, CreationDate, LastPasswordChangedDate, LastActivityDate," & _
           " ApplicationName, IsLockedOut, LastLockedOutDate," & _
           " FailedPasswordAttemptCount, FailedPasswordAttemptWindowStart, " & _
           " FailedPasswordAnswerAttemptCount, FailedPasswordAnswerAttemptWindowStart, " & _
           " IsSubscriber, CustomerID)" & _
           " Values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn)

    cmd.Parameters.Add("@PKID", OdbcType.UniqueIdentifier).Value = providerUserKey
    cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username
    cmd.Parameters.Add("@Password", OdbcType.VarChar, 255).Value = EncodePassword(password)
    cmd.Parameters.Add("@Email", OdbcType.VarChar, 128).Value = email
    cmd.Parameters.Add("@PasswordQuestion", OdbcType.VarChar, 255).Value = passwordQuestion
    cmd.Parameters.Add("@PasswordAnswer", OdbcType.VarChar, 255).Value = EncodePassword(passwordAnswer)
    cmd.Parameters.Add("@IsApproved", OdbcType.Bit).Value = isApproved
    cmd.Parameters.Add("@Comment", OdbcType.VarChar, 255).Value = ""
    cmd.Parameters.Add("@CreationDate", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@LastPasswordChangedDate", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@LastActivityDate", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = pApplicationName
    cmd.Parameters.Add("@IsLockedOut", OdbcType.Bit).Value = False
    cmd.Parameters.Add("@LastLockedOutDate", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@FailedPasswordAttemptCount", OdbcType.Int).Value = 0
    cmd.Parameters.Add("@FailedPasswordAttemptWindowStart", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@FailedPasswordAnswerAttemptCount", OdbcType.Int).Value = 0
    cmd.Parameters.Add("@FailedPasswordAnswerAttemptWindowStart", OdbcType.DateTime).Value = createDate
    cmd.Parameters.Add("@IsSubscriber", OdbcType.Bit).Value = isSubscriber
    cmd.Parameters.Add("@CustomerID", OdbcType.VarChar, 128).Value = customerID
    Try
        conn.Open()
        Dim recAdded As Integer = cmd.ExecuteNonQuery()
        If recAdded > 0 Then
            status = MembershipCreateStatus.Success
        Else
            status = MembershipCreateStatus.UserRejected
        End If 
    Catch e As OdbcException
        If WriteExceptionsToEventLog Then
            WriteToEventLog(e, "CreateUser")
        End If
        status = MembershipCreateStatus.ProviderError
    Finally
        conn.Close()
    End Try 
    Return GetUser(username, False)
Else
    status = MembershipCreateStatus.DuplicateUserName
End If 
Return Nothing 
End Function
2

There are 2 answers

2
Justin Ryan On

Your functions need to be wrapped in a class or module.

Imports System

Class MyFunctions

    Dim MyVariable

    Function MyFunction() 
    End Function

End Class

From Statement is not valid in a Namespace on MSDN,

The statement cannot appear at the level of a namespace. The only declarations allowed at namespace level are module, interface, class, delegate, enumeration, and structure declarations.

To correct this error:

Move the statement to a location within a module, class, interface, structure, enumeration, or delegate definition.

1
DeborahK On

In VB.NET functions must reside within a class or a module.

So after the imports and before the first function, define a class:

<imports up here>
Public Class User
<all other code here>
End Class

For more information on object-oriented programming in VB, check out my blog.