conn.Close() error, missing directive?

Discussion in 'ASP .Net' started by FC, Sep 27, 2005.

  1. FC

    FC Guest

    Hello All:
    I am getting the following error: Compiler Error Message: CS0246: The
    type or namespace name 'conn' could not be found (are you missing a
    using directive or an assembly reference?)

    This error point to the line conn.Close() on my finally statement. I am
    pretty new at this and I did not find much on the web. Any help would
    be greatly appreciated. My code is as follows:

    <%@ Page Language="C#" debug="true" %>
    <%@ Import Namespace = "System.Data" %>
    <%@ Import Namespace = "System.Data.SqlClient" %>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    </head>
    <body>
    <%
    string ConnString = "ommitted for security reasons"
    try
    {
    string stmt = @"select * from gbook";
    SqlConnection conn = null;
    conn = new SqlConnection(ConnString);
    conn.Open();
    SqlCommand cmd = new SqlCommand(stmt,conn);
    SqlDataReader reader = cmd.ExecuteReader();

    while ( reader.Read() )

    %>
    <table width="790" border="0" align="center" cellpadding="3"
    cellspacing="3">
    <tr>
    <td><table width="100%" border="1" cellspacing="3" cellpadding="3">
    <tr>
    <td width="40%"><strong><font size="2" face="Verdana, Arial,
    Helvetica, sans-serif">
    On
    <%
    Response.Write(reader.GetString(1));//this worked before I put the
    HTML
    //Response.Write(" ");
    //Response.Write(reader.GetString(2));
    %>
    said:</font></strong></td>
    <td width="60%"><font size="2" face="Verdana, Arial,
    Helvetica, sans-serif">
    message goes here</font></td>
    </tr>
    </table></td>
    </tr>
    </table>
    <% }
    catch(SqlException e)
    {
    Response.Write(e.Message);
    }
    finally
    {
    conn.Close();
    }
    %>
    </body>
    </html>

    Thanks in advance.
     
    FC, Sep 27, 2005
    #1
    1. Advertising

  2. Hi,
    declare SqlConnection conn before the try block
    it will fix it.

    Cheers


    "FC" wrote:

    > Hello All:
    > I am getting the following error: Compiler Error Message: CS0246: The
    > type or namespace name 'conn' could not be found (are you missing a
    > using directive or an assembly reference?)
    >
    > This error point to the line conn.Close() on my finally statement. I am
    > pretty new at this and I did not find much on the web. Any help would
    > be greatly appreciated. My code is as follows:
    >
    > <%@ Page Language="C#" debug="true" %>
    > <%@ Import Namespace = "System.Data" %>
    > <%@ Import Namespace = "System.Data.SqlClient" %>
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > </head>
    > <body>
    > <%
    > string ConnString = "ommitted for security reasons"
    > try
    > {
    > string stmt = @"select * from gbook";
    > SqlConnection conn = null;
    > conn = new SqlConnection(ConnString);
    > conn.Open();
    > SqlCommand cmd = new SqlCommand(stmt,conn);
    > SqlDataReader reader = cmd.ExecuteReader();
    >
    > while ( reader.Read() )
    >
    > %>
    > <table width="790" border="0" align="center" cellpadding="3"
    > cellspacing="3">
    > <tr>
    > <td><table width="100%" border="1" cellspacing="3" cellpadding="3">
    > <tr>
    > <td width="40%"><strong><font size="2" face="Verdana, Arial,
    > Helvetica, sans-serif">
    > On
    > <%
    > Response.Write(reader.GetString(1));//this worked before I put the
    > HTML
    > //Response.Write(" ");
    > //Response.Write(reader.GetString(2));
    > %>
    > said:</font></strong></td>
    > <td width="60%"><font size="2" face="Verdana, Arial,
    > Helvetica, sans-serif">
    > message goes here</font></td>
    > </tr>
    > </table></td>
    > </tr>
    > </table>
    > <% }
    > catch(SqlException e)
    > {
    > Response.Write(e.Message);
    > }
    > finally
    > {
    > conn.Close();
    > }
    > %>
    > </body>
    > </html>
    >
    > Thanks in advance.
    >
    >
     
    =?Utf-8?B?YWxleDQ0NA==?=, Sep 27, 2005
    #2
    1. Advertising

  3. FC

    Pat Guest

    SqlConnection conn = new
    SqlConnection(ConfigurationSettings.AppSettings["YourConnectionStringKeyValu
    e"]);
    Try this it should help
    Patrick



    "FC" <> wrote in message
    news:...
    > Hello All:
    > I am getting the following error: Compiler Error Message: CS0246: The
    > type or namespace name 'conn' could not be found (are you missing a
    > using directive or an assembly reference?)
    >
    > This error point to the line conn.Close() on my finally statement. I am
    > pretty new at this and I did not find much on the web. Any help would
    > be greatly appreciated. My code is as follows:
    >
    > <%@ Page Language="C#" debug="true" %>
    > <%@ Import Namespace = "System.Data" %>
    > <%@ Import Namespace = "System.Data.SqlClient" %>
    > <html>
    > <head>
    > <title>Untitled Document</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > </head>
    > <body>
    > <%
    > string ConnString = "ommitted for security reasons"
    > try
    > {
    > string stmt = @"select * from gbook";
    > SqlConnection conn = null;
    > conn = new SqlConnection(ConnString);
    > conn.Open();
    > SqlCommand cmd = new SqlCommand(stmt,conn);
    > SqlDataReader reader = cmd.ExecuteReader();
    >
    > while ( reader.Read() )
    >
    > %>
    > <table width="790" border="0" align="center" cellpadding="3"
    > cellspacing="3">
    > <tr>
    > <td><table width="100%" border="1" cellspacing="3" cellpadding="3">
    > <tr>
    > <td width="40%"><strong><font size="2" face="Verdana, Arial,
    > Helvetica, sans-serif">
    > On
    > <%
    > Response.Write(reader.GetString(1));//this worked before I put the
    > HTML
    > //Response.Write(" ");
    > //Response.Write(reader.GetString(2));
    > %>
    > said:</font></strong></td>
    > <td width="60%"><font size="2" face="Verdana, Arial,
    > Helvetica, sans-serif">
    > message goes here</font></td>
    > </tr>
    > </table></td>
    > </tr>
    > </table>
    > <% }
    > catch(SqlException e)
    > {
    > Response.Write(e.Message);
    > }
    > finally
    > {
    > conn.Close();
    > }
    > %>
    > </body>
    > </html>
    >
    > Thanks in advance.
    >
     
    Pat, Sep 27, 2005
    #3
  4. FC

    FC Guest

    Thanks for the responses. I was missing the curly brackets for the
    while loop.
     
    FC, Sep 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ante Perkovic

    Re: reading sqldatareader after conn.close()

    Ante Perkovic, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    429
    Ante Perkovic
    Jul 2, 2003
  2. OLEDB conn giving error

    , May 20, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    687
  3. Simon Harris

    Conn.Close & Conn.Dispose

    Simon Harris, May 31, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    6,031
    Karl Seguin
    Jun 1, 2005
  4. JTL.zheng
    Replies:
    6
    Views:
    429
    JTL.zheng
    Nov 11, 2007
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    921
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page