Jes Sie

Jes Sie

  • 699
  • 1.2k
  • 265.2k

Transactions and stored proc in SQL SERVER 2014

Apr 18 2017 3:33 AM
Can someone guide/show me how to make a transaction in sql server and how to call that in my code behind. The details are presented below. 
 
I created a stored procedure as shown below:
  1. ALTER PROCEDURE [dbo].[spInsertCashierSalesDetails_V2_From_Underwriting]  
  2.     -- Add the parameters for the stored procedure here  
  3.     (  
  4.             @ID int,  
  5.             @TransactionNumber nvarchar(50),  
  6.             @CustomerNo nvarchar(50),  
  7.             @CustomerName nvarchar(50),  
  8.             @PolicyCINo nvarchar(50),  
  9.             @MotorCINo nvarchar(50),  
  10.             @ProductType nvarchar(50),  
  11.             @COA_Code nvarchar(50),  
  12.             @Debit decimal(18,2),  
  13.             @Credit decimal(18,2),  
  14.             @TransactionType nvarchar(50),  
  15.             @AgentID nvarchar(50),  
  16.             @Username nvarchar(50))  
  17. AS  
  18. BEGIN  
  19.     -- SET NOCOUNT ON added to prevent extra result sets from  
  20.     -- interfering with SELECT statements.  
  21.     SET NOCOUNT ON;  
  22.   
  23.     -- Insert statements for procedure here  
  24.     INSERT INTO [dbo].[CashierSalesDetails_V2]  
  25.            (  
  26.             [TransactionNumber]  
  27.             ,[CustomerNo]  
  28.             ,[CustomerName]  
  29.             ,[PolicyCINo]  
  30.             ,[MotorCINo]  
  31.             ,[ProductType]  
  32.             ,[COA_Code]  
  33.             ,[Debit]  
  34.             ,[Credit]  
  35.             ,[TransactionType]  
  36.             ,[AgentID]  
  37.             ,[Username])  
  38.      VALUES  
  39.            (  
  40.            @TransactionNumber,   
  41.            @CustomerNo,   
  42.            @CustomerName,   
  43.            @PolicyCINo,  
  44.            @MotorCINo,   
  45.            @ProductType,   
  46.            @COA_Code,   
  47.            @Debit,   
  48.            @Credit,   
  49.            @TransactionType,  
  50.            @AgentID,   
  51.            @Username)  
  52. END  
In my code behind in C#, I called this sp
  1. public int InsertCashierDetailsv2_FromUnderwriting(CashierDetailsTableV2 cashierDetailsTable)  
  2.         {  
  3.             using (SqlConnection con = DBConnection.GetDbCon())  
  4.             {  
  5.                 SqlCommand cmd = new SqlCommand("spInsertCashierSalesDetails_V2_From_Underwriting", con);  
  6.                 cmd.CommandType = CommandType.StoredProcedure;  
  7.                 con.Open();  
  8.   
  9.                 try  
  10.                 {  
  11.                     cmd.Parameters.AddWithValue("@ID", cashierDetailsTable.ID);  
  12.                     cmd.Parameters.AddWithValue("@TransactionNumber", cashierDetailsTable.TransactionNumber);  
  13.                     cmd.Parameters.AddWithValue("@CustomerNo", cashierDetailsTable.CustomerNo);  
  14.                     cmd.Parameters.AddWithValue("@CustomerName", cashierDetailsTable.CustomerName);  
  15.                     cmd.Parameters.AddWithValue("@PolicyCINo", cashierDetailsTable.PolicyCINo ?? (object)DBNull.Value);   
  16.                     cmd.Parameters.AddWithValue("@MotorCINo", cashierDetailsTable.MotorCINo ?? (object)DBNull.Value);   
  17.                     cmd.Parameters.AddWithValue("@ProductType", cashierDetailsTable.ProductType);  
  18.                     cmd.Parameters.AddWithValue("@COA_Code", cashierDetailsTable.COA_Code);  
  19.                     cmd.Parameters.AddWithValue("@Debit", cashierDetailsTable.Debit);  
  20.                     cmd.Parameters.AddWithValue("@Credit", cashierDetailsTable.Credit);  
  21.                     cmd.Parameters.AddWithValue("@TransactionType", cashierDetailsTable.TransactionType);  
  22.                     cmd.Parameters.AddWithValue("@AgentID", cashierDetailsTable.AgentID);  
  23.                     cmd.Parameters.AddWithValue("@Username", cashierDetailsTable.Username);  
  24.   
  25.                     return cmd.ExecuteNonQuery();  
  26.                 }  
  27.                 catch  
  28.                 {  
  29.                     throw;  
  30.                 }  
  31.                 finally  
  32.                 {  
  33.                     cmd.Dispose();  
  34.                     con.Close();  
  35.                     con.Dispose();  
  36.                 }  
  37.             }  
  38.         }  
I also created 4 methods to insert almost the same data except for the COA_Code, Debit and Credit as shown below:
  1. private void InsertCashierDetailsCOAAR()  
  2.       {  
  3.           CashierDetailsTableV2 cashierDetails = new CashierDetailsTableV2();  
  4.           CashierDetailsDA insertCashierDetails = new CashierDetailsDA();  
  5.           try  
  6.           {  
  7.               cashierDetails.TransactionNumber = lblTransactionNumber.Text;  
  8.               cashierDetails.CustomerNo = txtCustomerNo.Text;  
  9.               cashierDetails.CustomerName = txtCustomerName.Text;  
  10.               cashierDetails.MotorCINo = ddlMotorCI.SelectedValue;  
  11.               cashierDetails.ProductType = txtProductType.Text;  
  12.               cashierDetails.COA_Code = ddlCOAAR.SelectedItem.Text;  
  13.               cashierDetails.Debit = Convert.ToDecimal(txtTotalPrem.Text); //insurance premium  
  14.               cashierDetails.TransactionType = lblTransactionType.Text;  
  15.               cashierDetails.AgentID = lblAgentCode.Text;  
  16.               cashierDetails.Username = lblUserName.Text;  
  17.   
  18.               insertCashierDetails.InsertCashierDetailsv2_FromUnderwriting(cashierDetails);  
  19.   
  20.               InsertCashierDetailsCOAIP();  
  21.           }  
  22.           catch (Exception ex)  
  23.           {  
  24.               throw ex;  
  25.           }  
  26.   
  27.       }  
  28.       private void InsertCashierDetailsCOAIP()  
  29.       {  
  30.           CashierDetailsTableV2 cashierDetails = new CashierDetailsTableV2();  
  31.           CashierDetailsDA insertCashierDetails = new CashierDetailsDA();  
  32.           try  
  33.           {  
  34.               cashierDetails.TransactionNumber = lblTransactionNumber.Text;  
  35.               cashierDetails.CustomerNo = txtCustomerNo.Text;  
  36.               cashierDetails.CustomerName = txtCustomerName.Text;  
  37.               cashierDetails.MotorCINo = ddlMotorCI.SelectedValue;  
  38.               cashierDetails.ProductType = txtProductType.Text;  
  39.               cashierDetails.COA_Code = ddlCOAIP.SelectedItem.Text;  
  40.               cashierDetails.Credit = Convert.ToDecimal(txtNetPremium2.Text); //insurance premium  
  41.               cashierDetails.TransactionType = lblTransactionType.Text;  
  42.               cashierDetails.AgentID = lblAgentCode.Text;  
  43.               cashierDetails.Username = lblUserName.Text;  
  44.   
  45.               insertCashierDetails.InsertCashierDetailsv2_FromUnderwriting(cashierDetails);  
  46.   
  47.               InsertCashierDetailsCOAOT();  
  48.           }  
  49.           catch (Exception ex)  
  50.           {  
  51.               throw ex;  
  52.           }  
  53.       }  
  54.       private void InsertCashierDetailsCOAOT()  
  55.       {  
  56.           CashierDetailsTableV2 cashierDetails = new CashierDetailsTableV2();  
  57.           CashierDetailsDA insertCashierDetails = new CashierDetailsDA();  
  58.           try  
  59.           {  
  60.               cashierDetails.TransactionNumber = lblTransactionNumber.Text;  
  61.               cashierDetails.CustomerNo = txtCustomerNo.Text;  
  62.               cashierDetails.CustomerName = txtCustomerName.Text;  
  63.               cashierDetails.MotorCINo = ddlMotorCI.SelectedValue;  
  64.               cashierDetails.ProductType = txtProductType.Text;  
  65.               cashierDetails.COA_Code = ddlCOAOT.SelectedItem.Text;  
  66.               cashierDetails.Credit = Convert.ToDecimal(txtVat.Text); // vat  
  67.               cashierDetails.TransactionType = lblTransactionType.Text;  
  68.               cashierDetails.AgentID = lblAgentCode.Text;  
  69.               cashierDetails.Username = lblUserName.Text;  
  70.   
  71.               insertCashierDetails.InsertCashierDetailsv2_FromUnderwriting(cashierDetails);  
  72.   
  73.               InsertCashierDetailsCOARF();  
  74.           }  
  75.           catch (Exception ex)  
  76.           {  
  77.               throw ex;  
  78.           }  
  79.       }  
  80.       private void InsertCashierDetailsCOARF()  
  81.       {  
  82.           CashierDetailsTableV2 cashierDetails = new CashierDetailsTableV2();  
  83.           CashierDetailsDA insertCashierDetails = new CashierDetailsDA();  
  84.           try  
  85.           {  
  86.               cashierDetails.TransactionNumber = lblTransactionNumber.Text;  
  87.               cashierDetails.CustomerNo = txtCustomerNo.Text;  
  88.               cashierDetails.CustomerName = txtCustomerName.Text;  
  89.               cashierDetails.MotorCINo = ddlMotorCI.SelectedValue;  
  90.               cashierDetails.ProductType = txtProductType.Text;  
  91.               cashierDetails.COA_Code = ddlCOARF.SelectedItem.Text;  
  92.               cashierDetails.Credit = Convert.ToDecimal(txtRegistry.Text); // registry fee  
  93.               cashierDetails.TransactionType = lblTransactionType.Text;  
  94.               cashierDetails.AgentID = lblAgentCode.Text;  
  95.               cashierDetails.Username = lblUserName.Text;  
  96.   
  97.               insertCashierDetails.InsertCashierDetailsv2_FromUnderwriting(cashierDetails);  
  98.   
  99.               InsertCashierHeader();  
  100.           }  
  101.           catch (Exception ex)  
  102.           {  
  103.               throw ex;  
  104.           }  
  105.       }  
I know that there is a better way of doing this. Thanks in advance. 

Answers (4)