Dapper does not catch error when I try and insert a duplicate record. It however populates the first record of the result with the error message. E.g.
try
{
var result = Dapper.SqlMapper.Query(SCADConn, sql, d, null, true, 30, ct);
return result;
}
catch (Exception e)
{
throw (e);
}
When I run the above code the result variable contains a record with a count of 1 row with the following DapperRow
{{DapperRow, ErrorNumber = '2601', ErrorSeverity = '14', ErrorState = '1', ErrorProcedure = 'OrganizationAdd', ErrorLine = '55', ErrorMessage = 'Cannot insert duplicate key row in object 'dbo.Organization' with unique index 'IX_Organization_1'. The duplicate key value is (AAA Purchasing Pty (Ltd), 123123123123).'}}
What a stupid mistake. I returned a record with the error in stored proc error handling.
Related
I have this procedure that does a RAISEERROR and a return #tmp_cnt at the end. This RAISEERROR doesn't stop the procedure from executing as it should return the tmp_cnt as well. I use this in .NET and my code goes into the catch (SqlException e) part so this tmp_cnt doesn't get returned. This is the code for it
(string, int) result;
result.Item1 = null;
result.Item2 = -1;
try {
result.Item2 = await _context.Database.ExecuteSqlRawAsync("EXECUTE core.STORED_PROCEDURE", params);
} catch (SqlException e) {
foreach(SqlError error in e.Errors) {
if (error.Class > 10) {
result.Item1 = error.Message;
}
}
}
This way, I only get the error.Message while the result.Item2 remains -1 and I'm aware that this is a normal thing to do as this is what it should do. If I remove the try/catch part, the app throws an exception and code 500. The question I have is, is there a way to get both the RAISEERROR and the return from a stored procedure in .NET? This is the SQL part
IF #tmp_cnt < #ent_cnt
BEGIN
DECLARE #msg AS NVARCHAR(MAX) = CONCAT('Not all of the selected entities are eligible for change. Will be changed for ',
CAST(#tmp_cnt AS NVARCHAR(50)), ' out of the selected ', CAST(#ent_cnt AS NVARCHAR(50)), ' entities.')
RAISERROR(#msg, 15, 1)
RETURN #tmp_cnt;
END
If not possible, have you ever stumbled upon a scenario like this and is there a workaround for it?
I use CockroachDb and Npgsql driver.
I have a simple Users table. When I insert new record, every other query fails with syntax error, which seems bizarre to me.
CREATE TABLE Users (
RequestIdentifier BYTEA NOT NULL UNIQUE,
Identifier UUID PRIMARY KEY DEFAULT gen_random_uuid(),
Id INT8 NOT NULL DEFAULT unique_rowid(),
Email BYTEA NOT NULL UNIQUE,
Username BYTEA NOT NULL,
PasswordHash BYTEA NOT NULL
);
var q = #"
INSERT INTO Users (RequestIdentifier, Email, Username, PasswordHash)
VALUES (#RequestIdentifier, #Email, #Username, #PasswordHash)
ON CONFLICT (RequestIdentifier)
DO NOTHING
RETURNING Identifier
";
byte[] userIdentifier = null;
using (var cmd = new NpgsqlCommand(q, dbConn)) {
cmd.Parameters.Add("RequestIdentifier", NpgsqlDbType.Bytea);
cmd.Parameters.Add("Email", NpgsqlDbType.Bytea);
cmd.Parameters.Add("Username", NpgsqlDbType.Bytea);
cmd.Parameters.Add("PasswordHash", NpgsqlDbType.Bytea);
await cmd.PrepareAsync();
cmd.Parameters[0].Value = msg.RequestIdentifier;
cmd.Parameters[1].Value = msg.Email;
cmd.Parameters[2].Value = msg.Username;
cmd.Parameters[3].Value = passwordHash;
try {
userIdentifier = ((Guid) await cmd.ExecuteScalarAsync()).ToByteArray();
} catch (PostgresException e) when (e.SqlState == SqlErrorCodes.UniqueViolation) {
logger.Information("Email {Email} already in use", UTF8.GetString(msg.Email));
} catch (PostgresException e) {
logger.Error("{Exception}", e);
throw;
}
}
Npgsql.PostgresException (0x80004005): 42601: at or near "close": syntax error
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteScalar(Boolean async, CancellationToken cancellationToken)
at OwlAuthentication.Services.CockroachDbStorageService.CreateOrRetrieveExistingUser(SignUpMessage msg) in C:\Users\Che Khen Kho\Desktop\dotnet\OwlAuthentication\Services\CockroachDbStorageService.cs:line 94
Exception data:
Severity: ERROR
SqlState: 42601
MessageText: at or near "close": syntax error
Detail: source SQL:
CLOSE ALL
^
File: lexer.go
Line: 175
Routine: Error
If I try, say, 10 queries, 5 of them would fail with this exception, but quite often more than 5 rows would actually get inserted (sometimes 6, sometimes 8, etc.).
I tested it with PostgreSQL as well (using uuid_generate_v4 instead of gen_random_uuid and BIGSERIAL for Id column), and everything works fine.
I think this is covered by this issue (https://github.com/cockroachdb/cockroach/issues/45490) which is fixed in CockroachDB v20.1
Strange issue. I have a managed OPD.Net application that calls a stored procedure to insert records. When I call the procedure normally and rollback the transaction, the records are not saved to the table (duh!). When I use the procedure with parameter array binding and still roll back the transaction, the records ARE saved to the table. Somehow, the records are getting committed even though I do a rollback!
TEST Schema:
CREATE TABLE TEST
(
ID NUMBER(15,0),
VALUE VARCHAR2(50 CHAR)
)
/
CREATE SEQUENCE TEST_ID_SEQ
INCREMENT BY 1
START WITH 50
MINVALUE 1
MAXVALUE 999999999999999
NOCYCLE
NOORDER
CACHE 100
/
CREATE OR REPLACE PROCEDURE TEST_INSERT
(
iVALUE IN VARCHAR2,
oID OUT NUMBER
)
AS
BEGIN
oID := TEST_ID_SEQ.NEXTVAL;
INSERT INTO TEST
(
ID,
VALUE
)
VALUES
(
oID,
iVALUE
);
END;
/
Test Code:
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using System;
using System.Data;
namespace OdpTestArrayBinding
{
class Program
{
private const string cConnectioString = "Data Source=DB_DEV;User Id=TMP;Password=sqlsql";
static void Main(string[] args)
{
using (OracleConnection lConnectionA = new OracleConnection(cConnectioString))
{
lConnectionA.StateChange += ConnectionStateChanged;
lConnectionA.Open();
Console.WriteLine($"[Connection={lConnectionA.GetHashCode()}] Connection opened.");
int lStartCount = CountTestTableRows(lConnectionA);
Console.WriteLine($"[Connection={lConnectionA.GetHashCode()}] Number of rows in table at start is {lStartCount}.");
using (OracleTransaction lTransaction = lConnectionA.BeginTransaction())
{
Console.WriteLine($"[Connection={lConnectionA.GetHashCode()}] Transaction started.");
try
{
using (OracleCommand lCmd = new OracleCommand())
{
lCmd.Connection = lConnectionA;
lCmd.BindByName = true;
lCmd.CommandType = System.Data.CommandType.StoredProcedure;
lCmd.CommandText = "TEST_INSERT";
lCmd.Parameters.Add("iVALUE", OracleDbType.Varchar2, ParameterDirection.Input);
// The OracleDbType of the output does not seem to matter, the actual value is always OracleDecimal
lCmd.Parameters.Add("oID", OracleDbType.Int64, ParameterDirection.Output);
lCmd.ArrayBindCount = 3;
lCmd.Parameters["iVALUE"].Value = new string[] { "Foo", "Bar", "Boo" };
// Not required.
//lCmd.Parameters["oID"].Value = new long[] { -1, -1, -1 };
lCmd.ExecuteNonQuery();
OracleDecimal[] lOutIds = (OracleDecimal[])lCmd.Parameters["oID"].Value;
Console.WriteLine($"[Connection={lConnectionA.GetHashCode()}] Inserted 3 rows using stored procedure, out ID vales are {string.Join(",", lOutIds)}.");
}
ListRows(lConnectionA, lStartCount + 3);
using (OracleConnection lConnectionB = new OracleConnection(cConnectioString))
{
lConnectionB.StateChange += ConnectionStateChanged;
lConnectionB.Open();
Console.WriteLine($"[Connection={lConnectionB.GetHashCode()}] Connection opened.");
ListRows(lConnectionB, lStartCount);
}
}
finally
{
lTransaction.Rollback();
Console.WriteLine($"[Connection={lConnectionA.GetHashCode()}] Transaction rolled back.");
}
}
}
Console.WriteLine("Press the ENTER key to continue...");
Console.ReadLine();
}
private static void ConnectionStateChanged(object sender, StateChangeEventArgs e)
{
Console.WriteLine($"[Connection={sender.GetHashCode()}] State changed from {e.OriginalState} to {e.CurrentState}.");
}
private static int CountTestTableRows(OracleConnection aConnection)
{
using (OracleCommand lCmd = new OracleCommand())
{
lCmd.Connection = aConnection;
lCmd.BindByName = true;
lCmd.CommandType = System.Data.CommandType.Text;
lCmd.CommandText = "SELECT COUNT(*) FROM TEST";
return Convert.ToInt32(lCmd.ExecuteScalar());
}
}
private static void ListRows(OracleConnection aConnection, int aExpectedRowCount)
{
int lCount = CountTestTableRows(aConnection);
Console.Write($"[Connection={aConnection.GetHashCode()}] Number of rows in table {lCount}");
if (lCount == aExpectedRowCount)
Console.WriteLine(" (Test passed, actual and expected row count are the same).");
else
Console.WriteLine($" (Test FAILED!, expected {aExpectedRowCount} rows).");
}
}
}
First run console output:
[Connection=47973293] State changed from Closed to Open.
[Connection=47973293] Connection opened.
[Connection=47973293] Number of rows in table at start is 0.
[Connection=47973293] Transaction started.
[Connection=47973293] Inserted 3 rows using stored procedure, out ID vales are 50,51,52.
[Connection=47973293] Number of rows in table 3 (Test passed, actual and expected row count are the same).
[Connection=21040294] State changed from Closed to Open.
[Connection=21040294] Connection opened.
[Connection=21040294] Number of rows in table 3 (Test FAILED!, expected 0 rows).
[Connection=21040294] State changed from Open to Closed.
[Connection=47973293] Transaction rolled back.
[Connection=47973293] State changed from Open to Closed.
I have tried Oracle.ManagedDataAccess.dll 4.121.2.20141216 (ODAC RELEASE 3) and 4.121.2.20150926 (ODAC RELEASE 4), both give the same result. Any ideas or workarounds?
Can you try to change your code to use this example.
Open only a using block for your connection.
public void RunOracleTransaction(string connectionString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand command = connection.CreateCommand();
OracleTransaction transaction;
// Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
command.Transaction = transaction;
try
{
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
command.ExecuteNonQuery();
command.CommandText =
"INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
command.ExecuteNonQuery();
transaction.Commit();
Console.WriteLine("Both records are written to database.");
}
catch (Exception e)
{
transaction.Rollback();
Console.WriteLine(e.ToString());
Console.WriteLine("Neither record was written to database.");
}
}
}
Pleaase check your command : you have forget assign the transaction on your command?
lCmd.Connection = lConnectionA;
// Assign transaction to your command
lCmd.Transaction = lTransaction;
You should create a TransactionScope that wraps your database connection and within your TransactionScope, try enlisting in the ambient transaction:
<your connection object>.EnlistTransaction(Transaction.Current);
this is my trigger
CREATE OR REPLACE TRIGGER trg_cek_pengurus
BEFORE INSERT ON tbl_pengurus
FOR EACH ROW
DECLARE
v_cek NUMBER(2);
BEGIN
IF :NEW.jabatan = 1 OR :NEW.jabatan = 2 THEN
select count(id)
into v_cek
from tbl_pengurus
where idkoperasi = :NEW.idkoperasi and jabatan = :NEW.jabatan;
IF v_cek > 0 THEN
RAISE_APPLICATION_ERROR (-20000, 'kepengurusan sudah ada');
END IF;
END IF;
END;
/
nah.., if the trigger have a return value is good,:v but it doesn't :v
so I set RAISE_APPLICATION_ERROR
And this is my controller
public function actionTambahPengurus()
{
$model = new Pengurus();
if ($model->load(Yii::$app->request->post())){
$model->IDKOPERASI = Yii::$app->user->identity->ID;
if($model->save())
return $this->redirect('kepengurusan');
}
return $this->render('tambah-pengurus',[
'model' => $model,
]);
}
And then I get error
SQLSTATE[HY000]: General error: 20000 OCIStmtExecute: ORA-20000: kepengurusan sudah ada
ORA-06512: at "DB_KOPERASI.TRG_CEK_PENGURUS", line 13
ORA-04088: error during execution of trigger 'DB_KOPERASI.TRG_CEK_PENGURUS'
(ext\pdo_oci\oci_statement.c:148)
The SQL being executed was: INSERT INTO "TBL_PENGURUS" ("NIK", "NAMA", "JABATAN", "EMAIL", "TGL_LAHIR", "ALAMAT", "TELEPON", "IDKOPERASI") VALUES ('2110131041', 'Rahmat Heru Kurniawan', 1, 'rahmatheruka2#gmail.com2', '3 July, 2015', 'sidoarjo', '0987654321', 8) RETURNING "ID" INTO :qp8
this is good, because this tell me that trigger is working
but of course this is bad for my website.
So i want to handle this, I've tries various ways from google, but nothing works.
Please.. is there anyone who can help me?
As far as I remember, any database related error will throw yii\db\Exception. You can use standard try / catch block to handle that:
try {
...
} catch (\yii\db\Exception $e) {
...
}
My DBA created the following Stored Proc which he insists works fine when called in SQL Server:
CREATE procedure [dbo].[GetParentID]
#SSHIP_AppID as varchar(50),
#ParentID as varchar(150) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT #ParentID = a.iBuild_GUID
FROM dbo.XRef_iBuild_SSHIP as a
WHERE a.SSHIP_appId = #SSHIP_AppID
AND a.SSHIP_appId <> ''
END
I have created the following ADO.NET Wrapper but I am having trouble getting the output parameter. I keep getting back "OUTPUT" as its value:
private string GetParentId(string appId)
{
var connection = new SqlConnection();
string parentId = String.Empty;
try
{
connection.ConnectionString = "...)
var command = new SqlCommand("GetParentId", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("#SSHIP_AppID", appId));
command.Parameters.Add(new SqlParameter("#ParentID", ParameterDirection.Output));
connection.Open();
command.ExecuteNonQuery();
parentId = (command.Parameters["#ParentId"].Value).ToString();
}
catch (Exception ex)
{
LogError(appId, ex.ToString(), "Interface12 - Cannot get ParentId", null, 0);
}
finally
{
connection.Close();
}
return parentId;
}
}
What am I doing wrong?
In new SqlParameter("#ParentID", ParameterDirection.Output) the 2nd argument is treated as the object value argument and apparently converted to a string.
(This implicit conversion is, in my opinion, a design flaw in ADO.NET. It should throw an exception for any unknown input type.).
Choose a better overload.