none
ExecuteReader requires an open and available Connection. The connection's current state is closed. RRS feed

  • Question

  • Pls check what is the problem on my code, it says that the connection is closed:
     string strAccessConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                @"Data Source=C:\Users\Franz\Documents\Visual Studio 2008\Projects\SBC Load\SBC Load\SBC Load.accdb;" +
                "Persist Security Info=True;" +
                "User id = Admin;" +
                "Password =;";
     OleDbConnection conn = new OleDbConnection(strAccessConn);
     string strAccess = "Select * From Balance";
     OleDbCommand command = new OleDbCommand(strAccess, conn);
     OleDbDataReader reader = command.ExecuteReader();
     while (reader.Read())
     {
         textBoxSun.Text = reader.GetString(1);
     }
    

    Thursday, August 20, 2009 5:28 PM

Answers

  • ExecuteReader requires an ope connection. Use an conn.Open() and conn.Close() command as follows:
    string strAccessConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
               @"Data Source=C:\Users\Franz\Documents\Visual Studio 2008\Projects\SBC Load\SBC Load\SBC Load.accdb;" +
               "Persist Security Info=True;" +
               "User id = Admin;" +
               "Password =;";
    OleDbConnection conn = new OleDbConnection(strAccessConn);
    string strAccess = "Select * From Balance";
    OleDbCommand command = new OleDbCommand(strAccess, conn);
    conn.Open();
    OleDbDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        textBoxSun.Text = reader.GetString(1);
    }
    conn.Close();
    Thursday, August 20, 2009 6:28 PM
    Moderator