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:
- ALTER PROCEDURE [dbo].[spInsertCashierSalesDetails_V2_From_Underwriting]
-
- (
- @ID int,
- @TransactionNumber nvarchar(50),
- @CustomerNo nvarchar(50),
- @CustomerName nvarchar(50),
- @PolicyCINo nvarchar(50),
- @MotorCINo nvarchar(50),
- @ProductType nvarchar(50),
- @COA_Code nvarchar(50),
- @Debit decimal(18,2),
- @Credit decimal(18,2),
- @TransactionType nvarchar(50),
- @AgentID nvarchar(50),
- @Username nvarchar(50))
- AS
- BEGIN
-
-
- SET NOCOUNT ON;
-
-
- INSERT INTO [dbo].[CashierSalesDetails_V2]
- (
- [TransactionNumber]
- ,[CustomerNo]
- ,[CustomerName]
- ,[PolicyCINo]
- ,[MotorCINo]
- ,[ProductType]
- ,[COA_Code]
- ,[Debit]
- ,[Credit]
- ,[TransactionType]
- ,[AgentID]
- ,[Username])
- VALUES
- (
- @TransactionNumber,
- @CustomerNo,
- @CustomerName,
- @PolicyCINo,
- @MotorCINo,
- @ProductType,
- @COA_Code,
- @Debit,
- @Credit,
- @TransactionType,
- @AgentID,
- @Username)
- END
In my code behind in C#, I called this sp
- public int InsertCashierDetailsv2_FromUnderwriting(CashierDetailsTableV2 cashierDetailsTable)
- {
- using (SqlConnection con = DBConnection.GetDbCon())
- {
- SqlCommand cmd = new SqlCommand("spInsertCashierSalesDetails_V2_From_Underwriting", con);
- cmd.CommandType = CommandType.StoredProcedure;
- con.Open();
-
- try
- {
- cmd.Parameters.AddWithValue("@ID", cashierDetailsTable.ID);
- cmd.Parameters.AddWithValue("@TransactionNumber", cashierDetailsTable.TransactionNumber);
- cmd.Parameters.AddWithValue("@CustomerNo", cashierDetailsTable.CustomerNo);
- cmd.Parameters.AddWithValue("@CustomerName", cashierDetailsTable.CustomerName);
- cmd.Parameters.AddWithValue("@PolicyCINo", cashierDetailsTable.PolicyCINo ?? (object)DBNull.Value);
- cmd.Parameters.AddWithValue("@MotorCINo", cashierDetailsTable.MotorCINo ?? (object)DBNull.Value);
- cmd.Parameters.AddWithValue("@ProductType", cashierDetailsTable.ProductType);
- cmd.Parameters.AddWithValue("@COA_Code", cashierDetailsTable.COA_Code);
- cmd.Parameters.AddWithValue("@Debit", cashierDetailsTable.Debit);
- cmd.Parameters.AddWithValue("@Credit", cashierDetailsTable.Credit);
- cmd.Parameters.AddWithValue("@TransactionType", cashierDetailsTable.TransactionType);
- cmd.Parameters.AddWithValue("@AgentID", cashierDetailsTable.AgentID);
- cmd.Parameters.AddWithValue("@Username", cashierDetailsTable.Username);
-
- return cmd.ExecuteNonQuery();
- }
- catch
- {
- throw;
- }
- finally
- {
- cmd.Dispose();
- con.Close();
- con.Dispose();
- }
- }
- }
I also created 4 methods to insert almost the same data except for the COA_Code, Debit and Credit as shown below:
I know that there is a better way of doing this. Thanks in advance.