views:

257

answers:

3

Hi Gurus!

I have a wierd problem. I am having a web application developed in ASP.NET, the database is SQL Server Express 2005. I have written a trigger which updates another table's column value when a record is inserted or update.

The two tables are

  1. InvtVendorInvoices (The trigger is on this table, trigger name is OnAlter_VendorInvoices)

  2. InvtMaster (This table's coloumn "invtAmtOthers" value gets updated from the trigger on InvtVendorInvoices)

When I am inserting or updating a record from SQL Server Management Studio Express in InvtVendorInvoices table, the "invtAmtOthers" coloumn value in the InvtMaster table is being updated successfully. However, when I insert or update a record from an .aspx web page the trigger doesn't fires. The interesting thing is that the record which tried to inserted from the webpage gets inserted or update in the InvtVendorInvoices table but the trigger doesn't fire and the "invtAmtOther" coloumn in InvtMaster table is left unaltered.

I have done lot of search on the internet but couldn't find the solution.

Please help me out of this mess...

Any help will be highly appreciated.

Database Tables I have are...

USE [Stone]
GO
/****** Object:  Table [dbo].[InvtMaster]    Script Date: 12/10/2009 22:19:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[InvtMaster](
 [invtID] [int] IDENTITY(1,1) NOT NULL,
 [invtDate] [datetime] NULL,
 [invtSupID] [int] NULL,
 [invtRefNo] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtRefDate] [datetime] NULL,
 [invtPOID] [int] NULL,
 [invtCurID] [int] NULL,
 [invtPayTermsID] [int] NULL,
 [invtLocationID] [int] NULL,
 [invtNotesPrinted] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtNotesInternal] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtAmtMaterial] [money] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtAmtTotal]  DEFAULT ((0)),
 [invtAmtFreight] [money] NULL,
 [invtAmtOthers] [money] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtAmtOthers]  DEFAULT ((0)),
 [invtStatusID] [int] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtStatusID]  DEFAULT ((0)),
 [invtWflStatusID] [int] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtWflStatusID]  DEFAULT ((0)),
 [invtOwnerID] [int] NULL,
 CONSTRAINT [PK_InvtMaster] PRIMARY KEY CLUSTERED 
(
 [invtID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


USE [Stone]
GO
/****** Object:  Table [dbo].[InvtMaster]    Script Date: 12/10/2009 22:23:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[InvtMaster](
 [invtID] [int] IDENTITY(1,1) NOT NULL,
 [invtDate] [datetime] NULL,
 [invtSupID] [int] NULL,
 [invtRefNo] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtRefDate] [datetime] NULL,
 [invtPOID] [int] NULL,
 [invtCurID] [int] NULL,
 [invtPayTermsID] [int] NULL,
 [invtLocationID] [int] NULL,
 [invtNotesPrinted] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtNotesInternal] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 [invtAmtMaterial] [money] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtAmtTotal]  DEFAULT ((0)),
 [invtAmtFreight] [money] NULL,
 [invtAmtOthers] [money] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtAmtOthers]  DEFAULT ((0)),
 [invtStatusID] [int] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtStatusID]  DEFAULT ((0)),
 [invtWflStatusID] [int] NULL CONSTRAINT [DF_MaterialInventoryMaster_invtWflStatusID]  DEFAULT ((0)),
 [invtOwnerID] [int] NULL,
 CONSTRAINT [PK_InvtMaster] PRIMARY KEY CLUSTERED 
(
 [invtID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Trigger I have...

USE [Stone]
GO
/****** Object:  Trigger [dbo].[OnAlter_VendorInvoice]    Script Date: 12/10/2009 22:25:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  Farooq
-- Create date: 10-Dec-09
-- Description: When the Inventory Invoice Is Created, the total amout for all the additional cost is calculated and updated in the invtAmtOthers field in the InvtMaster Table
-- =============================================
CREATE TRIGGER .[dbo].[OnAlter_VendorInvoice] 
   ON  .[dbo].[InvtVendorInvoices] 
   AFTER INSERT,UPDATE
AS 
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for trigger here
 declare @invtID int;
 declare @value float;

 select @invtID=invInvtID from inserted;

 --The ISNULL is used to convert the NULL returned value to ZERO
 select @value=ISNULL(sum(invAmtTotal),0) from InvtVendorInvoices where invInvtID=@invtID;
 --select @value=sum(invAmtTotal) from InvtVendorInvoices where invInvtID=@invtID;

 update InvtMaster set invtAmtOthers=@value where invtID=@invtID;

END

The ASPX web page has the following insert record code on a button click event

Dim cmd As New SqlCommand
    cmd.CommandText = "INSERT INTO InvtVendorInvoices (invSupID,invInvtID,invRefNo,invDesc,invDate,invDateDue,invAmtTotal,invNotesInternal) VALUES (@invSupID,@invInvtID,@invRefNo,@invDesc,@invDate,@invDateDue,@invAmtTotal,@invNotesInternal)"
    cmd.Connection = DataHelper.GetConnection

    cmd.Parameters.Add("@invSupID", SqlDbType.Int).Value = e.NewValues("invSupID")
    cmd.Parameters.Add("@invInvtID", SqlDbType.Int).Value = lblInvtID.Text
    cmd.Parameters.Add("@invRefNo", SqlDbType.NVarChar).Value = e.NewValues("invRefNo")
    cmd.Parameters.Add("@invDesc", SqlDbType.NText).Value = e.NewValues("invDesc")
    cmd.Parameters.Add("@invDate", SqlDbType.DateTime).Value = e.NewValues("invDate")
    cmd.Parameters.Add("@invDateDue", SqlDbType.DateTime).Value = e.NewValues("invDateDue")
    cmd.Parameters.Add("@invAmtTotal", SqlDbType.Money).Value = e.NewValues("invAmtTotal")
    cmd.Parameters.Add("@invNotesInternal", SqlDbType.NText).Value = e.NewValues("invNotesInternal")

    cmd.Connection.Open()
    cmd.ExecuteNonQuery()
+2  A: 

How do you know the trigger doesn't fire? Mayber the trigger does get fire but the update doesn't find any record that satisifes the condition WHERE invtID=@invtID? Could be that the test value you try from Management Studio has a corresponding record in InvtMaster, but when the ASP page runs the code, there is no record.

Remus Rusanu
Hi there,I have tested my logic with only one record in the InvtMaster and i tried to pass the invtID of that record specifically while i was inserting a record in the InvtVendorInvoices table. There is no doubt about having a wrong invtID. Moreover, if we are updating the exisitng record, then the value gets updated correctly in the table "InvtVendorInvoices" which is being updated but the trigger which should update this value in the InvtMaster table doesn't updates at all.
Farooq
However, if i update the InvtVendorInvoices record from the SQL Management Studio then it works fine. I can see the record in the InvtMaster table updates correctly.
Farooq
In order to disable a trigger execution a session has to go to great length. If you add a RAISERROR() in the trigger, does the error get thrown in the ASP invocation? If yes, you have proof that the trigger gets executed but the update is missed.
Remus Rusanu
Hi Remus, Thanks for your great advice. I have added the RAISEERROR() in my trigger but the webpage logic executes without any exception thrown...Please advice...
Farooq
To disable a trigger one woul have to expicilty use DISABLE TRIGGER, see http://msdn.microsoft.com/en-us/library/ms189748.aspx. Make sure you are not doing any mistake like updating a different table (connection string points to a different db) or something similar.
Remus Rusanu
Also, if you can attach the profiler to the Express instance (if you use AttachDBFilename then you must attach profiler to the user instance, see http://msdn.microsoft.com/en-us/library/bb264564(SQL.90).aspx) and trace the execution. The procedure trigger will trigger SP:StmtCompleted events and can be traced step-by-step, see http://msdn.microsoft.com/en-us/library/ms189570.aspx
Remus Rusanu
Hi Remus, thanks again. I confirm that the database triggers are ENABLED. The trigger works fine from the SQL Server Management Studio when i update/insert a record. In this case lets say i only update the value. The updated value definitely gets reflected in the InvtMaster table. But when i update the same value from the ASP.NET webpage it does not get updated. Please remember, that the value is updating the table where i am updating but not in the table where the trigger has to update it "InvtMater".
Farooq
A: 

Does lblInvtID.Text contain the ID of the record you're expecting to be updated?

Tim
Yes, it does... And i have tried to put in a fixed ID as well but no luck..
Farooq
Try not inserting any NText fields and see if the trigger fires.
Tim
A: 

Hello Guys, I have solved the problem... there was actually a callback panel in parent webpage which was also updating the invtAmtOthers coloumn. The callback was being performed after i insert/upadate a record.

Farooq