Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I am developing a three tier project and in dataservices when I am using the following code for instert statement I am getting this error "Invalid operation. The connection is closed."

public static void AddNewInventory(string STU_NAME, string GPA, string COURSE, string identification, string datetime)
        {
OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("WHTS");

string sql = "Insert into I_Student (STU_ID, STU_NAME, GPA, COURSE, IDENTIFICATION, DATETIME,) Values (I_STU_ID.NEXTVAL, '" + stu_name.Replace("'", "''") + "', '" + GPA.Replace("'", "''") + "', '" + COURSE.Replace("'", "''") + "', '" + identification.Replace("'", "''") +                    " '"  + "', DATETIME('" + DATETIME + " 00:00:00', 'MM/DD/YYYY HH24:MI:SS')";
            Console.WriteLine("Insert SQL = " + sql.ToString());

using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))            
     {

     cmd.ExecuteNonQuery();
     }

     }



Can anyone help me to slove this error

Thanks
Posted
Updated 23-Jul-11 7:36am
v3

Is the connection closed?
You can always add some defensive coding - check if the connection is closed before executing the query, and if it is, reopen it.
 
Share this answer
 
Comments
rbjanaki 23-Jul-11 13:39pm    
But it is loading data into the forms when I am saving the data it is giving me this error. How can I reopen the connection?
[no name] 23-Jul-11 13:45pm    
try db.Open();
rbjanaki 23-Jul-11 14:03pm    
I tried that one and it is giving me this error Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase' does not contain a definition for 'open' and no extension method 'open' accepting a first argument of type 'Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase' could be found (are you missing a using directive or an assembly reference?
rbjanaki 23-Jul-11 13:54pm    
I tried that one and it is giving me this error Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase' does not contain a definition for 'open' and no extension method 'open' accepting a first argument of type 'Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase' could be found (are you missing a using directive or an assembly reference?
[no name] 23-Jul-11 15:11pm    
check this
http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.data.database.openconnection%28v=pandp.31%29.aspx
I'm just going to comment on the quality of the code, not fix the problem.

Scrap that string concatenated attrocity and replace it with a parameterized SQL query. Then use Parameter objects to fill in the blanks. It'll make your code FAR easier to support and avoid hard-to-find coding errors. You won't have to worry about replacing all the single quotes that may be in the data if you do.
 
Share this answer
 
Comments
Eslam Mostafa 24-Jul-11 2:05am    
what about my solution
Dave Kreskowiak 24-Jul-11 9:18am    
Who are you and WHAT solution?? You haven't posted anything in this entire discussion.

If you're just trying to get everyones attention to your solution to a different question, DON'T. You'll just end up pissing everyone off.
I don't use EL but in SQL the command object (cmd) has a connection and you should be able to check it's state and open the connection.

if(cmd.Connection.ConnectionState == ConnectionState.Closed)
 
Share this answer
 
hi,

I think the correct syntax should be something like below.

C#
using (DbCommand cmd= db.GetSqlStringCommand(sql))
{
    db.ExecuteNonQuery(cmd);
}


Here is an example on how to get DataSet, DataReader, single result

http://www.devx.com/dotnet/Article/30910/1954[^]
 
Share this answer
 
HI,
use try catch to handle this,

if the connection is open then leave it,otherwise open it,same apply in the other case also
 
Share this answer
 
MIDL
using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))
     {
        using(SqlConnection con=new SqlConnection(yourConnectionString)
        {
           //if you has Method or:
           //using(System.Data.Common.DbCommand cmd = db.GetSqlConnection(yourConnectionString))
           if(cmd.Connection.ConnectionState == ConnectionState.Closed)
           {
              con.Open();
           }
           md.ExecuteNonQuery();
        }
     }
 
Share this answer
 
C#
public static void AddNewInventory(string STU_NAME, string GPA, string COURSE, string identification, string datetime)
        {
OracleDatabase db = (OracleDatabase)DatabaseFactory.CreateDatabase("WHTS");

string sql = "Begin Insert into I_Student (STU_ID, STU_NAME, GPA, COURSE, IDENTIFICATION, DATETIME,) Values (I_STU_ID.NEXTVAL, '" + stu_name.Replace("'", "''") + "', '" + GPA.Replace("'", "''") + "', '" + COURSE.Replace("'", "''") + "', '" + identification.Replace("'", "''") +                    " '"  + "', DATETIME('" + DATETIME + " 00:00:00', 'MM/DD/YYYY HH24:MI:SS');End";
            Console.WriteLine("Insert SQL = " + sql.ToString());

using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql))
     {

     db.ExecuteNonQuery(cmd);
     }

     }


Finally I got it worked I just added begin and end to my sql statement
 
Share this answer
 

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