Showing posts with label detailsview. Show all posts
Showing posts with label detailsview. Show all posts

Tuesday, February 14, 2012

Best way to insert a DateModified in updates

how do i update the date modified Field in my DB. i was thinking of update trigger? i am using ASP 2 with DetailsView for Editing(Updates in SQL term). i can make a Update SP but think that might not be needed

If you use the SP you can define the date now to a variable. @.datenow = GetDate()|||

In the Update procedure, you can just "SET dateModified=GETDATE()".

In my view, Triggers are a little bad in the respect that they are hidden. If you look at a simple SQL statement, you really can't "trace" the code execution completely if your DB has a whole pile of Triggers firing all the time, which means, things might be happening that the SQL statement writer isn't aware of, and that can cause problems.

Of course, their not overall bad, but my personal preference is to use an SP, unless a Trigger is specifically required or for some odd reason more effecient.

Sunday, February 12, 2012

Best Way to delete a parent record

whats the best way to delete a parent record?

for now what i am doing is to have a special delete page (means not DetailsView/GridView) just to delete parent records so user can see what child records will be deleted too. is the a better way?

my code below:

The T-SQL Stored Procedure

ALTER PROCEDURE dbo.DeleteInventory(@.InventoryIDint)ASDELETE FROM BookingWHERE InventoryID = @.InventoryIDDELETE FROM InventoryWHERE InventoryID = @.InventoryID

The code in the Code Behind to execute when a button(delete) click

Try Dim tempAs Integer = SqlDataSource4.DeleteIf temp > 0Then lblDeleteDtatus.ForeColor = Drawing.Color.Blue lblDeleteDtatus.Text = temp &" Records/Rows Deleted."Else lblDeleteDtatus.ForeColor = Drawing.Color.Orange lblDeleteDtatus.Text ="No records Deleted!"End If Catch exAs Exception lblDeleteDtatus.ForeColor = Drawing.Color.Red lblDeleteDtatus.Text ="An Error Occured.<br />" & ex.Message.ToStringEnd Try
any better methods anyone wants to share?

Assuming that it makes business sense to delete an inventory record and orders for that item, your stored procedure would be better as

ALTER PROCEDURE dbo.DeleteInventory
(
@.InventoryID int,
@.Count INT OUTPUT -- Return the number of Inventory records to delete as an output parameter
)
AS
SET NOCOUNT ON
SELECT @.COUNT = COUNT(*) FROM Booking WHERE InventoryID = @.InventoryID
BEGIN TRANSACTION
DELETE FROM Booking WHERE InventoryID = @.InventoryID
DELETE FROM Inventory WHERE InventoryID = @.InventoryID
COMMIT

|||

i used "

Dim
"tempAs Integer = SqlDataSource4.Delete"
to get the rows affected, isn't it about the same? by the way with ur method, its simpler but
i seem to have problems. how do i "link" it to a variable? i know how to link it to a Control, etc but how to a variable?