Syntax error about imports having to precede declarations

Posted on 2004-10-29
Last Modified: 2008-12-18
I have the following code that is giving me an error:

Imports statements must precede declarations.  My imports is before my declarations.  Any idea what I'm doing wrong?

<%@ Page Language="VB" %>
<script runat="server">

    Imports Microsoft.Win32
    dim Accountskey as registrykey
    Accountskey = Registry.CurrentUser.OpenSubKey("Software\VB and VBA Program Settings\Mailgov (Redirector)\AccountSettings", False)
        for each strAccount in Accountskey.getvaluenames()
            strAccounts = strAccounts & strAccount
        Label1.text = strAccounts

    <form runat="server">
        <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        <asp:Label id="Label1" runat="server">Label</asp:Label>
        <!-- Insert content here -->
Question by:jodyglidden
    LVL 44

    Expert Comment

    why are you writing the VB.NET code in your aspx page,and not using the CodeBehind.  The problem is that the code as you have it here, is actually being seen, by the JIT compiler, as if it were following the implied declarations of the controls on your page, and that is sees as creasting the 'error' that you are seeing.

    ASP.NET does NOT support coding as you have it here, which is the older ASP style, using embedded VBScript.  In fact, you cannot embed VBScript in the ASPX page, in any fashion.  

    ASP.NET is a completely DIFFERENT style of web development, and you would be VERY wise to TOTALLY FORGET (yes FORGET) everything your ever knew (or thought you knew) about ASP development, becuase it has ABSOLUTELY no relevance to ASP.NET development.  The two are as different as Night and Day - actually they are more different - say like Cats and Airplanes.

    LVL 15

    Accepted Solution

    Importing namespaces is possible even when using interspersed ASP.NET code.

    Add this under your @Page directive.
    <% @Import Namespace="Microsoft.Win32" %>


    As far as I know, there is nothing code-behind can do that embedded code cannot do. I use code-behind myself, but embedded code is just as powerful.
    LVL 1

    Author Comment

    That's what I've read as well.  Also, Timbo fixed my bug.  Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    846 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

    6 Experts available now in Live!

    Get 1:1 Help Now