Passing a Variable

  • Thread starter Jim via DotNetMonster.com
  • Start date
J

Jim via DotNetMonster.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(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
IntLessonID = Request.QueryString( "LessonID" )
Page.Databind()
End If
End Sub

Function RecordExists(ByVal PageNumber As Integer, ByVal LessonID As
Integer) As System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

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.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_PageNumber As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_PageNumber.ParameterName = "@PageNumber"
dbParam_PageNumber.Value = PageNumber
dbParam_PageNumber.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_PageNumber)
Dim dbParam_LessonID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_LessonID.ParameterName = "@LessonID"
dbParam_LessonID.Value = LessonID
dbParam_LessonID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_LessonID)

dbConnection.Open

Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

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

Sub FinishButton_Click(sender As Object, e As EventArgs)
IntLessonID = Request.QueryString( "LessonID" )
RecordExists (fPageNumber.SelectedItem.Value, 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:DropDownList 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:DropDownList>
<br>
<asp:Button id="FinishButton" onclick="FinishButton_Click"
runat="server" Text="Finish"></asp:Button>

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

OHM \( Terry Burns \)

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/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
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.com said:
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(sender As Object, e As EventArgs)
If Page.IsPostBack = False Then
IntLessonID = Request.QueryString( "LessonID" )
Page.Databind()
End If
End Sub

Function RecordExists(ByVal PageNumber As Integer, ByVal LessonID As
Integer) As System.Data.IDataReader
Dim connectionString As String = "server='(local)';
trusted_connection=true; database='xx'"
Dim dbConnection As System.Data.IDbConnection = New
System.Data.SqlClient.SqlConnection(connectionString)

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.IDbCommand = New
System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_PageNumber As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_PageNumber.ParameterName = "@PageNumber"
dbParam_PageNumber.Value = PageNumber
dbParam_PageNumber.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_PageNumber)
Dim dbParam_LessonID As System.Data.IDataParameter = New
System.Data.SqlClient.SqlParameter
dbParam_LessonID.ParameterName = "@LessonID"
dbParam_LessonID.Value = LessonID
dbParam_LessonID.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_LessonID)

dbConnection.Open

Dim dataReader As System.Data.IDataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

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

Sub FinishButton_Click(sender As Object, e As EventArgs)
IntLessonID = Request.QueryString( "LessonID" )
RecordExists (fPageNumber.SelectedItem.Value, 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:DropDownList 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:DropDownList>
<br>
<asp:Button id="FinishButton" onclick="FinishButton_Click"
runat="server" Text="Finish"></asp:Button>

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

Jim via DotNetMonster.com

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.
 
G

Guest

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
 
J

Jim via DotNetMonster.com

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.
 
J

Joshua Flanagan

You can set a "hidden field" in the page from your server-side code
(your RecordExists function) by using the ViewState object:

ViewState("RecordExists") = true;

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

If ViewState("RecordExists") Then...


Note that I am using a Boolean instead of an Integer, since that seems
to be what you want.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top