0

I want to read data from an oracle database and add that data in to a List. But when I run the application I'm getting the 'Invalid operation. The connection is closed.' error.

This is my code:

public class NewOtherCompanyMapper
{
    OtherCompany om;
    private Database db;
    private DbCommand cmd;
    private DbConnection con;

     public NewOtherCompanyMapper(OtherCompany om_temp)
    {
        om = om_temp;
        db = DatabaseFactory.CreateDatabase("NDA_generator");
    }

   public List<OtherCompany> getCompanyDetails()
    {
        List<OtherCompany> list = new List<OtherCompany>();
        OtherCompany oc = new OtherCompany();

        try
        {
            con = db.CreateConnection();
            con.Open();

            string query = "SELECT * FROM OtherCompanyData";
            cmd = db.GetSqlStringCommand(query);
            DbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                oc = new OtherCompany(reader["RegNumber"].ToString(), reader["ComName"].ToString(), reader["Country"].ToString(), reader["Address"].ToString(), reader["CoreBusi"].ToString());
                list.Add(oc);
            }

        }

        catch (Exception ex)
        {
            throw ex;
        }
       return list;
   }
 }

Can you please help me to solve this problem.

Madushi
  • 1
  • 2
  • 5
  • _"when I run the application I'm getting the 'Invalid operation"_ Perhaps you could be more specific. You are not even calling `getCompanyDetails`. – Tim Schmelter Mar 23 '15 at 14:00
  • did you debug? which line gives error? – btevfik Mar 23 '15 at 14:01
  • Since this is an ASP.NET question and you're using a custom connection-manager class, could it be that the connection is `static`? Then this is something for you: http://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren – Tim Schmelter Mar 23 '15 at 14:04
  • throws exception in catch block- @btevfik – Madushi Mar 23 '15 at 14:04
  • @Madushi: as an aside, don't use `throw ex;` but `throw;` to keep the original stack trace. Then you can see where it was triggered. Use the debugger to step through this method, one line after the other. – Tim Schmelter Mar 23 '15 at 14:07

1 Answers1

0

Try replacing this

cmd = db.GetSqlStringCommand(query);

with this:

cmd = con.CreateCommand();
cmd.CommandText = query;
Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24