0

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.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Lemorlenny
0

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();
            }
        }
    }

Edited by Ramesh S: n/a

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.