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

Post on 29-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Application Start

Sub Application_Start(. . .)

DataClass.setupAdapter ()

End Sub

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

4

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

SessionStart

Sub Session_Start(. . .)

DataClass.getAllProducts()

End Sub

Refill the data table for each session,

6

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

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

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

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

Enable/Disable Buttons

Could make a private Sub

14

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

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

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

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

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

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

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

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

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

Lab3

• Should make delete work

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

24

top related