Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Practice for DetailsView

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Joined
Jun 26, 2002
Messages
609
Location
GB
Using .NET 2.0 and VS2005 I'm creating a new website and I'm trying to utilise the new controls and features were possible.

At the moment I'm making a simple update / add record form and initially tried creating the textboxes and using a ObjectDataSource Control set to a TableAdapter to add the fields. This worked but I can't for the life of me work out how to read a record from the ObjectDataSource and populate the textboxes ready for an edit to occur. I'd still be interested in hearing how to do this?

Anyway, so I decided to try the detailsview control. At first I couldn't work out how to validate it and add multiline textboxes, but some time playing with templates has got most of that ironed out. However I'm trying to work out the best practice for updating some default fields in the table that the user should not see in the details view. I have a field for modified date to enable basic auditing of the updates and want to update it with the current date and time when the user clicks the update command in the detailsview. How can I do this / what method is best?

All advice greatly appreciated.

Cheers,

Steve G (MCP)
 
Put a trigger on the table being updated to update the datetime column.
 
Thanks. I hadn't considered that. I've not used triggers in the past but shall look into it.

I'm still curious though how this could be accomplished in code and how one can populate a textbox by getting the value from the ObjectDataSource?

Cheers,

Steve G (MCP)
 
Triggers are simple and the best way to accomplish what you want. Let me know if you need help.

Jim
 
This is what I have so far

Code:
ALTER TRIGGER Modified 
ON dbo.EVENTS 
FOR INSERT, UPDATE
AS
IF NOT UPDATE(MODIFIED_DATE) 
   UPDATE dbo.EVENTS SET MODIFIED_DATE=GETDATE() 
   WHERE EVENT_ID IN (SELECT EVENT_ID FROM inserted)

but I can't get it working. My googling hasn't yielded any helpful info.

Just a thought... I havn't yet implemented it but will be having users and roles working created through the vs2005 wizard. Will I be able to get the USERID who is logged in and add this to a MODIFIED_BY field?

Cheers,

Steve G (MCP)
 
Why do you have IF NOT Update(...)?

if that column is updated, then your code won't run.

And yes, you can get the user logged in with this:
Code:
Page.User.Identity.Name

Jim
 
OK I now have

Code:
ALTER TRIGGER Modified 
ON dbo.EVENTS 
FOR INSERT, UPDATE
AS
   UPDATE dbo.EVENTS SET MODIFIED_DATE=GETDATE() 
   WHERE EVENT_ID IN (SELECT EVENT_ID FROM inserted)

This only works if I remove the where statement, but of course then updates the modified date field for every record. What statement should I use to ensure it only updates the updated / inserted record?

Steve G (MCP)
 
Try this:
Code:
ALTER TRIGGER Modified 
ON dbo.EVENTS 
FOR INSERT, UPDATE
AS
   UPDATE dbo.EVENTS
   SET MODIFIED_DATE=GETDATE() 
   From dbo.Event e
   Inner Join Inserted i ON
              e.EVENT_ID = i.EVENT_ID

Jim
 
Don't worry!

I changed the IN to an = in the WHERE clause and it worked.

If I were to get the user details using the code you suggested how would I then pass that onto the table?

Cheers,

Steve G (MCP)
 
I'm not quiet sure why the IN clause does not work. I remember seeing an example in the sql server programming forum. Maybe you could post there for an explination.
To place the user info into a table, you will need to write and update and/or insert statement.

Jim
 
Thanks for all your help.

I still have lots to figure out but this has got me going.

Cheers,



Steve G (MCP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top