473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a Variable

Hi,

I need to pass the value of a variable from one function to another but I
don't seem to get the value. I declare the variable outside all functions.
What I'm trying to do is that when the button is clicked, check to see if
the record exists using input from a form. I want to pass that variable to
the button click Sub because if it doesn't exist then the record is
inserted and the user is directed to a different page. The variable I'm
checking is recExists.

Thanks for any help.

This is my code

<script runat="server">
Dim recExists as Integer
Dim IntLessonID as Integer
Sub Page_Load(sende r As Object, e As EventArgs)
If Page.IsPostBack = False Then
IntLessonID = Request.QuerySt ring( "LessonID" )
Page.Databind()
End If
End Sub

Function RecordExists(By Val PageNumber As Integer, ByVal LessonID As
Integer) As System.Data.IDa taReader
Dim connectionStrin g As String = "server='(local )';
trusted_connect ion=true; database='xx'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)

Dim queryString As String = "SELECT [tblPage].[PageNumber], [tblPage].
[LessonID] FROM [tblPage] WHERE (([t"& _
"blPage].[PageNumber] = @PageNumber) AND ([tblPage].[LessonID] =
@LessonID))"
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Comma ndText = queryString
dbCommand.Conne ction = dbConnection

Dim dbParam_PageNum ber As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_PageNum ber.ParameterNa me = "@PageNumbe r"
dbParam_PageNum ber.Value = PageNumber
dbParam_PageNum ber.DbType = System.Data.DbT ype.Int32
dbCommand.Param eters.Add(dbPar am_PageNumber)
Dim dbParam_LessonI D As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_LessonI D.ParameterName = "@LessonID"
dbParam_LessonI D.Value = LessonID
dbParam_LessonI D.DbType = System.Data.DbT ype.Int32
dbCommand.Param eters.Add(dbPar am_LessonID)

dbConnection.Op en

Dim dataReader As System.Data.IDa taReader =
dbCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection)

If (dataReader.Rea d = True) Then
recexists = 1
msgLabel.Text = "Record exists"
Else
recexists = 0
'insert record code here
End If
Return dataReader
End Function

Sub FinishButton_Cl ick(sender As Object, e As EventArgs)
IntLessonID = Request.QuerySt ring( "LessonID" )
RecordExists (fPageNumber.Se lectedItem.Valu e, IntLessonID)
If RecExists = 1 Then
Response.Write( "Record Exists")
Else
Response.Write( "Record doesn't Exist")
End If

End Sub

</script>
<html>
<head>
<link href="assets/style_admin.css " type="text/css" rel="stylesheet " />
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
Page Number:
<asp:DropDownLi st id="fPageNumber " runat="server">
<asp:ListItem Value="0"> </asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownLis t>
<br>
<asp:Button id="FinishButto n" onclick="Finish Button_Click"
runat="server" Text="Finish"></asp:Button>

</form>
</body>
</html>

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
5 1847
Dont forget that any variable assign a value to will be lost between
successive postback's unless you preserve it say using say a Session
key/value pair for example.

So use the IsPostback to determine if you get the value from Session.

HTH
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar( Convert.ToInt16 (ch(i)) - 1)
Next
Process.Start(" mailto:" & New String(ch))
--
"Jim via DotNetMonster.c om" <fo***@DotNetMo nster.com> wrote in message
news:23******** *************** *******@DotNetM onster.com...
Hi,

I need to pass the value of a variable from one function to another but I
don't seem to get the value. I declare the variable outside all functions.
What I'm trying to do is that when the button is clicked, check to see if
the record exists using input from a form. I want to pass that variable to
the button click Sub because if it doesn't exist then the record is
inserted and the user is directed to a different page. The variable I'm
checking is recExists.

Thanks for any help.

This is my code

<script runat="server">
Dim recExists as Integer
Dim IntLessonID as Integer
Sub Page_Load(sende r As Object, e As EventArgs)
If Page.IsPostBack = False Then
IntLessonID = Request.QuerySt ring( "LessonID" )
Page.Databind()
End If
End Sub

Function RecordExists(By Val PageNumber As Integer, ByVal LessonID As
Integer) As System.Data.IDa taReader
Dim connectionStrin g As String = "server='(local )';
trusted_connect ion=true; database='xx'"
Dim dbConnection As System.Data.IDb Connection = New
System.Data.Sql Client.SqlConne ction(connectio nString)

