Syntax error Declaration Expected

Anyone see why I'm getting the declaration expected error?

<%@ Page Language="VB" %>
<%@ import Namespace="Microsoft.Win32" %>
<script runat="server">

    dim Accountskey as RegistryKey

right here-->    Accountskey = Registry.CurrentUser.OpenSubKey("Software", False)

        for each strAccount in Accountskey.getvaluenames()
            strAccounts = strAccounts & strAccount
        next
        Label1.text = strAccounts

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        Done
        <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        <asp:Label id="Label1" runat="server">Label</asp:Label>
        <!-- Insert content here -->
    </form>
</body>
</html>
LVL 1
jodygliddenAsked:
Who is Participating?
 
ryerrasConnect With a Mentor Commented:
hmm ok. its because, you put the inline code between the script tags.

When you use the <script runat="server"></script> tags to define a block of code, essentially ASP.NET inlines everything between the blocks directly into the body of the class that is generated for the page.  In other words, you can define variables for the class, and routines or functions for a class, but you cannot directly include source code inside a <script> tag.

However, if you use the <% %> tags to isolate your code blocks, ASP.NET essentially creates a new function that is executed at that point in the document cycle.  All of the inner text of this block is dumped into the internals of the function, which means that you can use source code or definitions or whatever else you want inside of this tag, and you won't get an error.  I say that it essentially creates a function because it doesn't really, variables that are defined inside a <% %> block are scoped for the entire page, so that any code running in the page can access them.  This is different than if you were creating real functions

So your code should be



<%@ Page Language="VB" %>
<%@ import Namespace="Microsoft.Win32" %>
<script runat="server">
Public Sub ReadFromRegistry
    dim Accountskey as RegistryKey
  //---------- new declarations
    dim strAccount as string
    dim strAccounts as string
   Accountskey = Registry.CurrentUser.OpenSubKey("Software", False)

        for each strAccount in Accountskey.getvaluenames()
            strAccounts = strAccounts & strAccount
        next
        Label1.text = strAccounts
End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        Done
        <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        <asp:Label id="Label1" runat="server">Label</asp:Label>
        <!-- Insert content here -->
    </form>
</body>
</html>

This should compile and work for you
0
 
nauman_ahmedCommented:
It looks fine to me, as this is the way described in MSDN:

Private Sub DoesKeyExist
    Dim regVersion As Microsoft.Win32.RegistryKey
    regVersion = _
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0", True)
    If regVersion Is Nothing Then
        regVersion = _
Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0")
    End If
End Sub

Also, here you are trying to acces the registry on the server. Does user has permission to access the registry? Here are some links that might be helpful:

http://msdn.microsoft.com/library/en-us/dnnetsec/html/SecNetHT11.asp
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2874&lngWId=10

Best, Nauman.
0
 
ryerrasCommented:
I am not sure why u r getting the error over there, your error should be in the following for loop

for each strAccount in Accountskey.getvaluenames()
            strAccounts = strAccounts & strAccount
        next
        Label1.text = strAccounts

If you you have not declared neither strAccount or strAccounts

you did not seem to declare them as strings prior to using them. thats all the syntax errors i can see
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mmarinovCommented:
Hi jodyglidden,

ryerras is correct - the problem is with strAccount and strAccounts, not with the OpenSubKey method

Regards!
B..M
0
 
jodygliddenAuthor Commented:
Even if I add the strAccount and strAccounts Dim statements I am still getting a Declaration error for the statement mentioned above.
0
 
jodygliddenAuthor Commented:
Yes, that worked.  I just changed the sub name to Page_Load
0
All Courses

From novice to tech pro — start learning today.