0
I am developing a service in c#.
- calls a function to check if it has active records.
- if you have any record call another function to select the data referring to the first query.
When I run my service it gives already open connection error.
So before I call each function I make one reader.Close();
and I no longer open the connection in the other functions. However I do not know if this is correct and if it would be the right way.
Step 1
public int VerifyStatus30(List<Class.ReturnTableName> tableInfo)
{
try
{
var countDis = 0;
using (MySqlConnection conn = DB.DatabaseConnection.getHSDBConnection())
{
conn.Open();
MySqlCommand command = new MySqlCommand("SELECT DataObj, RecId FROM HS_REGISTRIES WHERE ErrorCode = 0 AND HandleStatus = 30 ORDER BY UpdateDate30 ASC LIMIT 1", conn);
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
countDis = 1;
while (reader.Read())
{
int dataObj = reader.GetInt32(0);
int recId = reader.GetInt32(1);
reader.Close();
ClassTable.ReturnSapId returnSapId = new ClassTable.ReturnSapId();
returnSapId.GetSapId(recId, dataObj, tableInfo);
}
}
}
}
return countDis; // retorna a variável quantidade
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Step 2
public void GetSapId(int recId, int dataObj, List<Class.ReturnTableName> tableInfo)
{
try
{
Class.ReturnTableName result = tableInfo.Find(x => x.IdIntegraHardness == dataObj);
using (MySqlConnection conn = DB.DatabaseConnection.getHSDBConnection())
{
//conn.Open();
MySqlCommand command = new MySqlCommand("SELECT AbsEntry,u_D005_id FROM HS501_ONCM WHERE RecId = @recId", conn);
command.Parameters.AddWithValue("@tableName", result.TableIntegraHardness.Replace("'", ""));
command.Parameters.AddWithValue("@recId", recId);
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
int idSAP = reader.GetInt32(0);//ID SAP
int idHardness = reader.GetInt32(1);//ID HARDNESS
Class.UpdateStatus updateStatus = new Class.UpdateStatus();
reader.Close();
updateStatus.Update40(recId, 1);
SaveIdSH(idSAP,idHardness, tableInfo, dataObj);
updateStatus.Update50(recId, 1);
}
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
thanks for the reply I will change to what you suggested. So following the logic that was given, I would only need a Try{}catch{} at the beginning of everything that it is responsible for capturing the Exception and so on. Thank you
– Hejafe
It’s no more complex than this, I would suggest learning all these concepts and code tools before using them. Here on the site there are several things about exceptions. You’re trying to do something complex without understanding the basics, it doesn’t work and you’re going to make a lot of mistakes and you won’t even notice, because a lot of it works, but it’s still wrong. https://answall.com/questions/tagged/exce%C3%A7%C3%a3o? Sort=votes&pageSize=50
– Maniero
I understand I will research and study what you said. Grateful.
– Hejafe