Forum Moderators: open
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object sender , EventArgs e)
{
using eInfoDesigns.dbProvider.MySqlClient;
MySqlConnection oMySqlConn = new MySqlConnection(); oMySqlConn.ConnectionString = "my connection string"; oMySqlConn.Open();
}
</script>
I'm getting an error, Syntax error '(' expected, on the 'using eInfoDesigns.dbProvider.MySqlClient;' line. If anyone could help I would appreciate it. I've been searching for hours for just a simple example of a working connection with a query.
The using statement is a Good Thing for opening database connections, because it will always dispose of the connection when the closing brace is reached. Here's the syntax for this usage:
using (SqlConnection conn = new SqlConnection(myConnectionString))
{
conn.Open();
doSomething(conn);
} As you can see, this syntax includes parentheses, and is what the compiler thinks you're trying to do, because you have the "using" statement inside the script block.
However, looking at the format of the using statement in your post, that's not where that statement belongs.
In your example "using eInfoDesigns.dbProvider.MySqlClient;" is NOT the same thing as the using statement detailed above. That format is a "Using" directive that tells the compiler which namespace holds the 3rd party classes and methods you want to use. It belongs outside of any classes. However, since your example indicates you are using inline coding rather than code-behind, that directive should be in another format, and *outside* of the script block. Based on your code example, it should appear thusly:
<%@ Page Language="C#" %>
<%@ Import Namespace="eInfoDesigns.dbProvider.MySqlClient" %><script runat="server">
void Page_Load(Object sender , EventArgs e)
{
MySqlConnection oMySqlConn = new MySqlConnection(); oMySqlConn.ConnectionString = "my connection string"; oMySqlConn.Open();
}
</script>
Now, this only addresses the compiler error you mentioned... I don't use MySQL from ASP.NET, so I don't claim this code actually will work with MySQL :)
Thanks again! :)