简体   繁体   English

如何使用vb.net在会话中存储用户ID

[英]How to store userid in session using vb.net

How to store user id in session so that I can use in other pages? 如何在会话中存储用户ID,以便可以在其他页面中使用?

My code is as follows 我的代码如下

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Web
Imports System.IO

Private Sub cmdlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    cnn.Open()

    Dim sqlcomm2 As New SqlCommand("Select * from Users where userid='" & txtuserid.Text & "'", cnn)
    Dim r2 As SqlDataReader = sqlcomm2.ExecuteReader()
    While r2.Read
        Dim UserId1 As String = Trim(UCase(CStr(r2("UserId"))))
        Dim Password As String = Trim(UCase(CStr(r2("Password"))))
        Dim username As String = CStr(r2("Name"))

        Session("User") = UserId1
        Session("Password") = Password

        'System.Web.HttpContext.Current.Session("item")

    End While
    r2.Close()

but its showing error as: 但其显示错误为:

session is not declared 会话未声明

You get this error when compiling your application: 您在编译应用程序时收到此错误:

Name 'session' is not declared ... 未声明名称“会话” ...

This generally occurs when using session variables in a code customization and your compiler is not able to find the session variable class. 通常在代码定制中使用会话变量并且编译器无法找到会话变量类时,会发生这种情况。

Specify the full path of the session variable. 指定会话变量的完整路径。 If you cannot access the Session class directly, you may need to specify its full path as follows: 如果您不能直接访问Session类,则可能需要如下指定其完整路径:

In VB.NET, to access a value: 在VB.NET中,访问值:

System.Web.HttpContext.Current.Session(“MyVariable”)
System.Web.HttpContext.Current.Session(“MyVariable”).ToString()

In VB.NET, to set a value: 在VB.NET中,要设置一个值:

System.Web.HttpContext.Current.Session(“MyVariable”) = “NewValue”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM