executescalar in middle (be part) of transaction


Author
Message
Fabian R Silva, -
Fabian R Silva, -
StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)
Group: StrataFrame Users
Posts: 153, Visits: 1.2K
I need to execute a "executescalar" on a transaction to get the last invoice number again (if someone get the number that the actual invoice have... change it before save on this transaction)

I have:
...(try)...
BusinessLayer.TransactionBegin("invoiceTr", IsolationLevel.ReadCommitted);

var nextNumber = invBO.GetNextNumber(invBO.IdTalonario, DataSourceKey); // <--- THIS METHOD call a  EXECUTESCALAR VALUE.... freezes and timeout after some seconds...
invBO.Numero = invBO.HastaNumero = nextNumber;

saveUndoResult = invBO.Save(true);
...
...
...(rollback/commit)...
Edited 13 Years Ago by Fabian R Silva, -
Replies
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
The GetHastaNumeroProximo method should be in the ComprobantesBO or if you have a BO Base class, then create it in that class and pass the parametes of the fields to use in the SELECT MAX() statement.
I am not a C# developer, but here is the version I came up with:

        public static int GetHastaNumeroProximo(int idTalonario)
         {
             int result;
             using (var sqlCmd = new SqlCommand())
             {
                 // select (max)numero from comprobantes where IdTalonario= Comprobantes.IdTalonario and anulado=0
                 var sql = string.Format("select max({0}) from {1} where {2} = @valor", ComprobantesBOFieldNames.HastaNumero.ToString, this.TableNameAndSchema, ComprobantesBOFieldNames.IdTalonario.ToString);
                 sqlCmd.CommandTimeout = 0; // Added this to avoid taking to much time, also make sure your tables are properly indexed for the SQL condition
                 sqlCmd.CommandText = sql;
sqlCmd.Parameters.Add("@valor"SqlDbType.Int).Value = idTalonario; //Also noticed that this parameter should be Integer and not String as you had it originaly.
                 var res = this.ExecuteScalar(sqlCmd);
                 result = (res == null) || res == DBNull.Value ? 1 : ((int)res) + 1;
             } // end using
             return result;
         }



Edhy Rijo

Edited 13 Years Ago by Edhy Rijo
Fabian R Silva, -
Fabian R Silva, -
StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)
Group: StrataFrame Users
Posts: 153, Visits: 1.2K
-
Edited 13 Years Ago by Fabian R Silva, -
Fabian R Silva, -
Fabian R Silva, -
StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)StrataFrame User (317 reputation)
Group: StrataFrame Users
Posts: 153, Visits: 1.2K
I see that I was wrong on what I tried to accomplish, I will explain better.

I need to save invoices, and was searching better way of update the invoice number (each type have his own number)



I was planning to select max actual record and put that max+1, but if I do this the problem is that there is a posible repeat of number if it is not part of transaction.

¿can be a good idea to have a table like "InvoiceNumbers" that have a column "type" and a "number"?



Then on the current transaction I can fill the table InvoiceNumbers" adding +1 to actual number by the actual invoice type (¿this lock that table?)

then I read the current table InvoiceNumbers on transaction to set that number to the Invoice.



if another transaction try to get same invoice number and type on invoicenumbers, it will wait ?

thanks and sorry my lack of understanding about transactions, first time that have multiuser app that really need to be transactional and update some tables.
Edited 13 Years Ago by Fabian R Silva, -
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Fabian R Silva, - - 13 Years Ago
Edhy Rijo - 13 Years Ago
Fabian R Silva, - - 13 Years Ago
Edhy Rijo - 13 Years Ago
                     -
Fabian R Silva, - - 13 Years Ago
                         I see that I was wrong on what I tried to accomplish, I will explain...
Fabian R Silva, - - 13 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search