✅ 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
15 Replies
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 databaseWhat is the type of the rows variable? Also not awaiting the ExecuteAsync
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
to
to
Not awaiting the
connect.ExecuteAsync(sql, parameters)
will just let it keep going and close the connection.II get this error for the
return value;
Cannot implicitly convert type 'int' to 'System.Threading.Tasks.Task'
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()
seems to work now
what happens if I dont await SaveData?
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.
Do i need to explicitly close the connection?
Depends on what IDisposable does for a MySqlConnection
Looks like on dispose it will close it for you
OK thanx, I'll just leave the dispose.
What do I do with this thread when the issue is solved?
!close
Ask the thread owner or member with permission to close this!
!close
Closed!