Rollback in single stored procedure for forloop & insert query in SQL Server 2012

Hardik Parmar

I am new to rollback. I have six insert queries. I am executing all six queries after a button click.

But out of the six queries, two of the insert queries are in the for loop (in for loop I am iterating trough the repeater).

If any of the insert query giving exception then I want to do the rollback if all the insert queries are successfully executed then only I have to commit all of them.

Here's is my code:

string InsertInventory = "insert into RS_Inventory_Master (product_name,category_id,quick_overview,description,image,isactive,product_code,barcode, inventory_group_id,entry_date,update_date) values ('" + ProductName + "','" + category_ddl.SelectedValue + "','" + Quick_Overview + "','" + Desc + "','" + inventory_img + "','" + active + "','" + productcde_txt.Text + "','" + barcode_name + "', '" + brand_ddl.SelectedValue + "','" + DateTime.Now + "','" + DateTime.Now + "')";
cm.TableInsert(insertInventory);

string query = "insert into RS_Inventory_buying_master (product_code,buying_price,isactive,date,latest) values('" + productcde_txt.Text + "','" + BuyingPrice + "','1','" + DateTime.Now.ToString() + "','1')";
cm.TableInsert(query);

string qry = "insert into RS_Inventory_Selling_Master (product_code,selling_price,isactive,date,latest) values('" + productcde_txt.Text + "','" + SellingPrice + "','1','" + DateTime.Now.ToString() + "','1')";
cm.TableInsert(qry);

//string WeightU = weight_txt.Text.Replace(".", "");
string InsertWeight = "insert into RS_Inventory_Weight_Master (product_code,weight,latest,date,isactive,unit_id) values('" + productcde_txt.Text + "','" + ProductWeight + "','1','" + DateTime.Now.ToString() + "','1','" + unit_ddl.SelectedValue + "')";
cm.TableInsert(InsertWeight);

for (int j = 0; j < rpt_sup_lst.Items.Count; j++)
{
    CheckBox ck = rpt_sup_lst.Items[j].FindControl("vendor_ck") as CheckBox;
    HiddenField vendor_id = rpt_sup_lst.Items[j].FindControl("h_vendor_id") as HiddenField;
    HiddenField vendor_code = rpt_sup_lst.Items[j].FindControl("h_vendor_code") as HiddenField;

    if (ck.Checked == true)
    {
       string v_id = vendor_id.Value;
       string vc_id = vendor_code.Value;
       string insertVendor = "insert into RS_Inventory_Vendor_Mapping (vendor_id,product_code,vendor_code) values ('" + v_id + "','" + productcde_txt.Text + "','" + vc_id + "')";
       cm.TableInsert(insertVendor);
    }
 }

 for (int i = 0; i < rpt_tax_lst.Items.Count; i++)
 {
     CheckBox ck = rpt_tax_lst.Items[i].FindControl("tax_chk") as CheckBox;
     HiddenField tax_id = rpt_tax_lst.Items[i].FindControl("h_tax_id") as HiddenField;
     HiddenField tax_code = rpt_tax_lst.Items[i].FindControl("h_taxcde_id") as HiddenField;

     if (ck.Checked == true)
     {
         string t_id = tax_id.Value;
         string tc_id = tax_code.Value;

         string insertTax = "insert into RS_Inventory_Tax_Mapping (tax_id,product_code,tax_code) values ('" + t_id + "','" + productcde_txt.Text + "','" + tc_id + "')";
         cm.TableInsert(insertTax);
      }
  }

How to achieve rollback for this?

Any help will be appreciated.

Thank you in advance.

Hardik Parmar

Finally I got the solution how to achieve rollback within one query.

Code behind(Asp .Net)

DataTable dt_vendor = new DataTable();
dt_vendor.Columns.Add("Vendor_id", typeof(int));
dt_vendor.Columns.Add("Product_code", typeof(string));
dt_vendor.Columns.Add("Vendor_code", typeof(string));


