Thursday, March 22, 2012

Session Variables (Repost)

Hello,

A couple of days ago I had put out a post to find out how to determin the ID of the current record being inserted to my DB and copy that to a session variable. I had recieved a couple of different answers but this one intrigues me the most:

-------------------


sql = "Insert blah blah;Select Ident_Current('tablename') As LastRecord"

Put LastRecord into a session variable.
-------------------

My question is how do I put "LastRecord" into a session variable? Do I just declare a variable and assign "LastRecord" to it, or, is there a different way of doing that?

Thanks
DaleI forgot to ask in the previous post if the "session variable" has to be done in the Global.asax.vb...

Thanks again...
To assign it to a Session variable is simple:

Session["lastrecord"] = LastRecord;

Do you really mean how do you return it from the stored proc?
Well, I guess I'd like to know it all really. I'm really new to this stuff so I need a lot of instruction on what to do with this.

If the stored proc you mentioned is on the DB end then no. I just need to copy the record ID for the current record inserted then copy that to the session variable. Now the session variable makes sense but where in the application do I put that? In the Global.asax.vb or do I make a class to hold it or...?

Things are starting to make sense though. Thanks!
Using SQL Server?

Use the SqlCommand.ExecuteScalar() method to retrieve the object in your query...
Session state is built-in to ASP.NET. On any web form (class that inherits from System.UI.Web.Page) you can use Session state the way I showed you earlier.

Session state is user-specific. If you want that one value to be globally accessible to all users, use Application state. Application state is used almost identically to Session state:

Application[ "lastRecordID" ] = lastRecordID;

I've been doing all my examples in C#, is that ok? Or are you a VB programmer? In which case it would be:

Application( "lastRecordID" ) = lastRecordID
Hello again...

I'm lost (and really frustrated) with this.

I'm using VB as my code and a SQL backend (FYI)...

I understand how the session variables and the application variables work in theory. I just don't understand where to put these things.

Specifically -
1: What document do I put the session variable and where (with a multiple page web app) so that the other parts of the web app can use it as well?

2: How would I obtain the ID of the current record being inserted?

I appologize if this is coming off in a rather brash way, that is not my intent. I am an admin guy with little to no development experience that has been put into a bad position with this application. The more specific explanations for any help I get would be great. Please don't assume I know anything because, frankly, I don't when it comes to this...

Thanks for putting up with my ranting in this one... :)

Dale
1. If you put a variable in Session state on one page, it is accessible across all the web pages for that session (ie, a user).

On page 1, in the Page_Load method:
dim x as int
x = 5
Session["someStringToAccessX"] = x

On page 2, add a label to the page called Label1, and in the Page_Load method
dim y as int
y = CType( Session["someStringToAccessX"], int )
Label1.Text = y.ToString()

Build your app, then open page 1 in the browser, then go to page 2. Label1 should show the text "5". See how that works?

2. Here is a close to complete example:

CREATE PROCEDURE dbo.GetUserPassword
(
@.vchLogin varchar(50),
@.vchPassword varchar(50) = NULL OUTPUT
)

AS
SET NOCOUNT ON
SELECT @.vchPassword = PASSWORD
FROM END_USER
WHERE LOGIN = @.vchLogin

VB.NET
Public Function GetUserPassword(ByVal Login As String, Optional
ByVal ConnString As String = "") As String
Dim strLocalConn As String
Try
strLocalConn = mGetConnection(ConnString)
Dim conn As New SqlConnection(strLocalConn)
conn.Open()
'// Set up the Command to use
Dim cmd As New SqlCommand("GetUserPassword", conn)
cmd.CommandType = CommandType.StoredProcedure
Dim parameterLogin As System.Data.SqlClient.SqlParameter
= New System.Data.SqlClient.SqlParameter("@.vchLogin", SqlDbType.VarChar,
50, ParameterDirection.Input)
parameterLogin.Value = Login
cmd.Parameters.Add(parameterLogin)
Dim parameterPassword As System.Data.SqlClient.SqlParameter =3D New
System.Data.SqlClient.SqlParameter("@.vchPassword", SqlDbType.VarChar,
50, ParameterDirection.Output)
cmd.Parameters.Add(parameterPassword)
cmd.ExecuteScalar()
GetUserPassword = parameterPassword.Value

Catch ex As Exception
Console.WriteLine(ex.ToString)
WriteErrorToAppEvtLog("UserDataComponent", ex.ToString)
End Try
End Function

0 comments:

Post a Comment