9

I have a ASP.NET website written by someone a while ago, I don't think they used VS to build their sites but unfortunately am not sure what they did use.

I open as website and build .. no errors.

As soon as I open up any of the pages or classes the intellisense moans about nearly everything, starting with

Error 25 Statement cannot appear outside of a method body/multiline lambda.

on the using statements but literally errors on everyline. But still builds fine.

Any ideas how I can resolve this? I can still build the site but if I make any real errors I can't tell where they have come from and also I have no intellisense.

Edit-- an example if I open one of the simplest files on the site.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class About : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.bodyClass = "about"; 
    }

}

The error list goes straight to 71 Errors on the single file.

Error   26  Syntax error.   PATH\about.aspx 1   PATH\   1
Error   27  Syntax error.   PATH\about.aspx 1       PATH\   1
Error   28  Syntax error.   PATH\about.aspx 1   PATH\   1
Error   29  'Class' statement must end with a matching 'End Class'. PATH\about.aspx 1   PATH\   1
Error   30  Declaration expected.   PATH\about.aspx 1   PATH\   1
Error   31  Bracketed identifier is missing closing ']'.    PATH\about.aspx 1   PATH\   1
Error   33  End of statement expected.  PATH\about.aspx 1   PATH\   1
Error   34  Syntax error.   PATH\about.aspx 1   PATH\   1

(to include the first 8 errors, have changed removed the project name / path)

It will still build correctly (the errors don't seem to bother the compiler), but makes it very difficult to work with.

EDIT -

line 1 of the aspx file

<%@ Page MasterPageFile="~/master.master" CodeFile="index.aspx.cs" Inherits="About" %>
Tom
  • 12,591
  • 13
  • 72
  • 112
  • Yep the site is working, and builds correctly. Just getting loads of errors when I open the pages with in the site. I found this http://stackoverflow.com/questions/2611147/visual-studio-erroneous-errors-when-building-a-website but doesn't solve my problem as I have no dependancies – Tom May 12 '11 at 13:51
  • 1
    Please show us the source with the errors. – SLaks May 12 '11 at 13:54
  • Have included some more info about the errors I am getting – Tom May 12 '11 at 14:07
  • The errors in the update refer the .aspx file, not the code-behind .aspx.cs file – devio May 12 '11 at 14:11
  • @devio have included line 1 of the aspx file. It errors with every single file in the project (but only if they are opened in the editor). – Tom May 12 '11 at 14:22

10 Answers10

14

Visual Studio thinks that your ASPX files are VB.Net rather than C#.

Change the default language in Web.config (<compilation defaultLanguage="C#">).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    is what is currenlty in there – Tom May 12 '11 at 14:30
  • What kind of project is it? Try making a new C# project, then copying all of the files into it. – SLaks May 12 '11 at 14:32
  • 4
    Ok, finally got it to work! The default language in the web.config seemed to be used for building but not for the IDE error checking / intellisense? The only way I got it to work was by adding a Language="C#" to the top of everypage. I did try that earlier, but didn't realise in order for it to have effect the both the page and code behind had to be closed and reopened. – Tom May 12 '11 at 16:56
  • I had to restart Visual Studio 2012 after adding `defaultLanguage="C#"` in web.config – northben Apr 19 '13 at 20:43
3

Had the same problem. None of the solutions here worked for me. My language was already set to C# in the ASPX file and web.config.

The solution in my case seemed to lie in excluding the ASPX & code-behind files, then compiling the project - VS complains about the missing file!

Then including the files back in cleared out the problem.

kannankeril
  • 187
  • 1
  • 6
1

Old thread, but none of the above solutions worked for me as it persisted. My fix was to exclude the aspx file from the project entirely and include it again. Not sure why none of the above suggestions didn't work, but Intellisense seems to happy again.

As a side note: If this is a solution attempted by anyone, make sure you have Show All Files checked in Solution Explorer, otherwise your file will be hidden once excluded.

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
1

To go further, verify that in your .aspx file, the code starts with "<%@ Page Language="C#"..". This manipulation has been described in the answer, but i will notice that it doesn't work IF THE PAGE STARTS WITH COMMENT LINES ! Visual Studio 2010 doesn't verify if the text before the declaration "<%@ Page />" is comment or not.

Example :

<%--<asp:Label Text="This is a comment"></asp:Label>--%>
<%@ Page Language="C#" ... %>

In this example VS 2010 think the page doesn't start at the Page declaration, but at the Label declaration, so, C# code isn't recognized...

Gwen
  • 11
  • 1
0

I faced the same problem. I encountered the problem, when I copied the .aspx code and pasted it into one "test page" which I used for my reference which by mistake was included in my project. I removed the contents in that test page. The intellisense started working fine. Check in your project if the aspx contents have been copied elsewhere. This should solve your problem.

Rob
  • 45,296
  • 24
  • 122
  • 150
Arun
  • 1
0

The problem was caused by copy and paste .aspx xml code. You can exclude the problem files from the whole project. Then add them back, the problem will get fixed.

0

I know it's too late to answer, but perhaps somebody is looking for an answer. I had the same issue, and in MY case it works,so first lines are:

<%@ Page Language="C#" AutoEventWireUp="True" CodeBehind="reservas.aspx.cs" Inherits="CapaWeb.reservas"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html>
<head runat="server">

I had to ensure the first (and I think the most important) line and the "runat=server"

Also, after adding these lines, I closed all classes. Rebuild the solution and error disappeared.

Cecilia Barboza
  • 107
  • 1
  • 6
0

I had same problem and visual studio suggest close and re-open that file. Close and re-open fixed problems for me.I have no idea why?!! :-\

Mohsen Abdollahi
  • 901
  • 13
  • 14
0

If this is a ASP.Net WebForms web site, then the markup in .aspx and .ascx is compiled only at runtime.

To compile the markup, use aspnet_compiler. (see also this question)

Update Re: line 1 of the aspx file

<%@ Page MasterPageFile="~/master.master" CodeFile="index.aspx.cs" Inherits="Home" %>

So the file is named about.aspx, the code behind is index.aspx.cs, and the page's class name is Home??

Community
  • 1
  • 1
devio
  • 36,858
  • 7
  • 80
  • 143
  • Why would this mean that I am getting all of the errors in VS? Is there a way of getting intellisense working? It builds fine using F5 / F6 – Tom May 12 '11 at 14:16
  • That's the opposite of his question. – SLaks May 12 '11 at 14:26
  • Whoops think I copied from the wrong place will update the question. Think its solved now, adding in the Language="C#" directive on every page seemed to work. The web.config language seemed to be ignored by the ide – Tom May 12 '11 at 17:07
0

A long shot, but usually when I run into errors like these, I check to make sure that if I'm referencing any other projects, every project is on the same targetted framework. You can check this by Alt+Enter on a project in VS and checking the targetted framework.

Josh
  • 10,352
  • 12
  • 58
  • 109
  • it is only one website which isn't referencing any other projects, I found this http://stackoverflow.com/questions/2611147/visual-studio-erroneous-errors-when-building-a-website but it doesn't seem to be any help in my case – Tom May 12 '11 at 14:18