for (int j = 0; j < rpt_sup_lst.Items.Count; j++)
{

    CheckBox ck = rpt_sup_lst.Items[j].FindControl("vendor_ck") as CheckBox;
    HiddenField vendor_id = rpt_sup_lst.Items[j].FindControl("h_vendor_id") as HiddenField;
    HiddenField vendor_code = rpt_sup_lst.Items[j].FindControl("h_vendor_code") as HiddenField;

    if (ck.Checked == true)
    {
        string v_id = vendor_id.Value;
        string vc_id = vendor_code.Value;
        dt_vendor.Rows.Add(v_id, productcde_txt.Text, vc_id);
        //string insertVendor = "insert into RS_Inventory_Vendor_Mapping (vendor_id,product_code,vendor_code) values ('" + v_id + "','" + productcde_txt.Text + "','" + vc_id + "')";
        //cm.TableInsert(insertVendor);
    }
}
DataTable dt_tax = new DataTable();
dt_tax.Columns.Add("Tax_id", typeof(int));
dt_tax.Columns.Add("Product_code", typeof(string));
dt_tax.Columns.Add("Tax_code", typeof(string));

for (int i = 0; i < rpt_tax_lst.Items.Count; i++)
{

    CheckBox ck = rpt_tax_lst.Items[i].FindControl("tax_chk") as CheckBox;
    HiddenField tax_id = rpt_tax_lst.Items[i].FindControl("h_tax_id") as HiddenField;
    HiddenField tax_code = rpt_tax_lst.Items[i].FindControl("h_taxcde_id") as HiddenField;

    if (ck.Checked == true)
    {
        string t_id = tax_id.Value;
        string tc_id = tax_code.Value;

        dt_tax.Rows.Add(t_id, productcde_txt.Text, tc_id);
        //string insertTax = "insert into RS_Inventory_Tax_Mapping (tax_id,product_code,tax_code) values ('" + t_id + "','" + productcde_txt.Text + "','" + tc_id + "')";
        //cm.TableInsert(insertTax);
    }
}

string  strasd = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection connection = new SqlConnection(strasd);
connection.Open();
SqlCommand cmd = new SqlCommand("spInsert_Add_Inventory", connection);
cmd.CommandType = CommandType.StoredProcedure;

//Pass table Valued parameter to Store Procedure
SqlParameter sqlParam = cmd.Parameters.AddWithValue("@tvp_vendor", dt_vendor);
sqlParam = cmd.Parameters.AddWithValue("@tvp_tax", dt_tax);

cmd.Parameters.AddWithValue("@product_name", productname_txt.Text);
cmd.Parameters.AddWithValue("@category_id", category_ddl.SelectedValue);
cmd.Parameters.AddWithValue("@quick_overview", quick_review_txt.Text);
cmd.Parameters.AddWithValue("@description", desc_txt.Content);
cmd.Parameters.AddWithValue("@image", inventory_img);
cmd.Parameters.AddWithValue("@isactive", act_chk.Checked);
cmd.Parameters.AddWithValue("@product_code", productcde_txt.Text);
cmd.Parameters.AddWithValue("@barcode", barcode_name);
cmd.Parameters.AddWithValue("@buying_price", buy_prce_txt.Text);
cmd.Parameters.AddWithValue("@selling_price", sell_prce_txt.Text);
cmd.Parameters.AddWithValue("@product_weight", weight_txt.Text);
cmd.Parameters.AddWithValue("@unit_id", unit_ddl.SelectedValue);
cmd.Parameters.AddWithValue("@inventory_group_id", brand_ddl.SelectedValue);

sqlParam.SqlDbType = SqlDbType.Structured;                
cmd.ExecuteNonQuery();
connection.Close();

SQL

