Yan Susanto
Yan Susanto

Reputation: 353

How to add session from database?

I still new using Asp.net with vb.net

Protected Sub login_Click(ByVal sender As Object, ByVal e As EventArgs) Handles login.Click
        Dim querystrings As String = "Select Email, Password ,Roles, Nama_Depan, Nama_Belakang from Employee where Email = @email;"
        Dim con As New SqlConnection(connected)
        Dim cmd As New SqlCommand(querystrings, con)
        Dim myreader As SqlDataReader

   Session ("Namadepan")= cmd.Parameters("Nama_Depan")

end sub

I wanted to store Roles, Nama_Depan, Nama_Belakang with session but i don't know how

Upvotes: 0

Views: 643

Answers (5)

Yan Susanto
Yan Susanto

Reputation: 353

Session("username") = myreader("Nama_Depan")

Upvotes: 0

thmsn
thmsn

Reputation: 1996

You need to fetch the result of you SqlDataReader and store it in session.

After executing the sql command against your server myReader should contain the data you need. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71)

you can then fetch the data from the reader and store it in your session. You can make a specific "UserData" object and store that in session.

Session("UserData") = new UserData{}

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

Refer this: Save values in Session

It is as simple as:

Session("Namadepan") = cmd.Parameters["Nama_Depan"].Value

Upvotes: 0

David East
David East

Reputation: 32604

Make a class that handles this logic so Session() and Session.Add() isn't scattered throughout your website.

Public Shared ReadOnly Property Instance() As ClassStoredInSession
    Get
        'store the object in session if it is not already stored
        If Session("example") Is Nothing Then
            Session("example") = New ClassStoredInSession()
        End If

        'return the object stored in session
        Return DirectCast(Session("example"), ClassStoredInSession)
    End Get
End Property

Upvotes: 1

Ravi Jain
Ravi Jain

Reputation: 1482

Make a class namely UserSession which have it's properties as the data retrieved from the database and insert the instance of the class into Session object.

Pseudocode :

UserSession user = new UserSession(name , roles .... )
Session["userData"] = user;

Upvotes: 0

Related Questions