Presenter:Beth Massi
Visual Basic programmers will see how to add field and row validation to an application in order to enforce business rules using the DataSet Designer and partial classes.
Click here to go to original video page
Minute 0
How my aim is Beth Massey and welcome to the Windows forms of a data via a series in its next video I m going to show you how we can add field and build validation to our data sets in order to enforce business rules will be doing this by using partial classes and the air provider I very started an application here card people call the order manager and I ve got one form open now and we can create a needle database called LMS so when it first going to do it add a new data source to this project and I m just going to connect to our customer table if you are even working on this application following along you can just use the customer data set and we are created in a previous video I m just doing this to reveal some call this customer data set and now we ve got a customer data set created for us here still if a deal is in the back tour form here and just
Minute 1
In this example were just going to drag over the customer table and this will create our binding navigator and will also set up our customer table adapter the binding source and the data set here in the component tray so first time that a deal is I m going to dockets impair containers of looks a little bit better and on make the form of a larger Senate we want to happen is when I add a new row to our customer a great year I want to be able to enforce the rule is that the last name must be specified so before the user can tab off the row and one a make sure that the row is phallic so we need to add a piece of validation and it s always bad practice to add business rules directly into the form itself so we don t want to go that route instead we re going to try and create the business rules hear it directly inside the data set so let s pop on over to the data set just double click and now I ve got the designer here and we can write
Minute 2
Click on the customer and we can save you code in the meteor that that opens up the partial class for us you ll notice that this has partial class customer data set and inside the customer data said we have a customer data table where free here to write any code we need that validates the data set the tables and rows the fields inside so that s a partial class allows you deal it allows you to extend a class that s code generated by the designer in order to add more functionality to it so the first piece of functionality on a hat is a Hispanic check if the last name is Joel or the empty string I do I want to display a message to the user that they must specify a last name so in order to do that I was going to add a function or a sob call check last name so that it will check last name and where to pass in the customer
Minute 3
Customer base at customer outfit and the check is this going to go really simply just to check if it s nowhere the empty string to sell it if is no last name for else last name equals empty string them up and want to display a message so Morgan and set the customer oh set column error case of this method will set a bearer on the column index case on this on the particular route and so that ll indicate that his last name needs be filled out the last named down to say please specify it in last night
Minute 4
May otherwise everything signed so there was an error on their can remove it by just specifying the empty string of tanks and ever get a need to call this method from a couple different events on our data table so these first events were going to select his were going to select in the column changed event so from this event many column changes it will get fired so first you need to check if the column is the last name column so if you look at our event arcs we have Re column and row and the proposed value here Sunday to check if this column is me not last name column then we ll go ahead and check the last name today and will only to do is send the
Minute 5
He taught row but since this row is customer row we can to send it right in the tank now the second event were going to want to handle is de table nouvelle case of this gets fired when a new row is added to the data set here so I m going to does then saying he taught check last name in this case we are going to be sending E row unfortunately is not a customer row here on the idea is that it s not cast it as such so always need to do is cast itself will just see tight arche row as our customer data set customer row and pass that into our check last name function okay Grace okay so now go back to my form and I m just going to set the edit mode on the daily greenfield
Minute 6
Equal to be at it on and terror so that our offense will fire how we want them to fire sonatas and a press F5 to start debugging in case and now we have are our list of customers and when I press ads you ll notice that our did this will fired please specify a last name if I m on a current record and I deleted last name and I Off of it our business goals will fire again so it s really an advantage to put your business rules inside the data sets themselves instead of on the forms because now we use data sets across our forms your business rules are centralized in one place go rate so one had a different form I won at a forum that shows the details instead of showing everything inside of the gray Simon is just right click add Windows form
Minute 7
A call for him to and in this case whenever they do is instead of dragging our customer on as a daily greenfield and I m a dragon on and the details he says will set up our controls the text box controls see is that when we created a grave it automatically displayed any field rules to us with the error icon in order to get that functionality to work with custom controls like these instead of the gray were going to call were going to call upon the air provider and if you just open your toolbox you ll see the air provider here you can just drag that in your component tray and now all we had to do is configure the air provider so that its data source is the customer binding source said now before we hit F5 and just in the double click on my project and make this startup form form to case an elegant go ahead
Minute 8
F5 and this time it opens up our details form and you can see if I remove the last name here because our air provider it s picking up the air s collection that s going on inside of the data table if we go ahead and press act you ll see that it also fires on our new routes they so that s all there is to it still remember it s good practice not to put business rules directly inside of your forms instead put them inside the data set so that when we you use data sets across your forms the business rules are centralized in one place so I d like to thank you for your time if you like to check out the Visual Basic developer Center will be posting updates these videos there please also check out the Visual Basic team blog and my blog for more information basic
Sunday, May 25, 2008
Video: How Do I: Add Validation?
Labels:
validation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment