creating a global variable to use throughout the application

J

Jim in Arizona

I'm making an app that inputs into multiple tables. I'm using the
SCOPE_IDENTITY() SQL function for foreign key purposes. I input into the
main table first and retrieve the scope_identity() and then insert into
related tables using the scope_identity() number as the foregin key entry
for each table.

Each of these inputs is done within the code for seperate buttons. How do I
create a variable (a string type) I can use throughout the application? I'd
populate it with the scope_identity() and would want that number to remain
in that variable until I manually clear it out.

I tried this at the top of the page (code file) before any other
subroutines.

Partial Class inventory_index
Inherits System.Web.UI.Page
Public strScopeID As String

But if I try a response.write(strScopeID) after I populate it (at least I
think I do), I get the error:

Object reference not set to an instance of an object.

TIA,

Jim
 
J

Joern Schou-Rode

I'm making an app that inputs into multiple tables. I'm using the
SCOPE_IDENTITY() SQL function for foreign key purposes. I input into the
main table first and retrieve the scope_identity() and then insert into
related tables using the scope_identity() number as the foregin key entry
for each table.

Each of these inputs is done within the code for seperate buttons. How
do I
create a variable (a string type) I can use throughout the application?
I'd
populate it with the scope_identity() and would want that number to
remain
in that variable until I manually clear it out.

I tried this at the top of the page (code file) before any other
subroutines.

Partial Class inventory_index
Inherits System.Web.UI.Page
Public strScopeID As String

But if I try a response.write(strScopeID) after I populate it (at least I
think I do), I get the error:

Object reference not set to an instance of an object.

TIA,

Jim

I am not absolutely sure whether I understand your question correctly, so
please bear with me if the following suggestion does not help you at all :)

To write the application variable from within a Page:
Application("ScopeID") = "something from the db"

To read and output the application variable from within a Page:
Response.Write(Application("ScopeID"))
 
J

Jim in Arizona

I am not absolutely sure whether I understand your question correctly, so
please bear with me if the following suggestion does not help you at all
:)

To write the application variable from within a Page:
Application("ScopeID") = "something from the db"

To read and output the application variable from within a Page:
Response.Write(Application("ScopeID"))

I ended up going off of your example and so far, with a small test that I
did, this seems to be the solution that I need.

Dim strScopeID, strSQLSI as String

strSQLSI = "SELECT SCOPE_IDENTITY()"

Dim objConnection As New SqlConnection(strConn)
Dim objCommand As New SqlCommand(strSQL, objConnection)
Dim objCmdSI As New SqlCommand(strSQLSI, objConnection)

objConnection.Open()
objCommand.ExecuteNonQuery()
strScopeID = objCmdSI.ExecuteScalar()
objConnection.Close()

Application.Lock()
Application("SI") = strScopeID.ToString
Application.UnLock()

Response.Write(Application("SI").ToString)


Thanks Joern!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top