Compiler Error Message: CS1519: Invalid token '=' in class, struct, or interface member declaration

19,561

Solution 1

The problem is that inline code in ASP.NET pages (unlike in classic ASP) is compiled in the scope of a class, not the scope of a function (or a loose ASP script) and therefore you need to surround your code with a method declaration. ASP.NET provides and automatically wires up a Page_Load function and calls it during the page lifecycle.

<script runat="server" language="C#">
public void Page_Load(object sender, EventArgs e)
{
    // put your existing code here
}
</script>

Solution 2

This error happens due to version your MSBuild, old version of MSBuild can only compile C# version 4, while your code written in C# version 6 format.

Example of code writing in C# version 6:

 public static string HostName { get; set; } = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";

For MSBuild to compile your code, you need to write in C# 4 style

public static string HostName { get; set; }
public SomeConstructor()
        {
            Host = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";... }
Share:
19,561
adL
Author by

adL

Updated on June 24, 2022

Comments

  • adL
    adL almost 2 years

    I keep encountering the following compilation error:

                Compilation Error
    
                Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
                Compiler Error Message: CS1519: Invalid token '=' in class, struct, or interface member declaration
    
                Source Error:
    
    
                  Line 22:              
                  Line 23:  //Assign a Connection String
                  Line 24:  conn.ConnectionString = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ConnectionString;
                  Line 25:          
                  Line 26:  //Connection Open   
    
                  Source Line: 24 
    

    Just want to preface this with new to programming in general and in ASP and C#. I've used the same code to connect to the database before and it worked fine but now I'm getting that error which I'm not too familiar with on how to resolve. Below is the code for my aspx page and my web.config.

                <%@Page Language="C#" MasterPageFile="MasterPage/AtronsSiteMaster.master"%>
                <%@ Import Namespace="System.Data"%>
                <%@ Import Namespace="System.Data.Common"%>
                <%@ Import Namespace="System.Data.OleDb"%>
                 <%@ Import Namespace="System.Configuration"%>
                <%@ Import Namespace="System.Collections.Generic"%>
    
                <asp:Content ContentPlaceHolderID="titleContentPlaceHolder" ID="titleContent" runat="server">Products</asp:Content>
    
               <asp:Content ContentPlaceHolderID="headContentPlaceHolder" ID="headContent" runat="server"></asp:Content>
    
                 <script runat="server" language="C#">
    
                        String provider = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ProviderName;
    
                        DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
    
                        //Open a Connection
                        DbConnection conn = factory.CreateConnection();
    
                        //Assign a Connection String
                        conn.ConnectionString = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ConnectionString;
    
                        //Connection Open
                        conn.Open();
    
                        //Initialize a Command
                        DbCommand comm = conn.CreateCommand();
    
                        //Tell the command which connection it will use
                        comm.Connection = conn;
    
                        //Give the command SQL to execute
                        comm.CommandText = "Select ProductName,ProductIssue,Writer,UnitPrice from Products order by ProductName, ProductIssue";
    
                        //Execute the command and get back the results via a reader
                        DbDataReader reader = comm.ExecuteReader();
    
                        //While we get results from the DB, add a row to the Table
                        while (reader.Read())
                        {
                            TableRow row = new TableRow();
                            TableCell cell;
    
                            cell = new TableCell();
                            cell.Text = reader["ProductName"].ToString();
                            row.Cells.Add(cell);
    
                            cell = new TableCell();
                            cell.Text = reader["ProductIssue"].ToString();
                            row.Cells.Add(cell);
    
                            cell = new TableCell();
                            cell.Text = reader["Writer"].ToString();
                            row.Cells.Add(cell);
    
                            cell = new TableCell();
                            cell.Text = reader["UnitPrice"].ToString();
                            row.Cells.Add(cell);
                        }
    
                        //Free up the connection
                        conn.Close();
    
                    </script>
    
                    <asp:Content ContentPlaceHolderID="pageTitleContentPlaceHolder" ID="pageTitleContent" runat="server">Products</asp:Content>
    
                    <asp:Content ContentPlaceHolderID="mainContentPlaceHolder" ID="mainContent" runat="server">
                        <asp:Table ID="tblData" runat="server">
                            <asp:TableHeaderRow>
                                <asp:TableHeaderCell>Comic Book Name</asp:TableHeaderCell>
                                <asp:TableHeaderCell>Issue</asp:TableHeaderCell>
                                <asp:TableHeaderCell>Writer Name</asp:TableHeaderCell>
                                <asp:TableHeaderCell>Price</asp:TableHeaderCell>
                            </asp:TableHeaderRow>
                        </asp:Table>
                 </asp:Content> 
    
                <configuration>
                       <system.web>
                         <customErrors mode="Off"/>
                        <compilation debug="true"/>
                     </system.web>
    
    
    
                       <connectionStrings>
                                <add name="databaseConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\Database\database.accdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
                                <add name="studentConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\Database\students.mdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
                                <add name="sauceatronConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\finals\Database\SauceAtronsVault.accdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
                       </connectionStrings>
    
                  </configuration>