Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 24th, 2011, 01:56 AM
Registered User
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Updating and inserting data (p 469)

Hi,

Most of the code is straightforward, however, I can't find much documentation (that makes sense) on "e".

What exactly do these lines do?

Code:
e.Values("UpdateDateTime") = DateTime.Now

and

e.NewValues("UpdateDateTime") = DateTime.Now
Obviously, they set the paramaeter UpdateDateTime to the current time, but why is the e.Values/e.NewValues required? In other words, why wouldn't the code just be?

Code:
UpdateDateTime = DateTime.Now
Thanks =)
 
Old April 24th, 2011, 04:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you used this:

Quote:
UpdateDateTime = DateTime.Now
you would be updating a property called UpdateDateTime on the current page (e.g. the ASPX page). This is equivalent to Me.UpdateDateTime = DateTime.Now

The e argument is a method argument (the name is random, but consistent with how event handlers work) passed to that method by the object raising the event.

So, if you have a SqlDataSource raising an Inserting event, it passes an object of type SqlDataSourceCommandEventArgs to the method in your Page class which handles the event. You receive this object in the e parameter. This e parameter then has a Values collection which you can access to supply additional parameter information used to construct the SQL Insert command against the database.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 25th, 2011, 08:09 PM
Registered User
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. That helps. I didn't think database operations were considered an event, I thought it was more like passing a variable to a sub.
 
Old April 26th, 2011, 03:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
I didn't think database operations were considered an event
They aren't, but they can raise events. In pseudo code the Inserting operation of the SqlDataSource control could like this:

Code:
 
Do work to prepare the Insert
' Then set up a new SqlDataSourceCommandEventArgs
Dim args As New SqlDataSourceCommandEventArgs()
Prepopulate the args with data you need in the event handler
' Raise the event to the caller:
RaiseEvent (OnInserting, args)
Perform the actual insert operation and use stuff like args.Values modified by the user
' Then raise the Inserted event
RaiseEvent (OnInserted)
In this "code", each line that doesn't start with a command could be part of the operation carried out by the control. Just before the control raises the Inserting event, it constructs a new SqlDataSourceCommandEventArgs and passes that to *your* method defined in the page classes when it calls RaiseEvent. Your method then adds or modified values in the object using its Values or NewValues collection. Then when your method is done, the control proceeds with its operation. It carries out the actual Insert operation against the database, using the values in the Values property of the args variable. It then raises the Inserted event so you can see whether the operation succeeded or not.

Quote:
I thought it was more like passing a variable to a sub.
Yes, that's what's happening. You register an *event handler* which is a normal method, but which is designed to handle a specific event (in VB using the Handles keyword). Then when the control raises a specific event, it makes available an EventArgs object (or a sub class of it). The run-time then calls your method and provides you with the EventArgs object with event-specific data so you can look at it, and optionally alter it.

Does this help?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 26th, 2011, 10:34 PM
Registered User
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes. Thanks =)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 9 p.469 O/R Design Surface behaior problem Pegasus40218 BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB 0 July 8th, 2010 05:55 PM
Problem with inserting, deleting, and updating using the detalis view bivek BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 10 December 1st, 2009 06:10 PM
inserting and updating records in classic asp codetoad Classic ASP Databases 2 March 21st, 2007 07:40 AM
Inserting/updating with empty date field takabyte Classic ASP Databases 3 December 23rd, 2004 02:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.