0

我有一个包含以下代码的母版页:

<script runat="server">



    Protected Sub Page_Load(sender As Object, e As EventArgs)

        If Session("userid") = Nothing Then
            txtLoginUser.Visible = True
            txtLoginPass.Visible = True

        Else


            Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
            Dim useridComm As String = "SELECT name, surname FROM users WHERE user_id=@userid"
            Dim sqlUserID As New SqlCommand

            conn.Open()

            Dim userid As String = Session("UserID")

            sqlUserID = New SqlCommand(useridComm, conn)
            sqlUserID.Parameters.AddWithValue("@userid", Convert.ToInt32(userid))
            Dim datareader As SqlDataReader = sqlUserID.ExecuteReader()

            datareader.Read()
            If datareader.HasRows Then



                userid = Session("UserID")

                lblLoggedIn.Text = "[Welcome, " + datareader("name").ToString() & " " & datareader("surname").ToString() + " ]"
                txtLoginUser.Visible = False
                txtLoginPass.Visible = False
                lblUsername.Visible = False
                lblRegister.Visible = False
                btnLogin.Visible = False
                lblUsername0.Visible = False


            End If
            datareader.Close()
            conn.Close()

        End If
    End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        Dim loginSQL As New SqlCommand
        Dim loginComm As String

        Dim CommonFunctions As New CommonFunctions()
        Dim dec_pass As String = CommonFunctions.EncryptPassword(txtLoginPass.Text.Trim)

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")


        loginComm = "SELECT user_id FROM users WHERE username=@username and password=@password"

        conn.Open()


        loginSQL = New SqlCommand(loginComm, conn)
        loginSQL.Parameters.AddWithValue("@username", txtLoginUser.Text.ToString)
        loginSQL.Parameters.AddWithValue("@password", dec_pass)
        Dim dr As SqlDataReader = loginSQL.ExecuteReader()
        dr.Read()


        If dr.HasRows Then
            Session("UserID") = dr("user_id")

        ElseIf dr.HasRows = False Then

            lblRegister.ForeColor = Drawing.Color.Red
            lblRegister.Text = "Incorrect Username/Password."
        End If


        dr.Close()
        conn.Close()

        Response.Redirect("Default.aspx")

    End Sub
</script>

在 Button1 上单击脚本应该使用 获取 user_iddatareader并创建 aSession("UserID")并将其传递给Default.aspx. Default.aspx然后获取Session("UserID")并搜索user_id具有相同值的 a 并使用user_roles表检查角色,如果role_id是 4 则tblAdmin显示,否则不显示。

这是 Default.aspx 的代码:

Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page



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


        Dim UserID As Integer = Convert.ToInt32(Session("UserID"))

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim userTypeCommand As String = "SELECT role_id FROM users_role WHERE user_id=@UserID"
        Dim userTypeSQL As New SqlCommand

        conn.Open()

        Try

            userTypeSQL = New SqlCommand(userTypeCommand, conn)
            userTypeSQL.Parameters.AddWithValue("@UserID", UserID)

            Dim datareader As SqlDataReader = userTypeSQL.ExecuteReader

            If datareader("role_id").ToString = "4" Then

                tblAdmin.Enabled = True
                tblAdmin.Visible = True

            ElseIf datareader("role_id").ToString IsNot "4" Then

                tblAdmin.Visible = False

            End If



        Catch ex As Exception



        End Try


        conn.Close()


    End Sub

    Protected Sub btnCreateArticle_Click(sender As Object, e As EventArgs) Handles btnCreateArticle.Click


        Response.Redirect("addArticle.aspx")

    End Sub

    Protected Sub btnAdmin_Click(sender As Object, e As EventArgs) Handles btnAdmin.Click
        Response.Redirect("Admin.aspx")
    End Sub
End Class

当我调试时,当我按下“登录”按钮后,user_id (Session('UserID') 保持为 0,而我用来登录的用户的 user_id 在表中为“12”。

我究竟做错了什么?

我正在使用 ASP.NET/VB.NET 和 SQL Server 2012。

4

1 回答 1

0

解决它。有一个失踪datareader.Read()

于 2013-04-20T19:09:12.340 回答