Hi,
It's me again and another problem :).
I have written the code like this:
private: System::Void btnUpdate_Click(System::Object * sender,
System::EventArgs * e)
{
if(!ValidateData())
return ;
FbCommand * cmd = new FbCommand ;
if(IsNew)
cmd->CommandText = S"insert into kontrahenci
(kod_kontr,nazwa,typ,miasto,kod_poczt,adres,nip) values
(@kod_kontr,@nazwa,@typ,@miasto,@kod_poczt,@adres,@nip)" ;
else
cmd->CommandText= S" update kontrahenci set
nazwa=@nazwa,typ=@typ,miasto=@miasto,kod_poczt=@kod_poczt,adres=@adres,nip=@
nip "
S" where kod_kontr=@kod_kontr";
cmd->Parameters->Add("@kod_kontr",FbDbType::Char)->Value = txtKod->Text
;
cmd->Parameters->Add("@nazwa",FbDbType::Char)->Value = txtNazwa->Text ;
cmd->Parameters->Add("@typ",FbDbType::Char)->Value = cmbTyp->Text ;
cmd->Parameters->Add("@kod_poczt",FbDbType::Char)->Value =
txtKodPoczt->Text ;
cmd->Parameters->Add("@miasto",FbDbType::Char)->Value = txtMiasto->Text
;
cmd->Parameters->Add("@adres",FbDbType::Char)->Value = txtAdres->Text ;
cmd->Parameters->Add("@nip",FbDbType::Char)->Value = txtNIP->Text ;
try {
cmd->Connection = dbConn ;
// dbConn is a global application variable. This connection is
open at the begin of the programm and keep opened all the time.
cmd->Transaction = dbConn->BeginTransaction();
cmd->ExecuteNonQuery();
cmd->Transaction->Commit();
}catch(Exception * e){
cmd->Transaction->Rollback();
MessageBox::Show(e->Message);
return ;
}
When the data in the update or insert statement is correct it works ok.
But when there is an error, for example an exception in a trigger has been
fired, or a string in a field is too long
I get the exception and this is ok. The problem is that line
cmd->Transaction->Rollback();
cause an another exception "An unhandled exception of type
'FirebirdSql.Data.Firebird.FbException' occurred in
firebirdsql.data.firebird.dll. Additional information: Error reading data
from the connection."
I have tried to remove this line.
}catch(Exception * e){
// cmd->Transaction->Rollback();
MessageBox::Show(e->Message);
return ;
}
Now the line
MessageBox::Show(e->Message);
displays the message "operation was cancelled". It is wrong too because the
transaction is still active an any other
updates are impossible in may program.
By the way the message "operation is cancelled" is rather strange, it should
be a message from an exception because an exception
in a trigger has been fired in that case.
What is wrong in my code ?.
regards,
Janusz Mars
|