1

I cannot seem to add namespaces to my .aspx page without getting "A namespace cannot directly contain members such as fields or methods". The header of my aspx project is set up like so:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient;"%>
<%@ Import Namespace="System.IO;"%>
<%@ Import Namespace="System.Net.Mail;"%>
<%@ Import Namespace="System.Text;"%>
<%@ Import Namespace="System.Web;"%>
<%@ Import Namespace="System.Linq;"%>
<%@ Import Namespace="System.Web.Security;"%>
<%@ Import Namespace="System.Web.UI;"%>
<%@ Import Namespace="System.Web.UI.WebControls;"%>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts;"%>
<%@ Import Namespace="System.Web.UI.HtmlControls;"%>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
    {
        lblDate.Text = DateTime.Now.ToString();
    }
//more functions

I did do a google search which lead me back here. This talks about finding your app.config file. Well, my project has no app.config, when I try to add one in VS2012 its not even listed as an option.

4
  • 1
    For aspx, it is not app.config it is web.config. Commented Jul 19, 2013 at 14:56
  • is this the only way to resolve this issue. I am using DNN if I change from compile to build it will cause serious havoc. I really do not want to make it into a module. Commented Jul 19, 2013 at 14:58
  • I don't think you need if you get solved by the below answer. Commented Jul 19, 2013 at 15:00
  • 2
    Just one quick thing: With an HTML5 doctype, you shouldn't have the XHTML attribute on your html tag. Commented Jul 19, 2013 at 15:01

2 Answers 2

5

Don't know why the ASP.NET compiler gives you that particular error, but the semi-colons are what's causing the issue. You need to remove them.

<%@ Import Namespace="System.Data.SqlClient" %>
Sign up to request clarification or add additional context in comments.

Comments

0

Building off of what Joe said above, semi-colons are fine for Object Oriented languages like C# or Java since they see the semi-colon as the end of a statement.

In a language like this though they're not needed as heavily if at all, and in the case of your imports it's looking at that as the literal path to the namespace. So with the semi-colon in there it assumes that it's part of the namespace path as well and is unable to find it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.