Database

LastError: What Went Wrong on the Last Call

GPALDatabase.LastError is null when the last call worked and a DatabaseError when it did not. It is cleared at the start of every call, so it always describes the most recent one. Number is the server's own error number, 0 when the failure did not come from the server. Message is what the failure said, Sql is the statement that was running, and Parameters is the grid of rows it was running against. Row is which of those rows was being written, or -1 when the failure was not on a row, and RowValues is that row's values so a handler does not have to index back into the grid. Occurred is when, and Exception is the exception itself for a caller that wants the stack.

WARNING

Nothing throws and the return value does not tell you, which matters most where a workflow is not watching: a logger writing to a database carries on quite happily with every row going nowhere. Read LastError after a phase of work rather than wrapping each call in a try.

Examples

GPAL Fluent: High-level fluent C# API

//Number is what a workflow branches on, since a server error number is stable where a message is not. 207 is an invalid column name, for example, which is a mistake in the SQL rather than in the data and is worth failing the run over.

GPAL.Database

.WithConnectionString(connStr)

.WithCreateAs("INSERT INTO Orders (CustomerId, Total) VALUES (@CustomerId, @Total)")

.ToGPALObject()

.Create

.WithIntegerParameter("CustomerId")

.WithDecimalParameter("Total")

.Using(ordersGrid)

.Execute(out int rowCount);


// Nothing threw. Ask whether it landed

if (null != ordersDb.LastError)

{

GPAL.PublishSimpleEvent(GPALEventType.FAILURE,

$"[{ordersDb.LastError.Number}] on row [{ordersDb.LastError.Row}] of [{ordersDb.LastError.Sql}]");


foreach (string value in ordersDb.LastError.RowValues ?? new List<string>())

GPAL.PublishSimpleEvent(GPALEventType.CAUTION, $" [{value}]");

}

💬 Ask GPAL