Solved

Syntax error Declaration Expected

Posted on 2004-10-30
563 Views
Last Modified: 2010-05-18
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>
0
Question by:jodyglidden
    6 Comments
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    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
     
    LVL 6

    Expert Comment

    by:ryerras
    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
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi jodyglidden,

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

    Regards!
    B..M
    0
     
    LVL 1

    Author Comment

    by:jodyglidden
    Even if I add the strAccount and strAccounts Dim statements I am still getting a Declaration error for the statement mentioned above.
    0
     
    LVL 6

    Accepted Solution

    by:
    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
     
    LVL 1

    Author Comment

    by:jodyglidden
    Yes, that worked.  I just changed the sub name to Page_Load
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now