locked
Invalid operation. The connection is closed error RRS feed

  • Question

  • User-1506965535 posted
    With my below code , I get error as

    Invalid operation. The connection is closed.

    Here is the code

    [WebMethod]
    public static bool DoesDataExist(string proj, string surv)
    {
    OracleCommand commandrepeat1 = new OracleCommand("Select * from XXCUS.XXACL_PN_FARMING_MST where project=" + proj + " and Survey=" + surv + " ");
    string constr = ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString;
    OracleConnection con = new OracleConnection(constr);
    con.Close();
    con.Open();
    OracleDataReader drmax1;
    drmax1 = commandrepeat1.ExecuteReader();
    drmax1.Read();
    if (drmax1.HasRows)
    {
    con.Close();
    return true;
    }

    con.Close();
    return false;
    }
    Thursday, September 1, 2016 10:09 AM

Answers

  • User-1404113929 posted

    hi,

    use bellow code.

    using(OracleCommand commandrepeat1 = new OracleCommand("Select * from XXCUS.XXACL_PN_FARMING_MST where project=" + proj + " and Survey=" + surv + " "))

    {
    string constr = ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString;
    OracleConnection con = new OracleConnection(constr);
    OracleDataReader drmax1;

    con.open();
    drmax1 = commandrepeat1.ExecuteReader();
    drmax1.Read();

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 10:25 AM
  • User36583972 posted

    Hi Nadeem157,

    From your error message, you have some error in your connection code. I suggest you can use  Using statement when you operate the database.

    The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and free up memory. Its purpose is to free up the resources that we used inside the Using statement.

    You can refer the following tutorial about how to connect to Oracle from C#. Then, make a test on your side.

    Connecting to Oracle from C# / Winforms / Asp.net without tnsnames.ora:

    http://www.splinter.com.au/connecting-to-oracle-from-c-winforms-aspnet-w/

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2016 10:45 AM

All replies

  • User-1404113929 posted

    hi,

    use bellow code.

    using(OracleCommand commandrepeat1 = new OracleCommand("Select * from XXCUS.XXACL_PN_FARMING_MST where project=" + proj + " and Survey=" + surv + " "))

    {
    string constr = ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString;
    OracleConnection con = new OracleConnection(constr);
    OracleDataReader drmax1;

    con.open();
    drmax1 = commandrepeat1.ExecuteReader();
    drmax1.Read();

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 10:25 AM
  • User36583972 posted

    Hi Nadeem157,

    From your error message, you have some error in your connection code. I suggest you can use  Using statement when you operate the database.

    The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and free up memory. Its purpose is to free up the resources that we used inside the Using statement.

    You can refer the following tutorial about how to connect to Oracle from C#. Then, make a test on your side.

    Connecting to Oracle from C# / Winforms / Asp.net without tnsnames.ora:

    http://www.splinter.com.au/connecting-to-oracle-from-c-winforms-aspnet-w/

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 7, 2016 10:45 AM