Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

I have a method which is storing values in database and after that it is print invoice. I'm printing a form for invoice printing. I want to make sure that if it's saving it should print. Otherwise rollback all chanages. Following is syntex of the method.
C#
private void DatabaseAddedUpdateBarcodeSalesToday()
    {
        sqlConnection.Open();
        sqlTransaction = sqlConnection.BeginTransaction();
        try
        {
            AddToDatabase();
            sqlTransaction.Commit();
        }
        catch (Exception exp)
        {
            MesageBox.Show(exp.Message.ToString(), "Transaction Failed");
            sqlTransaction.Rollback();
        }
    }


AddToDatabase is one form1 and I am using sqlConnection on this. While UpdateBarcode is on form 2 and using different sqlConnection on that form.

I have multiple methods in AddToDatabase

1: GenerateBarcodeTemp("Barcode")-> Getting next avaialble barcode sequence stored
2: GetCustomerType()-> Get current customer type
3: PrintInvoice()-> Difference form printing has multiple methods
4: UpdateQty(qty, id)-> Updating quantity of the customer

On Invoice form there are multiple methods as well...

1: VATChecker() ---> Check the value of VAT
2: UpdateBarcode(string p, string strI) ---> Updating Sales using Barcod

All methods should use one transation to fulfill my requirements.
C#
private void AddToDatabase()
{
    sqlCommand = new SqlCommand("Insert Into Sales(barcode_id,customer_id, pos_id,qty)values(@barcode_id,@customer_id, 1,@qty); 
    sqlCommand.Parameters.AddWithValue("@barcode_id", temp_barcode);
    sqlCommand.Parameters.AddWithValue("@customer_id", 1);
    sqlCommand.Parameters.AddWithValue("@qty", 1);
    sqlDataReader = sqlCommand.ExecuteReader();
    sqlDataReader.Close();
  }

Here is PrintInvoice Method
C#
private void printInvoice()
{
       Invoice invi = new Invoice();
       invi.invi_barcode = temp_barcode;
       invi.user_id = userId;
       invi.Show();
}

On the other hand in Invoice I have a method which is updating sales table during same transaction. On that method my program is hanged and giving me timeout error. Here is method on Invoice
C#
private void UpdateBarcode(string p, string strI){
                try{
                     sqlCommandInvoice = new SqlCommand("Update Sales Set mgroupId = @mGroup Where Barcode_Id = @Barcode_Id", sqlConnectionInvoice);
                     sqlCommandInvoice.Parameters.AddWithValue("@mGroup", strI);
                     sqlCommandInvoice.Parameters.AddWithValue("@Barcode_Id", p);
        
                     sqlConnectionInvoice.Open();
                     sqlDataReaderInvoice = sqlCommandInvoice.ExecuteReader();
                     sqlDataReaderInvoice.Close();
                     sqlConnectionInvoice.Close();
                }
                 catch (Exception exp)
                 {
                     MessageBox.Show(exp.Message.ToString(), "Error");
                 }

My software is hanging On UpdatBarcode method on printInvoice. Any suggestion ?

What I have tried:

I want suggestion how to avoid transaction locks
Posted
Updated 29-Aug-21 3:16am

1 solution

This:
private void AddToDatabase()
{
    sqlCommand = new SqlCommand("Insert Into Sales(barcode_id,customer_id, pos_id,qty)values(@barcode_id,@customer_id, 1,@qty); 
    sqlCommand.Parameters.AddWithValue("@barcode_id", temp_barcode);
    sqlCommand.Parameters.AddWithValue("@customer_id", 1);
    sqlCommand.Parameters.AddWithValue("@qty", 1);
    sqlDataReader = sqlCommand.ExecuteReader();
    sqlDataReader.Close();
  }

does NOT insert data into database, due to usage of incorrect method. If you would like to insert data, use SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient)[^] instead. See:

sqlCommand.ExecuteNonQuery(); //inserts/updates data and returns number of rows affected


In the same way you have to resolve issue in UpdateBarcode() method.

Note, that ExecuteReader method[^] is used to read data, NOT to write (insert/update).
 
Share this answer
 
v2
Comments
Jaa Zaib 13-Mar-21 8:17am    
Thanks for your response. Will it fix my problem ?
Maciej Los 13-Mar-21 10:35am    
Have you tried?
Jaa Zaib 14-Mar-21 1:04am    
Yes. I did. Changed all to ExecuteNonQuery() for Update/Insert.

Did not solve my issue of Transaction Lock

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900