2013-08-21 31 views
0

如何在会话中存储用户标识以便我可以在其他页面中使用?如何使用vb.net在会话中存储用户标识

我的代码如下

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() 

但由于其示值误差:

会议没有宣布

回答

4

您编译应用程序时出现此错误:

名称'会话'未声明...

在代码自定义中使用会话变量时,通常会发生这种情况,并且编译器无法找到会话变量类。

指定会话变量的完整路径。如果您无法直接访问会话类,你可能需要指定它的完整路径如下:

在VB.NET,访问值:

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

在VB.NET中,设置值:

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

感谢您的建议。我试着给出的代码也显示HttpContext不是网络的成员。 – user1702346

+0

尝试通过“添加引用”手动引用System.Web。右键单击您的应用程序在解决方案资源管理器,然后选择添加引用,然后在.net选项卡中选择system.web .. – Sasidharan

+0

我检查参考system.web.httpcontext不存在 – user1702346

相关问题