语句在命名空间中无效

时间:2015-06-13 17:01:05

标签: vb.net asp.net-membership

以下代码返回Statement在使用VS-2010的命名空间中无效。请查看代码并推荐任何合理的修复,研究表明我应该通过导入主要命名空间来涵盖基础。

我还应该做些什么才能使其有效?

这些评论的其余部分仅仅是为了超越授权的冗长。

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 个答案:

答案 0 :(得分:5)

您的函数需要包含在类或模块中。

Imports System

Class MyFunctions

    Dim MyVariable

    Function MyFunction() 
    End Function

End Class

来自MSDN上的Statement is not valid in a Namespace

  

该语句不能出现在命名空间的级别。命名空间级别允许的唯一声明是模块,接口,类,委托,枚举和结构声明。

     

要更正此错误:

     

将语句移动到模块,类,接口,结构,枚举或委托定义中的位置。

答案 1 :(得分:2)

在VB.NET中,函数必须驻留在类或模块中。

因此在导入之后和第一个函数之前,定义一个类:

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

有关VB中面向对象编程的更多信息,请查看my blog