simple db-problem

L

Lasse Edsvik

Hello

Why i get:

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

Line 10: Conn.Open();


with this code:

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="c#" runat="server">
SqlConnection Conn = new SqlConnection(
ConfigurationSettings.AppSettings("ConnectionString"));

const string SQL = "SELECT * FROM Customers";
SqlCommand Cmd = new SqlCommand(SQL, Conn);

Conn.Open();
nw.DataSource = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
nw.DataBind();
</script>
<html>
<body>
<asp:datagrid id="nw" runat="server" />
</body>
</html>


web.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="User ID=myuid;Password=mypass;Initial Catalog=Northwind;Data
Source=localhost"/>
</appSettings>

<system.web></system.web>
</configuration>
 
M

Martin Dechev

Hi, Lasse Edsvik,

The code execution in C# can be only done in methods. Your code is
equivalent to:

class class1 : System.Web.UI.Page
{
SqlConnection Conn = new
SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));

const string SQL = "SELECT * FROM Customers";
SqlCommand Cmd = new SqlCommand(SQL, Conn);

Conn.Open();
//...
}

which is incorrect. Add your code in the Page_Load(object,EvenArgs) method
for example and it should work fine.

Also, consider using the two-file, code-behind model. Here are the
differences:

http://msdn.microsoft.com/library/en-us/vbcon/html/vbconWebFormsCodeModel.asp

Hope this helps
Martin
Hello

Why i get:

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

Line 10: Conn.Open();


with this code:

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="c#" runat="server">
SqlConnection Conn = new SqlConnection(
ConfigurationSettings.AppSettings("ConnectionString"));

const string SQL = "SELECT * FROM Customers";
SqlCommand Cmd = new SqlCommand(SQL, Conn);

Conn.Open();
nw.DataSource = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
nw.DataBind();
</script>
<html>
<body>
<asp:datagrid id="nw" runat="server" />
</body>
</html>


web.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="User
ID=myuid;Password=mypass;Initial Catalog=Northwind;Data
Source=localhost"/>
</appSettings>

<system.web></system.web>
</configuration>
 
L

Lasse Edsvik

hmm ok......

but now i get:

Compiler Error Message: CS0118:
'System.Configuration.ConfigurationSettings.AppSettings' denotes a
'property' where a 'method' was expected


Line 9: ConfigurationSettings.AppSettings("ConnectionString"));
 
M

Martin Dechev

You should use the indexer:

ConfigurationSettings.AppSettings["ConnectionString"]

Greetings
Martin
 

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

Similar Threads


Top