0

I am working on Inventory management system .I wanted to update two tables just after inserting into one table so i have done it using SQL After insert trigger but this trigger is not firing from C# which is the front end of my application.I dont know how to fire a trigger from C#.Can anyone help me as i have to submit this project by coming Sunday? Here is my SQL trigger..

CREATE TRIGGER tbl_Sales_ForInsert
ON SALES 
FOR INSERT
AS
BEGIN



  DECLARE @ITEMMODEL varchar(100)
      SELECT @ITEMMODEL = @ITEMMODEL FROM inserted
      update SALES set PROFIT=TOTAL_PRICE - (SELECT QUANTITY FROM SALES WHERE ITEM_MODEL=@ITEMMODEL) * (SELECT RATE FROM ITEM_DETAILS WHERE ITEM_MODEL=@ITEMMODEL) WHERE ITEM_MODEL=@ITEMMODEL
      UPDATE ITEM_DETAILS SET QUANTITY=QUANTITY-(SELECT QUANTITY FROM SALES WHERE ITEM_MODEL=@ITEMMODEL) WHERE ITEM_MODEL=@ITEMMODEL
      UPDATE ITEM_DETAILS SET AMOUNT = AMOUNT - (SELECT RATE FROM ITEM_DETAILS WHERE ITEM_MODEL=@ITEMMODEL) * (SELECT QUANTITY FROM SALES WHERE ITEM_MODEL=@ITEMMODEL) where ITEM_MODEL=@ITEMMODEL
    END
3
  • 1
    And this triggers just fine if you insert a new record in the server? Commented Feb 18, 2014 at 21:01
  • 2
    I'd check if your trigger fires is you do an insert manually. Don't try to attempt everything at once. Isolate the problem and then move on. Commented Feb 18, 2014 at 21:09
  • Perhaps something that might affect others is that BulkCopy will not fire triggers by default for performance reasons. See here. Commented Nov 14, 2018 at 15:48

3 Answers 3

1

Trigger should fire regardless of client. You should try to profile the executing Sql with Sql Profiler. Also, try executing your TSQL code directly in Management Studio and see what happens.

Sign up to request clarification or add additional context in comments.

Comments

0

One approach is to test your trigger in your SQL database first. Create the trigger using your script (which looks to be correct) then add some data to the Sales table and see if the data in the Sales and item_details tables are updated. Once you know that the trigger is working and populating correctly, then work on the call for C# to insert new records into your Sales table. The trigger will fire automatically once you insert a new row into the Sales table.

Comments

0

Problem was @ITEMMODEL in the SELECT statement .I wrote :

 SELECT @ITEMMODEL = ITEM_MODEL FROM inserted

and it is working fine now .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.