1 cs387/cs587: note05 lab 3. 2 global.asax must not be under any sub-folder application_start...

24
1 CS387/CS587: Note05 Lab 3

Upload: phyllis-gilmore

Post on 29-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

1

CS387/CS587: Note05

Lab 3

Page 2: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

2

Global.asax

• Must not be under any sub-folder

• Application_Start

• Application_End

• Application_Error

• Session_Start

• Session_End

Page 3: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Debugging

• When an application starts/ends?– Application_Start– Application_End

• When the session starts/ends?– Session_Start– Session_End

3

Page 4: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Application Start

Sub Application_Start(. . .)

DataClass.setupAdapter ()

End Sub

Set up the adapter once for all requests from all users.

4

Page 5: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 6: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

SessionStart

Sub Session_Start(. . .)

DataClass.getAllProducts()

End Sub

Refill the data table for each session,

6

Page 7: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Page Updating

Textboxes for Individual Fields

• Binding the textboxes

• Not binding the textboxes

• Choosing the way you prefer

• We discuss the unbinding approach

7

Page 8: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 9: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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 Sub9

Page 10: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 11: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 12: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 13: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 14: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Enable/Disable Buttons

Could make a private Sub

14

Page 15: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 16: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 17: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 18: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 19: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 20: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Try-CatchPublic 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

Page 21: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Try-Catch-FinallyPublic 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

Page 22: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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

Page 23: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

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 correctly23

Page 24: 1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End

Lab3

• Should make delete work

• Try update and new– We will discuss the issues next time

24