1

在登录页面中:

Session("FirstName") = txtUserName.Text
Response.Redirect("CusRecords.aspx")

在第二页:

lbl1.Text = Session("FirstName").ToString

我使用了该代码,但出现此错误:

Object reference not set to an instance of an object. 
that error refers to lbl1.Text = Session("FirstName").ToString

这是登录页面的完整代码:

Public Class _Default
Inherits System.Web.UI.Page
Public s As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Session.Clear()
    Session.Abandon()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
    con.Open()
    Dim userCount As Int32 = GetUserCount(txtUserName.Text)
    Dim paasCount As Int32 = GetPassCount(TxtPassword.Text)


    If userCount > 0 And paasCount > 0 Then
        Session("FirstName") = txtUserName.Text
    Else
        lblErr.Visible = True
    End If
    Response.Redirect("CusRecords.aspx")
End Sub



' Method to check existence 
Public Shared Function GetUserCount(ByVal userName As String) As Int32
    Const sql = "SELECT COUNT(*) FROM Registration where username = @UserName"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql, con)
            cmd.Parameters.AddWithValue("@UserName", userName)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function



Public Shared Function GetPassCount(ByVal password As String) As Int32
    Const sql = "SELECT COUNT(*) FROM Registration where password = @Password"
    Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        Using cmd = New SqlCommand(sql, con)
            cmd.Parameters.AddWithValue("@Password", password)
            con.Open()
            Using reader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim count As Int32 = reader.GetInt32(0)
                    Return count
                End If
            End Using
        End Using
    End Using
End Function





Protected Sub txtUserName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtUserName.TextChanged

End Sub
End Class

其他页面:

Public Class CusRecords
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Session("FirstName") IsNot Nothing Then
        lbl1.Text = DirectCast(Session("FirstName"), String)
    Else
        lbl1.Text = "It is empty"
    End If


End Sub

结束类

4

3 回答 3

1

首先确保会话不为空

If Session("FirstName") IsNot Nothing Then
    lbl1.Text = DirectCast(Session("FirstName"), String)
End If

如果错误仍然存​​在,请确保 lbl1 不为空

另外,请注意这一行:

If userCount > 0 And paasCount > 0 Then
    Session("FirstName") = txtUserName.Text
Else
    lblErr.Visible = True
End If
Response.Redirect("CusRecords.aspx")

因此,即使您没有设置 Session("FirstName") 变量,您也总是会重定向- 这可能就是它为 null 的原因

编辑:
最后的问题是调用 Page_Load 中的 Session.Clear() 和 Session.Abandon()

于 2012-12-20T15:41:39.400 回答
0

我通过删除这一行来修复它

Session.Abandon()
于 2012-12-20T15:58:18.803 回答
0

试试这个 lbl1.Text = Session("FirstName") 不要输入 ToString()。

于 2013-06-25T12:06:40.683 回答