Problem with variable scope in aspx page

M

mister-Ed

I'm trying to check for "No Records Found" in a repeater list control,
but my 'numrows' variable cannot be seen by the function CheckNoR ,
even tho it's public (?) . I've been informed that the page class has
to have these declared as member variables, but still cannot see why
this won't work:

<script runat="server">
'Handle page load event
public Sub Page_Load(Sender As Object, E As EventArgs)

Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataAdapter
dim MyDataset As DataSet
dim MyTable As DataTable
dim numrows As Integer
dim sqlstr As String
sqlstr = "SELECT company,city from customers "

MyConnection = New SqlConnection("Data
Source=dbase.net;Initial
Catalog=mydb;User Id=myuser;Password=mypwd;")

MyCommand = New SQLDataAdapter(sqlstr, MyConnection)

' Create a Command object with the SQL statement.
MyCommand = New SQLDataAdapter(sqlstr, MyConnection)

' Fill a DataSet with data returned from the database.
MyDataset = New DataSet
MyCommand.Fill(MyDataset)

' Create a new DataTable object and assign to it
' the new table in the Tables collection.

MyTable = New DataTable
MyTable = MyDataset.Tables(0)

''''tie it to my repeater control
dr.DataSource = MyTable
dr.DataBind()

MyConnection.Close

End Sub

</script>
<script runat="server">
function checkNoR(ByVal as String)
numrows = MyTable.Rows.Count
If numrows = 0 then
Response.Write("<p>No records.</p>")
End If
end function
</script>
 
R

Robbe Morris - [MVP] C#

Just declare numrows as private just below
this line in your code (which is outside the Page_Load sub)
instead of inside the Page_Load sub.

'Handle page load event
 

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

Top