C
C#3d ago
Wasted

✅ Random error only when app is run on same server as database

I made a simple dotnet application that uses a remote database When I run it on my local computer it works flawlessly, I dont see the error and the application works When I run it on the same server as the database half of the time I get errors half the time it works correctly with no error I was told the connection needs to be closed but I dont think I did that correctly as I still get the same errors The errors I get are This method may not be called when another read operation is pending. or Connection must be valid and open. or Object reference not set to an instance of an object. This is most common, the save does appear to work as the database gets updated but the app shows this error This is the database class
namespace DataLibrary
{
public class DataAccess : IDataAccess
{
public async Task<List<T>> LoadData<T, U>(string sql, U parameters, string connectionString)
{
using (IDbConnection connect = new MySqlConnection(connectionString))
{
var rows = await connect.QueryAsync<T>(sql, parameters);
connect.Close();
return rows.ToList();
}
}
public Task SaveData<T>(string sql, T parameters, string connectionString)
{
using (IDbConnection connect = new MySqlConnection(connectionString))
{
var value = connect.ExecuteAsync(sql, parameters);
connect.Close();
return value;

}
}
}
}
namespace DataLibrary
{
public class DataAccess : IDataAccess
{
public async Task<List<T>> LoadData<T, U>(string sql, U parameters, string connectionString)
{
using (IDbConnection connect = new MySqlConnection(connectionString))
{
var rows = await connect.QueryAsync<T>(sql, parameters);
connect.Close();
return rows.ToList();
}
}
public Task SaveData<T>(string sql, T parameters, string connectionString)
{
using (IDbConnection connect = new MySqlConnection(connectionString))
{
var value = connect.ExecuteAsync(sql, parameters);
connect.Close();
return value;

}
}
}
}
15 Replies
Wasted
WastedOP3d ago
I tried adding a 100mS delay but still get a random error Object reference not set to an instance of an object. And is still works correctly on my local computer with the remote database
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ2d ago
What is the type of the rows variable? Also not awaiting the ExecuteAsync
Wasted
WastedOP2d ago
My c# skills are not that good I couldn't work out how to remove await What do youmean by type of rows available? I've used it on different tables with different data and it works always when on a different computer from the database, but half time when on the same server as the database
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ20h ago
public Task SaveData<T>(string sql, T parameters, string connectionString)
public Task SaveData<T>(string sql, T parameters, string connectionString)
to
public async Task SaveData<T>(string sql, T parameters, string connectionString)
public async Task SaveData<T>(string sql, T parameters, string connectionString)
var value = connect.ExecuteAsync(sql, parameters);
var value = connect.ExecuteAsync(sql, parameters);
to
var value = await connect.ExecuteAsync(sql, parameters);
var value = await connect.ExecuteAsync(sql, parameters);
Not awaiting the connect.ExecuteAsync(sql, parameters) will just let it keep going and close the connection.
Wasted
WastedOP20h ago
II get this error for the return value; Cannot implicitly convert type 'int' to 'System.Threading.Tasks.Task'
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ20h ago
Change the return type to Task<int> if you want the affected row counts back Then further up the call stack you have to await SaveData()
Wasted
WastedOP19h ago
seems to work now what happens if I dont await SaveData?
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ19h ago
Why would you not want to await? If you just want to fire and forget then it will probably crash the app with an UnobservedTaskException if SaveData throws any exceptions and you don't prevent them from bubbling up.
Wasted
WastedOP19h ago
Do i need to explicitly close the connection?
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ19h ago
Depends on what IDisposable does for a MySqlConnection Looks like on dispose it will close it for you
Wasted
WastedOP19h ago
OK thanx, I'll just leave the dispose. What do I do with this thread when the issue is solved?
ǝǝʞǝǝʞ
ǝǝʞǝǝʞ19h ago
!close
Accord
Accord19h ago
Ask the thread owner or member with permission to close this!
Wasted
WastedOP19h ago
!close
Accord
Accord19h ago
Closed!
Want results from more Discord servers?
Add your server