Com Exception is unhandeled while executing ExecuteReader

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:

privatestring conString;

OleDbDataReader dr;

OleDbDataAdapter da;

OleDbConnection con =newOleDbConnection();

OleDbCommand com =newOleDbCommand();

OleDbParameter id =newOleDbParameter();

OleDbParameter name =newOleDbParameter();

publicbool IsCompanyExist(string namee)

{

bool exist =false;

strOleDb ="Select strCompanyName from tblCompanyMain where strCompanyName = @strCompanyName";

this.com.CommandText = strOleDb;

this.con.ConnectionString =this.conString;

this.com.Connection = con;

this.name.ParameterName ="@strCompanyName";

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;

}

publicint GetCompanyID(string namee)

{

int id = 0;

strOleDb ="Select intID from tblCompanyMain where strCompanyName = @strCompanyName";

com.CommandText = strOleDb;

con.ConnectionString = conString;

com.Connection = con;

this.name.ParameterName ="@strCompanyName";

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

[5234 byte] By [MuhammdJassimMunir] at [2007-12-25]
# 1
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.
TorgeirF. at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2

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.

alazela at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
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();


Guillote at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified