Com Exception is unhandeled while executing ExecuteReader  
Author Message
Muhammd Jassim Munir





PostPosted: .NET Framework Data Access and Storage, Com Exception is unhandeled while executing ExecuteReader Top

hi

i get following exception when i execute my code

"COM object that has been separated from its underlying RCW cannot be used."

code is as follow:

private string conString;

OleDbDataReader dr;

OleDbDataAdapter da;

OleDbConnection con = new OleDbConnection();

OleDbCommand com = new OleDbCommand();

OleDbParameter id = new OleDbParameter();

OleDbParameter name = new OleDbParameter();

public bool IsCompanyExist(string namee)

{

bool exist = false;

strOleDb = ;

this.com.CommandText = strOleDb;

this.con.ConnectionString = this.conString;

this.com.Connection = con;

this.name.ParameterName = ;

this.name.Value = namee;

com.Parameters.Add(name);

try

{

con.Open();

dr = com.ExecuteReader(); <<<<< Here errror occurs

if (dr.Read())

{

exist = true;

}

else

{

exist = false;

}

con.Close();

dr.Close();

com.Parameters.Clear();

}

catch (OleDbException ex)

{

string s = ex.Message ;

}

return exist;

}

public int GetCompanyID(string namee)

{

int id = 0;

strOleDb = ;

com.CommandText = strOleDb;

con.ConnectionString = conString;

com.Connection = con;

this.name.ParameterName = ;

this.name.Value = namee;

com.Parameters.Add(this.name);

try

{

con.Open();

dr = com.ExecuteReader();<<<<<Here error comes

if (dr.Read())

{

id = dr.GetInt32(0);

}

}

catch (OleDbException ex)

{

string s = ex.Message;

}

con.Close();

dr.Close();

com.Parameters.Clear();

return id;

}

First time when this code executes it works fine...but second time when i hit the button to search the company it gives the exception....kindly can any one help mee




.NET Development31  
 
 
Torgeir F.





PostPosted: .NET Framework Data Access and Storage, Com Exception is unhandeled while executing ExecuteReader Top

Not sure if this helps here, but you should always put the con.Close() statement in a finally{} section to ensure the database connection closes.
 
 
alazela





PostPosted: .NET Framework Data Access and Storage, Com Exception is unhandeled while executing ExecuteReader Top

In general, the best practice is not to re-use connection, command and parameter objects when programming manually against the connected providers. Try re-writing it with the objects created just around the execute statements with this pattern:

using (OleDbConnection con = new OleDbConnection(this.conStr)) {
con.Open();
OleDbCommand cmd = new OleDbCommand(strOleDb, con);
cmd.Parameters.AddWithValue("<param name>", <value>);
using (OleDbDataReader rdr = cmd.ExecuteReader()) {
// process results as needed
}
}

This pattern should properly clean up any resources used between executions.



 
 
Guillote





PostPosted: .NET Framework Data Access and Storage, Com Exception is unhandeled while executing ExecuteReader Top

I had the same problem and i can solve it.
Try closing th datareader before closing the connection

You put:

con.Close();

dr.Close();


And must be:

dr.Close();

con.Close();