Download presentation
Presentation is loading. Please wait.
Published byPhyllis Gilmore Modified over 9 years ago
1
1 CS387/CS587: Note05 Lab 3
2
2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End
3
Debugging When an application starts/ends? –Application_Start –Application_End When the session starts/ends? –Session_Start –Session_End 3
4
Application Start Sub Application_Start(...) DataClass.setupAdapter () End Sub Set up the adapter once for all requests from all users. 4
5
Lab3/Default.aspx Protected Sub Page_Load(...) Handles Me.Load ‘DataClass.tblProduct.Clear() ‘DataClass.getAllProducts() GridView1.DataSource = DataClass.tblProduct GridView1.DataBind() End Sub Reloading table for every page request. (Could be done in Application_Start) Binding must be done for each page request. 5
6
SessionStart Sub Session_Start(...) DataClass.getAllProducts() End Sub Refill the data table for each session, 6
7
Page Updating Textboxes for Individual Fields Binding the textboxes Not binding the textboxes Choosing the way you prefer We discuss the unbinding approach 7
8
Page Updating Protected Sub Page_Load(…) Handles Me.Load DisplayRow(0) End Sub Private Sub DisplayRow(Index As Integer) Dim row As Data.DataRow row = DataClass.tblProducts.Rows(index) txtID.Text = row(0) txtName.Text = row(1) txtPrice.Text = row(2) End Sub 8
9
Page Updating Navigation Buttons Partial Class Lab3_Updating Private index As Integer = 0 Protected Sub Button6_Click(…) Handles btnNext.Click index += 1 DisplayRow(index) End Sub Protected Sub Button6_Click(…) Handles btnPrevious.Click index -= 1 DisplayRow(index) End Sub 9
10
Processing of Dynamic Web Pages Build Web Site: Compiling code-behind classes to a DLL file No exe file ASP.NET instantiates class objects ASP.NET processes the class object ASP.NET generates a page IIS accepts page requests and sends the generated pages back to client 10
11
Page Updating Navigation Buttons Partial Class Lab3_Updating ‘ Resets index to 0 every time Private index As Integer = 0 Protected Sub Button6_Click(…) Handles btnNext.Click index += 1 DisplayRow(index) End Sub 11
12
Session Variables Sub Session_Start(...) ‘ productIndex should be better ‘ we need page index later Session(“Index”) = 0 End Sub Protected Sub Page_Load(…) Handles Me.Load DisplayRow(Session(“Index”)) End Sub 12
13
Navigation Buttons Partial Class Lab3_Updating ‘ Private index As Integer = 0 Protected Sub Button6_Click(…) Handles btnNext.Click Session(“Index”) += 1 DisplayRow(Session(“Index”)) End Sub Protected Sub Button6_Click(…) Handles btnPrevious.Click Session(“Index”) -= 1 DisplayRow(Session(“Index”)) End Sub 13
14
Enable/Disable Buttons Could make a private Sub 14
15
Navigation Buttons Partial Class Lab3_Updating ‘ Private index As Integer = 0 Protected Sub Button6_Click(…) Handles btnNext.Click Session(“Index”) += 1 DisplayRow(Session(“Index”)) EnableDisableButtons() End Sub Protected Sub Button6_Click(…) Handles btnPrevious.Click Session(“Index”) -= 1 DisplayRow(Session(“Index”)) EnableDisableButtons() End Sub 15
16
SQL Statements Update Product Set ProductID = ‘NewID’, ProductName = ‘NewName’, UnitPrice = newPrice Where ProductID = ‘OldID’; Insert Into Product Values(‘NewID’, ‘NewName’, NewPrice); Delete From Product Where ProductID = ‘OldID’; 16
17
Button Update ‘ allow ID to be modified Protected Sub Button6_Click(…) Handles btnUpdate.Click // ID could be modified Dim Index As Integer = Session(“Index”) Dim oldID As String = DataClass.tblProducts.Rows(Index)(0) Dim newID As String = txtID.Text Dim newName As String Dim newPrice As Double DataClass.UpdateProduct(oldID, newID, newName, newPrice) End Sub 17
18
UpdateProduct Public Shared Sub UpdateProduct(oldID As String, newID As String, newName As String, newPrice As Double) ‘ Building SQL statement with variables prodCmd.CommandText = "..." con.Open() prodCmd.ExecuteNonQuery() con.Close() End Sub 18
19
Building SQL Statements With Variables Update Product Set ProductID = ‘NewID’, ProductName = ‘NewName’, Price = newPrice Where ProductID = ‘OldID’; Cmd = "Update Product " & "Set ProductID = '" & NewID & "', " & "ProductName = '" & NewName & "', " & “UnitPrice = " & newPrice & "Where ProductID = '" & OldID & "'" -- No ";" at the end -- need spaces between words 19
20
Try-Catch Public Shared Sub UpdateProduct(oldID As String, newID As String, newName As String, newPrice As Double) prodCmd.CommandText = "..." Try con.Open() prodCmd.ExecuteNonQuery() con.Close() Catch ex Throw ex End Try End Sub 20
21
Try-Catch-Finally Public Shared Sub UpdateProduct(oldID As String, newID As String, newName As String, newPrice As Double) prodCmd.CommandText = "..." Try con.Open() prodCmd.ExecuteNonQuery()‘ update database ‘ con.Close() Catch ex Throw new Exception(ex.Message & myCmd.CommandText) Finally con.Close()‘ always close it End Try End Sub 21
22
Button Update Protected Sub Button6_Click(…) Handles btnUpdate.Click Dim Index As Integer = Session(“Index”) Dim oldID As String = DataClass.tblProduct.Rows(Index)(0) Dim newID As String = txtID.Text Dim oldID As String Dim newPrice As Double Try DataClass.UpdateProduct(oldID, newID, newName, newPrice) ‘ must update tblProducts ‘ order, Session(“index”), DisplayRow Catch ex txtMsg.Text = ex.Message End Try End Sub 22
23
Product Order Current record still displayed Ascending order of ProductID at all times Update/Insertion/Deletion may change the order Save to database Reload records with “Order By” Update Session(“Index”) Navigation buttons must work correctly 23
24
Lab3 Should make delete work Try update and new –We will discuss the issues next time 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.