Below method is getting used to fill Dataset.
if we are calling this method in synchronous way it is working fine.
But now we need to call this method in Asynchronous way.so what changes i need to do so that below method should work properly without any issue.
public DataSet Filldata(string ProcName, string TableName)
{
DataSet ds = new DataSet();
try
{
da = new SqlDataAdapter(ProcName, con);
if (con.State != ConnectionState.Open)
{
con.Open();
}
da.SelectCommand.CommandTimeout = 15000;
da.Fill(ds, TableName);
}
catch (Exception ex)
{
ErrorMsg = ex.Message.ToString();
HMISLogger.logger.Error(ex.Message.ToString() + " " + ProcName, ex);
}
finally
{
con.Close();
da.Dispose();
}
return ds;
}
You can declare the class level static object as below
private static object lockObject = new object();
And modify the your method as below , As Fill method takes care of connection open and close we can add lock statement before it.
public DataSet Filldata(string ProcName, string TableName)
{
DataSet ds = new DataSet();
try
{
da = new SqlDataAdapter(ProcName, con);
da.SelectCommand.CommandTimeout = 15000;
lock (lockObj)
{
da.Fill(ds, TableName);
}
}
catch (Exception ex) {
ErrorMsg = ex.Message.ToString();
HMISLogger.logger.Error(ex.Message.ToString() + " " + ProcName, ex);
}
finally {
con.Close();
da.Dispose();
}
return ds;
}