Share via

.Net Framework 8 Disposing off objects prematurely.

Anish Soman 20 Reputation points
2024-01-17T18:53:42.77+00:00

We have a web application that was initially developed in .Net 2.0 and now using a target framework 4.6.2. Hosting Server runs .Net 4.7.2. Our server is getting a refresh and the new server will now have .Net 4.8. While prepping for the migration, we found a weird issue. We have some methods where there are 2 db2 queries executed. In our architecture, we separated out the DB Agent into a different class and have all the functions there. Some places this class instance is not renewed for execution of a new query and the object gets disposed off after the first execution. This was not happening in 4.7.2. Main Method.

public string GetDbData(string str1)         
{             
	oDataAgent = new DB2ADO();             
	string sql = dbLiterals.sql1;             
	oDataAgent.parameters = null;             
	oDataAgent.addParameter("@param1", str1, DB2ADO.DbType.VarChar, DataAgent.ParameterDirection.Input, 10);
	DataSet dsSummary = oDataAgent.executeSQL(dbLiterals.DB2Connect, sql, "GetDbData1"); //This completed fine. 
	if (dsSummary.Tables[0].Rows.Count.Equals(0))             
	{                 
		throw new dbException(commonMsgs.docNotFoundZDW);             
	}              
	dsSummary.Clear();              
	oDataAgent.parameters = null;             
	sql = dbLiterals.sql2;             
	oDataAgent.addParameter("@param2", str1, DB2ADO.DbType.VarChar, DataAgent.ParameterDirection.Input, 10);
	dsSummary = oDataAgent.executeSQL(dbLiterals.DB2Connect, sql, "GetDbData2"); // Getting "Cannot use disposed object " exception here
	dsSummary.GetXml();              
	if (dsSummary.Tables[0].Rows.Count.Equals(0))             
	{                 
		throw new dbException(commonMsgs.docNotFoundZDW);   
	}             
	return "Complete";      
}

DB2ADO Class is attached as DB2ADO.cs.txt Too big to format here. DB2ADO.cs.txt

Developer technologies | .NET | Other

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.