Link to home
Start Free TrialLog in
Avatar of websss
websssFlag for Kenya

asked on

C# code to detect if sql server cannot save data due to environmental issues (server offline, out of space etc) - and NOT malformed data types

I need to detect a SQL error on inserting records to a table (for disaster recovery)
Last night the server ran out of space, and all incoming data was lost as we process it on the fly

What I need is to be able to detect certain conditions like disk full etc, and NOT stuff like datatype doesn't match (i.e. saving to int column as "bob")
We get a lot of data in wrong format, so i want to discard this

If the DB is inaccessible for whatever reason (resource/environment/timeout reasons etc), I need to re-insert it back into the microsoft message queue and try and process it again
this will cause an infinite loop until the server disk space issue is fixed/server is restarted/timeout no longer issue etc), ....basically we won't have lost any valid data

Last nights error was

The transaction log for database 'TelemetryData' is full due to 'ACTIVE_TRANSACTION'.   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteScalar()
   at ATPLDAL.SqlHelper.ExecuteScalar(SqlConnection connection, CommandType commandType, String commandText, SqlParameter[] commandParameters)
   at ATPLDAL.SqlHelper.ExecuteScalar(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters)
   at ......

Open in new window


I'm sure there are a bunch errors which match being unable to save data due to environment issues?
As previously said, a simple catch would NOT suffice because there are valid reasons we want to discard data, i'm only concerned with stuff like database not available, offline, space full etc

I've looked into sqlexceptions, and there seems to be a lot of Id's (found in SELECT * FROM sysmessages)
Does anyone have any code that will detect the above conditions?

i.e.
catch (SqlException e) when (e.Number == 2601)
{
   // Do something.
}

Open in new window


The issue is there are so many error codes, i don't want trial and error, and hoping someone has done this before?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Question has been abandoned by the OP. ste5an was first in with sound advice.