Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem with audit table trigger
Message
From
04/03/2014 16:23:58
 
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01595410
Message ID:
01595782
Views:
44
Well, I thought maybe I could make this work with a datasource. So I put this code in the code behind.
	Private Sub SqlDataSource2_Updating(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource2.Updating
		SqlDataSource2.UpdateCommand = "DECLARE @ContextInfo varbinary(128);SELECT @ContextInfo = cast( @User AS varbinary(128) );SET CONTEXT_INFO @ContextInfo;UPDATE [TIP_Projects] SET [Notes] = @Notes, [GuardRails] = @GuardRails, [UtilityRelocation] = @UtilityRelocation, [PvmtPreservation] = @PvmtPreservation, [ADAEnhancement] = @ADAEnhancement, [Landscape] = @Landscape, [BikeRack] = @BikeRack, [Art] = @Art, [MultiUsePath] = @MultiUsePath, [Curbs] = @Curbs, [StreetLighting] = @StreetLighting, [PedLighting] = @PedLighting, [Rightofway] = @Rightofway, [Median] = @Median, [Signing] = @Signing, [Drainage] = @Drainage, [Overpass] = @Overpass, [Underpass] = @Underpass, [Wildlife] = @Wildlife, [Signals] = @Signals, [Cameras] = @Cameras, [Culvert] = @Culvert, [Bridge] = @Bridge, [Striping] = @Striping, [TurnLanes] = @TurnLanes, [BusPullout] = @BusPullout, [Soundwall] = @Soundwall, [RubberAsph] = @RubberAsph, [System] = @System, [SubSystem] = @SubSystem, [MapStyle] = @MapStyle, [GeoArea] = @GeoArea, [NewBikeLanes] = @NewBikeLanes, [NewSidewalks] = @NewSidewalks, [Modeled] = @Modeled, [LongDesc] = @LongDesc, [Capacity]=@Capacity, [Operations]=@Operations, [Enhancement]=@Enhancement, [Safety]=@Safety, [SR2S]=@SR2S, [Procurement]=@Procurement, [TrafficCalming]=@TrafficCalming WHERE [KeyId] = @KeyId"
		SqlDataSource2.UpdateParameters.Add("@User", DbType.String, "donf")
	End Sub
This executes and the save works OK both in the edited table and the audit table.

Then I tried to modify the trigger to capture the username.
SELECT @UserName = CAST(CONTEXT_INFO() AS VARCHAR(128)),
@UpdateDate = convert(varchar(8), 
	getdate(), 112) + ' ' + convert(varchar(12), 
	getdate(), 114)
This saved the edits OK but the trigger did not save the audit record.
I then modified the trigger to hard code the username.
SELECT @UserName = CAST(CONTEXT_INFO() AS VARCHAR(128)),
@UpdateDate = convert(varchar(8), 
	getdate(), 112) + ' ' + convert(varchar(12), 
	getdate(), 114)

SELECT @UserName = 'Donf'
And this works OK. So the failure must be that the code sending the username to Context_Info or the code to read it back out needs improvement.
Do you see any obvious errors??
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform