Hi all, this my first post.
I'm newbie in C# and ASP.NET so be patient with me, I have a problem when declare object in the
try clause working in VS2010 Express:

ex.

protected void Names()
{
    string strQuery; 
    OleDbDataReader myReader;
    strQuery = "SELECT TOP 1 Name FROM Names";

    try
    {
        OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\\project\\Db.mdb");
        myConn.Open();

        OleDbCommand myComm = new OleDbCommand(strQuery, myConn);
        myReader = myComm.ExecuteReader();
    }
    catch (Exception er)
    {
        Response.Write("Error:" + er.Message);
    }
    finally
    {
        //found something
        if (myReader.HasRows)
        {
            myReader.Read();

            Session.Add("Name", myReader[0].ToString());

            myReader.Close();

            //reuse the myConn connection
            strQuery = "SELECT bla bla bla WHERE bla bla bla";
            OleDbCommand myComm2 = new OleDbCommand(strQuery, myConn);
            myReader = myComm2.ExecuteReader();

                    
            myConn.Close();
        }
    }
}

The myComm declared in the Try clause appear undeclared at rest of routine and underlined like error, I can't declare it outside Try clause because it's the action to catch error.
What's I did not understand ?

Thanks in advance.

Hi,

Declare the Connection object myConn before the try clause and initialize it within the try.

I suggest to change your code as follows:

protected void Names()
    {

        OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\\project\\Db.mdb");
        OleDbDataReader myReader = null;
        string strQuery = "SELECT TOP 1 Name FROM Names";
        try
        {

            myConn.Open();
            OleDbCommand myComm = new OleDbCommand(strQuery, myConn);
            myReader = myComm.ExecuteReader();
            if (myReader.HasRows)
            {
                myReader.Read();
                Session.Add("Name", myReader[0].ToString());
                myReader.Close();

                //reuse the myConn connection
                strQuery = "SELECT bla bla bla WHERE bla bla bla";
                OleDbCommand myComm2 = new OleDbCommand(strQuery, myConn);
                myReader = myComm2.ExecuteReader();
            }
        }
        catch (Exception er)
        {
            Response.Write("Error:" + er.Message);
        }
        finally
        {
            if (myReader != null)
            {

                myReader.Dispose();
            }

            if (myConn != null)
            {
                myConn.Close();
                myConn.Dispose();
            }
        }
    }

It's works well now, many thanks.
The only thing I had to do was to move a
'Response.Redirect("Page.aspx", false);'
from the Try bracket to the Finally bracket.

Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.