asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Custom Object Data Binding with .NET -- Cont'd
By Luther Stanton


  • email this article to a colleague
  • suggest an article

    Data Binding 201: One Way Data Binding

    In this scenario you will take your first look at using the .NET Framework to manage our data binding with no modification to our Employee class and an actual reduction of logic within the form. The code for this scenario appears in the "OneWayDataBindingSolution".

    In this solution, I removed the two marshalling functions, LoadValuesToForm and LoadValuesFromForm within the Employee display form. These two functions were replaced with SetDataBindings, which is called from the form's load event handler. Again, the logic to set up the data binding could be included directly in the load event handler, however centralizing the logic in its own function leads to better code reuse and a cleaner implementation. The SetDataBindings method appears in Figure 11.


    Figure 11

    The method simply defines the data bindings between the GUI elements and the local object's properties. Each binding needs to be set up individually, such as the code in lines 243 - 244. Examining this line close, you see the following syntax:


    Figure 12

    From this you can conclude that each control has a collection of data bindings associated with it. Within this collection, each property of the control can have a data binding. One important point is that no property can have more than one data binding associated with it. You can assign more than one; however, a System Argument exception will be thrown. This is why each binding is preceded with a check to see if a binding already exists for the specified control's property and if it does, it is removed. An example of this code appears in line 239 - 241:


    Figure 13

    Looking closer at the syntax to create the binding (Figure 12, Lines 242 - 244), you see that you specify the property of the control, "Text", the data source, _oEmployee, and the property on the data source, "firstName". It is interesting to note that in the case sensitive world of C#, the property names specified for the control and data source are not case sensitive.

    Because the control's property to use for the binding is specified within the binding syntax, it is just as easy to bind to other properties, such as the "checked" property of a check box, which you do in lines 271 - 272.

    Another point of interest is that the data binding mechanisms automatically handle conversion for us. No longer do you need to write code to convert our decimal and DateTime values to and from strings. This is a nice feature in that as developers you no longer need to worry about such low level plumbing details. Another feature is that the input is filtered. If you run the project and try to enter a non date value for the Hire Date text box or a non numerical value for the Salary text box, the entries will be discarded and the original values are restored.

    Note, however, when the application runs, you get a default formatting. In the case of Hire Date text box you get a date and time displayed; however, this is not what you wanted. In the manual data binding example, you were formatting the value using the DateTime's ToShortDateTime() method. Alas, Microsoft addresses this scenario through allowing us to assign an event delegate that will be called when the data binding mechanisms format the data for display in the control. In this case, you need to assign an event delegate to the Format event raised by the binding when it is preparing to format the value for the assigned control. In order to do this, I created a second method within the form to define the data bindings, SetDataBindingsWithFormat. This method adds the necessary event plumbing to control the format of the display. I also defined an additional method as the delegate, DateTimeToShortDateString. As you are running the sample code, you can call either SetDataBindingsWithFormat or SetDataBindings to see the difference. The new code to add the event delegate is shown in Figure 14.


    Figure 14

    As Figure 14 shows, the binding for the hire date textbox, including registering the event handler is defined prior to assigning the binding to the actual control. This is because when the binding is assigned to the control, the binding mechanism will automatically pull the value from the data source. So, if the binding was defined "on the fly" and then assign the handler as in Figure 15, the formatting would not be applied.


    Figure 15

    Data binding can be seen working by running the application, changing some of the values in the text boxes, and clicking the Save button. You could either set a break point in the Save click event handler or within the object to inspect the values to see that the object's properties are updated with the new values. Alternatively, you could look at the serialized output in the Employees.xml file.

    In this implementation, however, the data flow is only one way - from the form to the object. If the object's property values change, the changes will not be reflected in the form. This can be verified by clicking the Load New Values button. The click event handler for this button changes the objects values behind the scenes. However, the form values do not change. Clicking the Save button at this point will cause the object to save with values different from the values shown in the form.

    If data binding is only one way, you may ask, "Why did the initial values populate in the form?". The answer is that when the data binding in assigned, the data binding mechanisms will look at the current values of the properties, but this is the only time that the controls will pull values from the object, until the changes outlined in the next section "Data Binding 301" are implemented.

    << Introduction •       • Data Binding 301: Graduate Level Data Binding >>

  • Supporting Products/Tools
    Proposion N2N
    Proposion N2N connects Microsoft .NET applications to Lotus Notes and Lotus Domino databases. This ADO.NET managed data provider allows you to perform blindingly fast queries and updates of Notes data from ASP.NET pages, .NET web services, Windows, or Mobile applications. An innovative SQL-like query language leverages the unique features of Notes and makes collaborative software accessible to relational database programmers.
    [Top]
    Other Articles
    Sep 15, 2005 - Building an Image Keyword System
    Unlike text-based file formats image files aren't made up of words, which makes searching for an image file by keyword difficult. Instead of being able to simply open the file to see what it contains, we're stuck looking at the text around it and other metadata to determine the image's meaning. In this article, Ziran Sun shows you how to build a simple database-based image keyword system that allows you to associate keywords with images and use these keywords to make finding images easier.
    [Read This Article]  [Top]
    Apr 7, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
    In the second part of of his article on using MySQL with ASP.NET, Ziran Sun covers how to add a new MySQL user to the database server, assign the user the appropriate permissions, connect to the database, and build a simple ASP.NET page to perform a query.
    [Read This Article]  [Top]
    Feb 10, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 1
    Back in the days of classic ASP, if you were building a database-driven web site, your choice was either to invest a lot of money to get a copy of Microsoft SQL Server (or some other enterprise-ready database) or invest a lot of time finding a way to deal with the performance and scalability limitations of Microsoft Access. Luckily these days there's another viable alternative: MySQL.
    [Read This Article]  [Top]
    Jan 27, 2005 - Moving a Database from SQL Server 7.0 to SQL Server 2000
    Moving or copying a SQL Server database from one machine to another requires a lot of preparation in order to ensure a smooth transfer. In this article, Dina Fleet Berry examines the different methods and highlights the different issues associated with each of them.
    [Read This Article]  [Top]
    Jan 6, 2005 - Debugging a SQL Stored Procedure from inside SQL Server 2000 Query Analyzer
    There are many times when using SQL Server 2000 Query Analyzer to debug SQL statements is a better choice than debugging in Visual Studio .NET. In this article, Dina Fleet Berry explains why and walks you through the debugging process step-by step.
    [Read This Article]  [Top]
    Nov 24, 2004 - Persisting .NET Objects to SQL Server Using SQLXML and Serialization
    As a follow up to his article on retrieving objects from SQL Server using SQLXML and serialization, Gianluca Nuzzo discusses saving objects back to SQL Server using a schema definition file and updategrams.
    [Read This Article]  [Top]
    Sep 14, 2004 - Transaction Processing in ADO.NET 2.0
    One area that stands out when comparing ADO.NET 1.x to ADO.NET 2.0 is transaction processing. Bill Ryan shows just how easy transaction processing has become with the TransactionScope object in ADO.NET 2.0.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Sep 2, 2004 - Queue MSMQ Messages from SQL Server
    Learn how to create a console application to queue a message in Microsoft Message Queuing (MSMQ) and then use an extended stored procedure to call the console application from a SQL Server trigger.
    [Read This Article]  [Top]
    Aug 30, 2004 - Tuning Up ADO.NET Connection Pooling in ASP.NET Applications
    Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. This article shows how to monitor the connection pool, diagnose a potential problem, and apply the appropriate fix.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers