Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declarat

E

Erik H.

I have an ASPX page in which I am trying to bind a datagrid to a dataset
pulled from Microsoft Access DB using code inline method. For some reason,
the compiler is having a problem with 'using'. Any help here would be much
appreciated. Thanks!

Getting the following 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 'using' in class, struct, or
interface member declaration

Source Error:



Line 2: <script runat="server">
Line 3:
Line 4: using System.Data;
Line 5: using System.Data.oledb;
Line 6: using System.Web.UI;



My Code:
<%@ Page Language="C#" %>
<script runat="server">

using System.Data;
using System.Data.oledb;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


private void Form_Load(object sender, System.EventArgs e)
{

// create a connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Dev\ATTRIBUTES_VH\ATTRIBUTES_VH.mdb";
OleDbConnection myConnection = new OleDbConnection();
myConnection.ConnectionString = connString;// create a data adapter
OleDbDataAdapter da = new OleDbDataAdapter("Select * from mytable",
myConnection);

// create a new dataset
DataSet ds = new DataSet();

// fill dataset
da.Fill(ds, "RateCenters");

// Attach DataSet to DataGrid
DataGrid1.DataSource = ds.DefaultViewManager;
DataGrid1.databind()

myConnection.close()
}

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</html>
 
C

Craig Deelsnyder

Line 2: <script runat="server">
Line 3:
Line 4: using System.Data;
Line 5: using System.Data.oledb;
Line 6: using System.Web.UI;

When you need to this on the aspx page itself, it uses a different
notation; you need to use the Import directive at the top of your page
(this applies to both C# and VB.NET, there is no USING directive)

My Code:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.oledb" %>
<script runat="server">


private void Form_Load(object sender, System.EventArgs e)
{

etc...the reason (ignore if confusing) is whatever code you put in an aspx
is already being defined within a special page class that is
auto-generated behind the scenes including the code you give it. That's
why a directive is used, to signify this needs to be added to the
auto-generated code and not just inserted into the class definition.
 
E

Erik H.

Much thanks! This eliminated the error that I was getting...

Was wondering why I didn't get this error when I tried the code behind
method =)

-Erik
 
C

Craig Deelsnyder

Much thanks! This eliminated the error that I was getting...

Was wondering why I didn't get this error when I tried the code behind
method =)

-Erik

Yup, that's correct in the code-behind, because you're defining the class
yourself. But when you put code into an .aspx, it's like you're defining
everything inside of a class definition:

public class DotNetAutoGeneratedClass_aspx()
{
//your code ends up in here, some class the
//framework creates, so a using statement would not work here
}

which is why a directive (preprocessing statement) is needed...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top