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.
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.
Comments