CREATE TYPE Inventory_Tax_Mapping_Type AS TABLE 
(   
  Tax_id  int,
  Product_code VARCHAR(500),
  Tax_code VARCHAR(500)
);
GO


CREATE TYPE Inventory_Vendor_Mapping_Type AS TABLE 
(   
  Vendor_id  int,
  Product_code VARCHAR(500),
  Vendor_code VARCHAR(500)
);
GO




ALTER PROC spInsert_Add_Inventory
(
    @product_name VARCHAR(100),
    @category_id int,
    @quick_overview VARCHAR(5000),
    @description ntext,
    @image VARCHAR(20),
    @isactive bit,  
    @product_code VARCHAR(20),
    @barcode VARCHAR(20),
    @buying_price float,
    @selling_price float,
    @product_weight float,
    @unit_id int,
    @inventory_group_id int,    
    @tvp_tax Inventory_Tax_Mapping_Type READONLY,
    @tvp_vendor Inventory_Vendor_Mapping_Type READONLY
)
AS
BEGIN

    SET NOCOUNT ON

    BEGIN TRY

    BEGIN TRANSACTION

            --Logic for Inserting Inventory Master
            INSERT INTO RS_Inventory_Master 
            (product_name,category_id,quick_overview,[description],[image],isactive,product_code,barcode, inventory_group_id,entry_date,update_date) 
            VALUES
            (@product_name,@category_id,@quick_overview,@description,@image,@isactive,@product_code,@barcode,@inventory_group_id,GETDATE(),GETDATE())


            --Logic for Inserting Inventory Buying Master
            INSERT INTO RS_Inventory_buying_master 
            (product_code,buying_price,isactive,date,latest) 
            VALUES
            (@product_code,@buying_price,1,GETDATE(),1)

            --Logic For Selling Price
            INSERT INTO RS_Inventory_Selling_Master 
            (product_code,selling_price,isactive,date,latest) 
            values(@product_code,@selling_price,1,GETDATE(),1)

            --Logic for Inserting Product Weight
            INSERT INTO RS_Inventory_Weight_Master 
            (product_code,weight,latest,date,isactive,unit_id) 
            VALUES
            (@product_code,@product_weight,1,GETDATE(),1,@unit_id)


            --Logic For Inserting Tax values.
            --@tvp_tax is the Table valued parmameter In this parameter we are pasing whole table ie Whole forloop
            INSERT INTO RS_Inventory_Tax_Mapping            
            SELECT Product_code,Tax_id,Tax_code
            FROM @tvp_tax

            --Logic For Inserting Vendor values
            INSERT INTO RS_Inventory_Vendor_Mapping
            SELECT Product_code,Vendor_id,Vendor_code
            FROM @tvp_vendor

            COMMIT

    END TRY 

    BEGIN CATCH 

              DECLARE @ERROR_MSG NVARCHAR(MAX), @SEVERITY INT, @STATE INT
              SELECT @SEVERITY = ERROR_SEVERITY(), @STATE = ERROR_STATE()
              , @ERROR_MSG = ERROR_MESSAGE() + ' err src line: ' + CAST( ERROR_LINE() AS NVARCHAR(20)) + ' ' + ISNULL(ERROR_PROCEDURE(), '');           
               --@ERROR_MSG = ERROR_MESSAGE()
                ROLLBACK;
             -- RE-THROW EXCEPTION FOR DIAGNOSTIC VISIBILITY
              RAISERROR (@ERROR_MSG ,@SEVERITY, @STATE);        
              RETURN

    END CATCH   

END

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server 2012 Insert Stored Procedure for N parameters

From Dev

Stored procedure in SQL Server 2012

From Dev

SQL Server 2012 - Pass stored procedure name to another stored procedure

From Dev

How do you merge two different SQL Server 2012 database tables in single stored procedure?

From Dev

Using insert stored procedure with SQL Server

From Dev

Insert into @tablename in SQL Server stored procedure

From Dev

