Reputation: 2215
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.
Upvotes: 1
Views: 1874
Reputation: 2516
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.
Upvotes: 0
Reputation: 40403
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" %>
Upvotes: 5