Download presentation
Presentation is loading. Please wait.
1
The Update 11:48:57 PM 1
2
2 Types of Updates 1 st type – Update a specific product – covered in previous lecture 2 nd Type – update a range of products – user selects a product type (which may consist of more than one specific product) – and a mass update if performed The GUI – change the menu so that Update has a sub menu Also – enable the inline styles option of the menu control so that you change many of the menu control properties 11:48:57 PM 2
3
Modified Menu 11:48:57 PM 3
4
Doing a Mass Update of Data A Mass update has to be performed by making use of a Dataset and a tableadapter Add a new Dataset object to your application…as you drag and drop a tableadapter object onto the dataset, the wizard opens up and talks you through the process of configuring the tableadapters for the dataset It creates a new typed dataset with datatables that you can use to manipulate the web page data 11:48:57 PM 4
5
The Adapter Configuration 11:48:57 PM 5
6
Adding a Parameter 11:48:57 PM 6
7
Choose the Advanced Option of Allowing Updates 11:48:57 PM 7
8
The New Parameterised Product Table DataType/Class You now have a Typed Dataset available to use in your program 11:48:57 PM 8
9
Scenario Your want to update the price of a set of products – not just one product e.g. – you want to increase the price of all bread items First – arrange the GUI so that the product type can be selected and then do a kind’a mass update of the product This is via a bit of coding where you make use of the typed Dataset 11:48:57 PM 9
10
The GUI Dropdown box controlled by a Sql Data Source linked to the Product Type Table Gridview linked to a 2 nd Sql Data Source that is parameterised …and the effect is … 11:48:57 PM 10
11
The Update part of the GUI 11:48:57 PM 11
12
Now to write the Mass Update Code Protected Sub btnUpdatePrice_Click(sender As Object, e As System.EventArgs) Handles btnUpdatePrice.Click Dim DSProd As New DataSetProduct Dim TAProd As New DataSetProductTableAdapters.ProductTableAdapter TAProd.Fill(DSProd.Product, DDLProductType.SelectedValue) For i As Integer = 0 To DSProd.Product.Rows.Count - 1 DSProd.Product.Rows(i).Item("ProductPrice") += txtUpdateAmount.Text Next TAProd.Update(DSProd.Product) GridView1.DataBind() End Sub 11:48:57 PM 12
13
Update ….cont’d How about if you wanted to do a specific update? This can be easily achieved via the datagrid or the detailsview controls However, if you had to do this manually – it entails manipulation of the SQLDataSource (SQLDS) A simple demo…You have a movie list table and you wanted to update the quantity on hand User selects a movie from a webpage…maybe from a dropdown control…you would like to adjust/edit the QOH You can go for a typed dataset and a Movie table dataAdapter …as was the case in the previous scenario …or you cold do a …parameterised update via the SQLDS 11:48:57 PM 13
14
A Parameterised Update via the SQLDS Invoke a SQLDS that retrieves all the Movies …ensure that the SQLDS is allowed to do updates 11:48:57 PM 14
15
A Dropdown List (DDL) The DDL is convenient to allow selection of the required movie (name) In order for the DDL to get the movie names from the database, it has to be bound to the SQLDS The Binding is done so that the DDL displays the Movie name but the actual field selected is the MovieID 11:48:57 PM 15
16
11:48:57 PM 16
17
Enable PostBack and Overwrite Enable Postback on the DDL and allow the SQLDS to Overwrite update changes that is made to the database 11:48:57 PM 17
18
Now for Configuration of the SQLDS 11:48:57 PM 18 Open up the Update Command from the Properties window of the SQLDS It has a pre-defined update command that basically scrolls through all records of the Movie Table and checks if any value has changed…if the value has changed…it does an update on the database However, for the current application, all that we require is that the SQLDS control updates only the QOH for the Movie that we select from the DDL So the SQL should go something like… Update tblMovie…set QOH = @QOH…where MovieID=@MovieID
19
SQL and the Wizards In order to generate a parameterised SQL query – the @ symbol is used Whenever an @ symbol is used, the wizard opens up a new interface where it asks you for the source of the parameter value The Parameter window of the SQLDS wizard is sometimes cluttered with unnecessary stuff You are liberty to clear this up via the Query Builder and then using the Query Builder only to generate the SQL that you require 11:48:57 PM 19
20
The Clutter 11:48:57 PM 20
21
The Cleansing Phase 11:48:57 PM 21
22
The New SQL Update 11:48:57 PM 22
23
Set the Parameter Values 11:48:57 PM 23
24
Sort out the GUI …and you’re done! 11:48:57 PM 24
25
Extending the Movie App What if you wanted to hire a movie?...just a primitive hire demo So – you can select a movie – but now you want to hire the movie as well Adopting a rather primitive approach – the customer details will simply be recorded – there is no sophistication with regards to customer records However, once a hire is done – the QOH for the movie needs to be UPDATED So let’s see what a possible GUI will look like! 11:48:57 PM 25
26
GUI for the Hire DDL – display movies ( note: movie name is displayed but the value is set to be the Movie ID The Movie details are displayed by linking the DDL to the Gridview A new section – DIV is added to enable Movie Hire The Movie Hire section consists of a label for the date – activated via the PageLoad event handler; 3 textboxes and 2 buttons 11:48:57 PM 26
27
Once the Hire Button is clicked The customer details and the movie name is recorded in a new table The big thing is that the QOH needs to be updated to a new value However, in order to do this the old QOH needs to be retrieved from the Gridview – or you could configure the SQL statement to simply update the QOH by “one less” However, for this app – we will retrieve the current QOH from the Gridview and use this value to feed the parameter value that is expected for the SQLDS insert command 11:48:57 PM 27
28
The Hire Code Since the parameter wizard expects a value to come from a control – a hidden label is used to store the value for QOH Also, the Gridview.Databind is useful since it will provide an immediate updated view of the QOH for the movie list 11:48:57 PM 28
29
The Power of Scope_Identity() After a new record has been inserted – let’s say a customer record – you wanted to retrieve the ID number of the newly inserted customer You have to configure the Insert Query of the Sql DataSource so that the Scope_Identity() method is called – this method returns the ID of the newly inserted record 11:48:57 PM 29
30
Configuring the Sql DataSource for Scope_Identity() On the SQLDs of the Customer Table – modify the Insert Query by adding a semi-colon and the statement Select @CustID = Scope_Identity() Refresh the Parameter list Configure the CustID parameter so that It is output The data type is Int32 11:48:57 PM 30
31
The GUI and Event Handler So …once the Register Customer button is clicked… The Insert method of the Sql Data source is invoked After the insert has taken place…the SqlDS_Inserted() method is invoked and the value stored in the CustID parameter is returned 11:48:57 PM 31
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.