找不到密码或用户名时,无法将对象从 DBNull 转换为其他类型

Object cannot be cast from DBNull to other types when password or username not found

我正在创建一个双重登录表单,该表单根据用户的角色对用户进行身份验证,并根据用户名和密码将他们重定向到特定页面。我最初使用 FormsAuthentication.RedirectFromLoginPage 来处理重定向,但我发现我只能在使用

成功登录后重定向到一个页面
<forms defaultUrl="~/InteriorStudentPortal.aspx" loginUrl="~/ExteriorLogin.aspx" slidingExpiration="true" timeout="2880"></forms>

我的 web.config 中的身份验证。相反,我在登录页面的后台代码中使用了一个简单的 Response.Redirect("~/InteriorStudentPortal.aspx") 属性。现在,当用户输入不正确的用户名或密码时,我会收到通常由 FormsAuthentication 处理的 Object cannot be cast from DBNull to other types 错误。我可以在后台代码或存储过程中添加什么来处理此异常?

这是我的存储过程之一:

CREATE PROCEDURE [dbo].[Validate_Student]

      @Username NVARCHAR(50),

      @Password NVARCHAR(50)

AS

BEGIN

      SET NOCOUNT ON;

      DECLARE @StudentID INT, @LastLoginDate DATETIME



      SELECT @StudentID = StudentID, @LastLoginDate = LastLoginDate 

      FROM Student WHERE [Username] = @Username AND [Password] = @Password



      IF @Username IS NOT NULL

      BEGIN

        UPDATE Student

        SET LastLoginDate = GETDATE()

        WHERE StudentID = @StudentID

        SELECT @StudentID [StudentID] -- User Valid

      END

      BEGIN

            SELECT -1 -- User invalid.

      END

END

连同后台代码:

Protected Sub ValidateStudent(sender As Object, e As AuthenticateEventArgs) Handles Login1.Authenticate
        Dim studentId As Integer = 0
        Dim constr As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString

            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("Validate_Student")
                    cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@Username", Login1.UserName)
                    cmd.Parameters.AddWithValue("@Password", Login1.Password)
                    cmd.Connection = con
                    con.Open()
                studentId = Convert.ToInt32(cmd.ExecuteScalar())
                    con.Close()
                End Using
            Select Case studentId
                Case -1
                    Login1.FailureText = "Username and/or password is incorrect."
                    Exit Select
                Case Else
                    Response.Redirect("~/InteriorStudentPortal.aspx")
                    Exit Select
            End Select
            End Using
    End Sub

错误发生在 studentId = Convert.ToInt32(cmd.ExecuteScalar()) 行。任何帮助将不胜感激,我很乐意提供所需的任何其他信息。

您可以像下面这样检查 DBNull

studentId = If(IsDBNull(cmd.ExecuteScalar()), 0, Convert.ToInt32(cmd.ExecuteScalar()))

并修改

Select Case studentId
       Case -1
         Login1.FailureText = "Username and/or password is incorrect."
       Exit Select
       Case 0
         Login1.FailureText = "Username and/or password is incorrect."
       Exit Select
       Case Else
         Response.Redirect("~/InteriorStudentPortal.aspx")
       Exit Select
End Select

或者您可以使用 IF ELSE 而不是 CASE

If studentId > 0 Then
    Response.Redirect("~/InteriorStudentPortal.aspx")
Else
    Login1.FailureText = "Username and/or password is incorrect."
End If