Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 27th, 2011, 09:05 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default sql select values to session state

Hi Guys

Wondering if anyone has any experience of working with session states; specifically In Process mode sessions? I have values returned from an sql select statement that I want to store in a session state but can't figure out the correct syntax?

Everything I have found on the web relates to SQL session mode but can't find anything on what I want it to do?

Any help will be really appreciated.

Phil
Reply With Quote
  #2 (permalink)  
Old February 27th, 2011, 02:24 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Just assign them to the Session object using a key:

Session["KeyName"] = yourObject;

http://msdn.microsoft.com/en-us/library/ms972429.aspx

Storing data in session state does not depend on the configured session mode; in all cases the code is the same.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old February 27th, 2011, 02:34 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default Session State

Hi Imar

Thanks for the response, my problem is that I don't have a defined object to store as the session variable. I want the returned SQL query values to be stored in the session, as highlighted in red below, unfortuantely everywhere I have looked has suggested this cannot be done?

It would be simple as you suggest in your example if I have object to take the values from such as textbox, etc.

Code:
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)

        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim cmdString As String = "SELECT [UserID], [UserName], [Password], [RoleType] FROM [Users], [Roles] WHERE" & " (([UserName]=@UserName) AND ([Password] = @Password) AND (Users.RoleID = Roles.RoleID))"

        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("LiteratureCataloguingConnectionString").ConnectionString)
        cmd = New SqlCommand(cmdString, conn)

        cmd.Parameters.Add("UserName", SqlDbType.NVarChar, 60)
        cmd.Parameters("UserName").Value = Login1.UserName
        cmd.Parameters.Add("Password", SqlDbType.NVarChar, 50)
        cmd.Parameters("Password").Value = Login1.Password

        conn.Open()

        Dim myReader As SqlDataReader
        myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        Session("UserID") = Response.Write("[UserID]")        If myReader.Read() Then 
            AddToSession()
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, True)
        Else
            Response.Write("Invalid Credentials")
        End If

    End Sub
Reply With Quote
  #4 (permalink)  
Old February 27th, 2011, 02:51 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

A SqlDataReader is connected to the database, so you can't store that object in Session state directly.

So, you need to decide how you want the data stored. You could store simple values:

Session("SomeKey") = reader.GetString(reader.GetOrdinal("ColumnName"))

Alternatively, you could create a custom class to hold the data and store an instance of that class in Session. E.g.:'

Code:
 
Public Class Person
  Public Property Id As Integer
  Public Property Name As String
End Class
And then in code you can do something like this:

Code:
 
Dim myPerson As New Person()
myPerson.Id = reader.GetInt32(reader.GetOrdinal("SomeColumnWithAnInt"))
myPerson.Name = reader.GetString(reader.GetOrdinal("SomeColumnWithAString"))
Session("SomeKey") = myPerson
Hope tis helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Phil Critchley (February 27th, 2011)
  #5 (permalink)  
Old March 1st, 2011, 11:55 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default Custom Role Association

Hi Imar

Thanks for the post the other day it really helped point me in the right direction.

Does anyone have any experience with custom role association? My code below gets the username and role from my own sql database and stores the values in a session state. The problem I have with my code is that I don't think it is passing the string role value from my retrieved session state to the instance of IPrincipal I have incorporated into my code. The website I have sourced this from (shown below) states the role (indicated in red below) should be specified as an array group, but because I only have one role associated to a each user I have stored this in a string value.

I think the syntax I have used "({RoleType})" is incorrect for this? Does anyone have any experience of using the IPrincipal procedure and if so has anyone else used just one associated user role instead of an array of many?

http://www.4guysfromrolla.com/webtech/121901-1.2.shtml

Code:
Partial Class Login
    Inherits System.Web.UI.Page

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)

        Dim myPerson As New Person
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim cmdString As String = "SELECT UserID, UserName, Password, RoleType FROM Users, Roles WHERE Users.RoleID = Roles.RoleID AND UserName=@UserName AND Password = @Password"

        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("LiteratureCataloguingConnectionString").ConnectionString)
        cmd = New SqlCommand(cmdString, conn)

        cmd.Parameters.Add("UserName", SqlDbType.NVarChar, 60)
        cmd.Parameters("UserName").Value = Login1.UserName
        cmd.Parameters.Add("Password", SqlDbType.NVarChar, 50)
        cmd.Parameters("Password").Value = Login1.Password

        conn.Open()

        Dim myReader As SqlDataReader = cmd.ExecuteReader()

        While myReader.Read
            myPerson.UserID = myReader.GetInt32(0)
            myPerson.UserName = myReader.GetString(1)
            myPerson.RoleType = myReader.GetString(3)
            System.Web.HttpContext.Current.Session("User") = myPerson
            AssociateRole()
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, True)
        End While

        myReader.Close()

    End Sub

    Protected Sub AssociateRole()

        Dim loginStatusPerson As Person = System.Web.HttpContext.Current.Session("User")
        Dim RoleType As String
        Dim UserName As String

        RoleType = loginStatusPerson.GetRoleType
        UserName = loginStatusPerson.GetUserName

        Dim objIdentity As GenericIdentity = New GenericIdentity(UserName)
        Dim objPrincipal As GenericPrincipal = New GenericPrincipal(objIdentity, ({RoleType}))        Thread.CurrentPrincipal = objPrincipal

    End Sub

End Class
Reply With Quote
  #6 (permalink)  
Old March 2nd, 2011, 07:40 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Have yoy tried new[] { Roleype } ?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session state prisma Classic ASP Basics 3 March 16th, 2010 01:26 PM
Using Session State with SQL Server Express 2005 toddw607 SQL Server 2005 1 December 18th, 2007 06:12 PM
SQL Server session state latency planoie ASP.NET 2.0 Professional 4 July 18th, 2007 09:47 AM
SQL Session State & Object Array jlrolin ASP.NET 2.0 Professional 4 June 20th, 2007 12:49 PM
Session State|View State|Do I have other options? rockon ASP.NET 1.x and 2.0 Application Design 2 October 5th, 2005 07:10 PM



All times are GMT -4. The time now is 07:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.