Dim queryString As String = "SELECT [tblPage].[PageNumber], [tblPage].
[LessonID] FROM [tblPage] WHERE (([t"& _
"blPage].[PageNumber] = @PageNumber) AND ([tblPage].[LessonID] =
@LessonID))"
Dim dbCommand As System.Data.IDb Command = New
System.Data.Sql Client.SqlComma nd
dbCommand.Comma ndText = queryString
dbCommand.Conne ction = dbConnection

Dim dbParam_PageNum ber As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_PageNum ber.ParameterNa me = "@PageNumbe r"
dbParam_PageNum ber.Value = PageNumber
dbParam_PageNum ber.DbType = System.Data.DbT ype.Int32
dbCommand.Param eters.Add(dbPar am_PageNumber)
Dim dbParam_LessonI D As System.Data.IDa taParameter = New
System.Data.Sql Client.SqlParam eter
dbParam_LessonI D.ParameterName = "@LessonID"
dbParam_LessonI D.Value = LessonID
dbParam_LessonI D.DbType = System.Data.DbT ype.Int32
dbCommand.Param eters.Add(dbPar am_LessonID)

dbConnection.Op en

Dim dataReader As System.Data.IDa taReader =
dbCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection)

If (dataReader.Rea d = True) Then
recexists = 1
msgLabel.Text = "Record exists"
Else
recexists = 0
'insert record code here
End If
Return dataReader
End Function

Sub FinishButton_Cl ick(sender As Object, e As EventArgs)
IntLessonID = Request.QuerySt ring( "LessonID" )
RecordExists (fPageNumber.Se lectedItem.Valu e, IntLessonID)
If RecExists = 1 Then
Response.Write( "Record Exists")
Else
Response.Write( "Record doesn't Exist")
End If

End Sub

</script>
<html>
<head>
<link href="assets/style_admin.css " type="text/css" rel="stylesheet " />
</head>
<body leftmargin="0" topmargin="0">
<form runat="server">
Page Number:
<asp:DropDownLi st id="fPageNumber " runat="server">
<asp:ListItem Value="0"> </asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownLis t>
<br>
<asp:Button id="FinishButto n" onclick="Finish Button_Click"
runat="server" Text="Finish"></asp:Button>

</form>
</body>
</html>

--
Message posted via http://www.dotnetmonster.com

Nov 19 '05 #2
I need the variable to just be active for that page since everytime they
submit that variable is going to be different so I don't think a session
variable will work unless I can set it to just that page.

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #3
Hi Jim,

If you need to keep the variable alive for a page by page basis, then keep
them in a viewstate / a hidden contro (HTMLHidden control) on the page.

This will make it alive only for that page.
Also, Check the IsPostBack on page_Load, a potential pitfall..

Need any help, do post a msg back...

Happy Coding

"Jim via DotNetMonster.c om" wrote:
I need the variable to just be active for that page since everytime they
submit that variable is going to be different so I don't think a session
variable will work unless I can set it to just that page.

--
Message posted via http://www.dotnetmonster.com

Nov 19 '05 #4
Thanks so much for your help. I ended up using a session variable that gets
set whenever the page is accessed and that works. I can't use the hidden
field because by the variable is set outside the form.

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #5
You can set a "hidden field" in the page from your server-side code
(your RecordExists function) by using the ViewState object:

ViewState("Reco rdExists") = true;

and then read it in another function (on a different page postback) using:

If ViewState("Reco rdExists") Then...
Note that I am using a Boolean instead of an Integer, since that seems
to be what you want.

Jim via DotNetMonster.c om wrote:
Thanks so much for your help. I ended up using a session variable that gets
set whenever the page is accessed and that works. I can't use the hidden
field because by the variable is set outside the form.

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14944
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10167
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
39
7661
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
6
3253
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
11
8128
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
6
5996
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
12
2683
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
3
1986
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on the WEB site to mail to passing a variable from the WEB site to the ASPX web site to E-Mail to? Hope I explained this correctly. This is a response from another group. There was no way for you to know it, but this is a classic asp newsgroup....
3
2441
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I thought passing by reference, your passing the address in memory. public bool DoSomething(ref byte data) { byte retVal = null; try
11
6167
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.