SQL Server stored procedure to insert in multiple tables

From Dev

SQL Server Stored Procedure to insert selected data

From Dev

SQL Server stored procedure to insert in multiple tables

From Dev

SQL Server : Insert cursor within stored procedure

From Dev

SQL Server insert table column to stored procedure

From Dev

how to verify insert & delete & update are done in single stored procedure of Ms Sql server

From Dev

SQL Server : Dynamic Query in Stored Procedure in

From Dev

Converting Stored Procedure into a query (SQL Server Compact)?

From Dev

SQL Server stored procedure for Insert runs, but does not insert values into the table

From Dev

Execute multiple statements and CTE in stored procedure in SQL Server 2012?

From Dev

How to give table name dynamically in stored procedure in SQL Server 2012

From Dev

How to create a select, and then an update stored procedure in SQL Server 2012

From Dev

tsql not firing (stored procedure via ssis) - SQL Server 2012

From Dev

How to give table name dynamically in stored procedure in SQL Server 2012

From Dev

Execute Stored Procedure with RollbackTrans using VBA and SQL Server 2012

From Dev

Insert into table values from single variable SQL stored procedure

From Dev

SQL Server 2012 Write Return Values of Stored Procedure to Cells using Stored Procedure

From Dev

SQL server : stored procedure

From Dev

SQL Server stored procedure display message after insert

From Dev

SQL Server stored procedure - check if ANY records exist, and insert if not

From Dev

Insert list of values into a SQL Server table with stored procedure

From Dev

SQL Server 2008: Insert variable into DML statements using Stored Procedure

From Dev

SQL Server Stored Procedure IF Exist Update Else Insert

Related Related

  1. 1

    SQL Server 2012 Insert Stored Procedure for N parameters

  2. 2

    Stored procedure in SQL Server 2012

  3. 3

    SQL Server 2012 - Pass stored procedure name to another stored procedure

  4. 4

    How do you merge two different SQL Server 2012 database tables in single stored procedure?

  5. 5

    Using insert stored procedure with SQL Server

  6. 6

    Insert into @tablename in SQL Server stored procedure

  7. 7

    SQL Server stored procedure to insert in multiple tables

  8. 8

    SQL Server Stored Procedure to insert selected data

  9. 9

    SQL Server stored procedure to insert in multiple tables

  10. 10

    SQL Server : Insert cursor within stored procedure

  11. 11

    SQL Server insert table column to stored procedure

  12. 12

    how to verify insert & delete & update are done in single stored procedure of Ms Sql server

  13. 13

    SQL Server : Dynamic Query in Stored Procedure in

  14. 14

    Converting Stored Procedure into a query (SQL Server Compact)?

  15. 15

    SQL Server stored procedure for Insert runs, but does not insert values into the table

  16. 16

    Execute multiple statements and CTE in stored procedure in SQL Server 2012?

  17. 17

    How to give table name dynamically in stored procedure in SQL Server 2012

  18. 18

    How to create a select, and then an update stored procedure in SQL Server 2012

  19. 19

    tsql not firing (stored procedure via ssis) - SQL Server 2012

  20. 20

    How to give table name dynamically in stored procedure in SQL Server 2012

  21. 21

    Execute Stored Procedure with RollbackTrans using VBA and SQL Server 2012

  22. 22

    Insert into table values from single variable SQL stored procedure

  23. 23

    SQL Server 2012 Write Return Values of Stored Procedure to Cells using Stored Procedure

  24. 24

    SQL server : stored procedure

  25. 25

    SQL Server stored procedure display message after insert

  26. 26

    SQL Server stored procedure - check if ANY records exist, and insert if not

  27. 27

    Insert list of values into a SQL Server table with stored procedure

  28. 28

    SQL Server 2008: Insert variable into DML statements using Stored Procedure

  29. 29

    SQL Server Stored Procedure IF Exist Update Else Insert

HotTag

Archive