Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I have written one data access layer code.

I have one query regarding same.

When i am trying to open connection to database it is showing as connection status as open.

But when connection i am closing connection of database i am getting bellow erroe msg for server version porperty

'con.ServerVersion' threw an exception of type 'System.InvalidOperationException'

below is my sample code


C#
private OracleConnection con = new OracleConnection();
        private DataSet ds = new DataSet();
        internal bool ERROR_FLAG = false;
       
       
        public Ora_DataAccess(string ConnectionString)
        {
            try
            {
                con.ConnectionString = ConnectionString;
                //con.ConnectionString = ConfigurationManager.ConnectionStrings[""].ToString();
            }
            catch (Exception ex)
            {
            }
        }


C#
public bool ExecuteProcedure(string Procedure, string[] ColumnNames, OracleDbType[] DataType, object[] Values)
{
    ERROR_FLAG = false;
    OracleCommand cmd = null;
    try
    {
        if (con.State != System.Data.ConnectionState.Open)
            con.Open();
        cmd = new OracleCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        for (int i = 0; i < ColumnNames.Length; i++)
        {
            cmd.Parameters.Add("@" + ColumnNames.GetValue(i).ToString(), (OracleDbType)DataType.GetValue(i));
            cmd.Parameters[i].Value = Values.GetValue(i);
        }
        cmd.Connection = con;
        cmd.CommandText = Procedure;
        if (cmd.ExecuteNonQuery() > 0)
        {
            cmd.Dispose();

            return (true);
        }
        else
        {
            cmd.Dispose();

            return (true);
        }
    }
    catch (Exception ex)
    {
        ERROR_FLAG = true;
        return (false);
    }
    finally
    {
        if (cmd != null)
            cmd.Dispose();
 
        if (con.State == ConnectionState.Open)
            con.Close();
        DataType = null;
        Values = null;
        ColumnNames = null;
    }
}
Posted
Updated 24-Nov-14 4:18am
v5
Comments
Tomas Takac 21-Nov-14 5:01am    
What database are you connecting to? i.e. What's the type of the connection object? Can you post a code sample where you declare and open the connection and access the property?
Pro86 21-Nov-14 5:27am    
yes sure below is my sample code
code:-

public bool ExecuteProcedure(string Procedure, string[] ColumnNames, OracleDbType[] DataType, object[] Values)
{
ERROR_FLAG = false;
OracleCommand cmd = null;
try
{
if (con.State != System.Data.ConnectionState.Open)
con.Open();
cmd = new OracleCommand();
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < ColumnNames.Length; i++)
{
cmd.Parameters.Add("@" + ColumnNames.GetValue(i).ToString(), (OracleDbType)DataType.GetValue(i));
cmd.Parameters[i].Value = Values.GetValue(i);
}
cmd.Connection = con;
cmd.CommandText = Procedure;
if (cmd.ExecuteNonQuery() > 0)
{
cmd.Dispose();

return (true);
}
else
{
cmd.Dispose();

return (true);
}
}
catch (Exception ex)
{
ERROR_FLAG = true;
return (false);
}
finally
{
if (cmd != null)
cmd.Dispose();

if (con.State == ConnectionState.Open)
con.Close();
DataType = null;
Values = null;
ColumnNames = null;
}
}
Tomas Takac 21-Nov-14 6:36am    
Please always update your question with additional information, do not post it as a comment. I did it for you this time, next time please use the "improve question" button.
Tomas Takac 21-Nov-14 6:47am    
What is the exact line where the exception is throw? con.Close()?
[no name] 21-Nov-14 7:04am    
Where have you declated Con ? Can post the code where "con" is declared

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900