+ All Categories
Home > Documents > CS 3870/CS 5870: Note05

CS 3870/CS 5870: Note05

Date post: 07-Jan-2016
Category:
Upload: kolina
View: 43 times
Download: 0 times
Share this document with a friend
Description:
CS 3870/CS 5870: Note05. Lab 3 Web Application with Dataase. Master Page. All Web pages will be similar Should be created before other web pages Add New Items Controls on the Master page ContentPlaceHolder Leave ContentPlaceHolder empty - PowerPoint PPT Presentation
40
1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database
Transcript
Page 1: CS 3870/CS 5870: Note05

1

CS 3870/CS 5870: Note05

Prog3

Web Application with Database

Page 2: CS 3870/CS 5870: Note05

2

Master Page

• All Web pages will be similar

• Should be created before other web pages

• Add New Items

• Master Page

• Name: Prog3MasterPage.master

• Check “Place code in separate file”

• Uncheck “Select master page”– Could create a master page based on another master page

• May be incomplete and need to come back later

Page 3: CS 3870/CS 5870: Note05

3

Elements on the Master page

• External CSS file<link href="StyleSheet.css" rel="stylesheet" type="text/css" />

• Form– All controls should be inside Form for dynamic pages

• Two ContentPlaceHolder controls: could change id

<asp:ContentPlaceHolder id="head" runat="server">

</asp:ContentPlaceHolder>

<asp:ContentPlaceHolder id="ContentPlaceHolder1"

runat="server">

</asp:ContentPlaceHolder>

Page 4: CS 3870/CS 5870: Note05

4

Elements on the Master page

• Leave ContentPlaceHolder empty

• Add all common elements before/after ContentPlaceHolder– Title and Names

– Adding navbar: incomplete

Page 5: CS 3870/CS 5870: Note05

Content Pages• Create the three pages using the master page

– Add New Item

– Web Form

– Place code in separate file

– Select master page

– Add

– Select folder

– Select Master page

• The second web form

– Add

– Web Form (with master)5

Page 6: CS 3870/CS 5870: Note05

Content Pages• No Form control on content pages

– The form control on the master page will be combined with the controls on the content page

• Two Content controls

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Prog3Body" Runat="Server">

</asp:Content>

• Type it if not there6

Page 7: CS 3870/CS 5870: Note05

7

Complete the Navbar on Master Page

<ul class="navbar">

<li> <a href="Default.aspx"> All Products </a></li>

<li> <a href="Updating.aspx"> Updating Products </a></li>

<li> <a href="Shopping.aspx"> Shopping </a></li>

</ul>

Type and select the pages.

Page 8: CS 3870/CS 5870: Note05

8

Adding Elements

• Type “All Products”, “Updating” and “Shopping” on the three pages

• Run IE to see the pages

Page 9: CS 3870/CS 5870: Note05

9

The Database

• UWPCS3870

• SQL Server on Alpha

• User: MSCS

(not case sensitive)

• Password: MasterInCS

(case sensitive)

Page 10: CS 3870/CS 5870: Note05

Table Product

Four Columns

ProductID : nchar(4), primary key,

not updatable

ProductNmae: nvarchar(50)

UnitPrice : smallmoney

Description : nvarchar(MAX), allow nulls

10

Page 11: CS 3870/CS 5870: Note05

11

Accessing Database

• Data Source Controls– SqlDataSource

– AccessDataSource

– . . .

• Code class– Connection

– Command

– DataAdpater

– AdapterBuilder

• Prog3– Use Code class

Page 12: CS 3870/CS 5870: Note05

ASP.NET Folders

• Solution Explorer

• Right click Web site

• Add ASP.NET Folder

• App_Code

• (App_Data)

• . . .

12

Page 13: CS 3870/CS 5870: Note05

SQLDataClass

• Code class in folder App_Code

• No code module

• All variables and procedures should be

Shared (Static in C#, Java and C++)

• Otherwise, need to create object

13

Page 14: CS 3870/CS 5870: Note05

Variables

Public Class SQLDataClass

Private Const ConStr As String = "Data Source=Alpha;” &

“Initial Catalog=UWPCS3870;Persist Security Info=True;” &

“User ID=MSCS;Password=MasterInCS"

Private Shared prodAdapter As System.Data.SqlClient.SqlDataAdapter

Private Shared prodBuilder As System.Data.SqlClient.SqlDataAdapter

Private Shared prodCmd As New Data.SqlClient.SqlCommand

Private Shared con As New Data.SqlClient.SqlConnection

Public Shared tblProduct As New Data.DataTable("Product")

. . .

End Class

The objects are available all the times.

14

Page 15: CS 3870/CS 5870: Note05

Setup Command and Adapter

‘ Sets up the connection, command and adapter

Public Shared Sub setupProdAdapter()

con.ConnectionString = ConStr

prodCmd.Connection = con

prodCmd.CommandType = Data.CommandType.Text

prodCmd.CommandText = "Select * from Product order by ProductID"

prodAdapter = New System.Data.SqlClient.SqlDataAdapter(prodCmd)

prodAdapter.FillSchema(tblProduct, Data.SchemaType.Source)

End Sub

15

Page 16: CS 3870/CS 5870: Note05

Retrieve Data Records‘ Gets the table records and the table schema

Public Shared Sub getAllProducts()

‘ Need to reset the command

prodCmd.CommandText = "Select * from Product order by ProductID"

Try

If Not tblProduct Is Nothing Then

tblProduct.Clear()

End If

prodAdapter.Fill(tblProduct)

Catch ex As Exception

Throw ex

Finally

con.Close()

End Try

End Sub 16

Page 17: CS 3870/CS 5870: Note05

Setting up the Adapter

‘ Global.asax

Sub Application_Start(. . .)

SQLDataClass.setupProdAdapter()

End Sub

Do it just once for the application for all sessions of all users.

17

Page 18: CS 3870/CS 5870: Note05

Setting up the AdapterPublic Shared Sub getAllProdcts()

If prodAdapter Is Nothing Then

setupProdAdapter()

End If

prodCmd.CommandText = "Select * from Product order by ProductID"

Try

. . .

End Try

End Sub

18

Page 19: CS 3870/CS 5870: Note05

Binding Gridview

‘ Add a GridView on page Default.aspx

‘ GridView: ToolBox – Data

Protected Sub Page_Load(. . .) Handles Me.Load

SQLDataClass.getAllProducts()

GridView1.DataSource = SQLDataClass.tblProduct

GridView1.DataBind()

End Sub

Refill the data table for each page request.

19

Page 20: CS 3870/CS 5870: Note05

Formatting GridView<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False"

style="z-index: 1; position: relative; width: 50%; margin-left:25%;

align-items: center; height: 176px" >

<Columns>

<asp:BoundField DataField="ProductID" HeaderText="Product ID" >

<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle></asp:BoundField>

<asp:BoundField DataField="ProductName" HeaderText="Product Name" >

<ItemStyle HorizontalAlign="Left" Width="20%"></ItemStyle></asp:BoundField>

<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price"

DataFormatString="{0:c}" HtmlEncode="False" >

<ItemStyle HorizontalAlign="Right" Width="10%"></ItemStyle></asp:BoundField>

<asp:BoundField DataField="Description" HeaderText="Description">

<ItemStyle HorizontalAlign="right" Width="10%"></ItemStyle></asp:BoundField>

</Columns>

</asp:GridView>

20

Page 21: CS 3870/CS 5870: Note05

Page Updating

• Display record one at a time

• Display the first record for the first visit

• Display the same record for return visit

• Need Session variable

• Begin with “Prog3_”

21

Page 22: CS 3870/CS 5870: Note05

Session Variables

Sub Session_Start(. . .)

Session(“Prog3_Index”) = 0

End Sub

Protected Sub Page_Load(…) Handles Me.Load

DisplayRow(Session("Prog3_Index"))

End Sub

22

Page 23: CS 3870/CS 5870: Note05

Display RecordProtected Sub Page_Load(…) Handles Me.Load

DisplayRow(Session(“Prog3_Index”))

End Sub

Private Sub DisplayRow(Index As Integer)

Dim row As Data.DataRow

row = SQLDataClass.tblProduct.Rows(index)

‘ May need formatting

txtID.Text = row(0)

txtName.Text = row(1)

txtPrice.Text = row(2)

txtDescription.Text = row(3)

End Sub

23

Page 24: CS 3870/CS 5870: Note05

Navigation Buttons

Partial Class Prog3_Updating

Protected Sub Button6_Click(…) Handles btnNext.Click

Session(“Prog3_Index”) += 1

DisplayRow(Session(“Prog3_Index”))

End Sub

Protected Sub Button6_Click(…) Handles btnPrevious.Click

Session(“Prog3_Index”) -= 1

DisplayRow(Session(“Prog3_Index”))

End Sub

24

Page 25: CS 3870/CS 5870: Note05

Enable/Disable Buttons

Could make a private Sub.

Your choice.

25

Page 26: CS 3870/CS 5870: Note05

Navigation Buttons

Partial Class Prog3_Updating

Protected Sub Button6_Click(…) Handles btnNext.Click

Session(“Prog3_Index”) += 1

DisplayRow(Session(“Prog3_Index”))

EnableDisableButtons()

End Sub

Protected Sub Button6_Click(…) Handles btnPrevious.Click

Session(“Prog3_Index”) -= 1

DisplayRow(Session(“Prog3_Index”))

EnableDisableButtons()

End Sub

26

Page 27: CS 3870/CS 5870: Note05

Enable/Disable Buttons

Sub EnableDisableButtons()

if Session(“Prog3_Index”) = 0 Then

ElseIf Session(“Prog3_Index") =

SQLDataClass.tblProduct.Rows.Count - 1 Then

Else

End If

End Sub

27

Page 28: CS 3870/CS 5870: Note05

SQL Statements

Update Product

Set ProductName = ‘NewName’,

UnitPrice = newPrice

Description = ‘NewDescription’,

Where ProductID = ‘theID’;

Insert Into Product

Values(‘ID’, ‘Name’, Price, ‘Description’);

Delete From Product

Where ProductID = ‘theID’;

28

Page 29: CS 3870/CS 5870: Note05

Button Update

‘ Page Updating.Aspx

‘ID not to be modified

Protected Sub Button6_Click(…) Handles btnUpdate.Click

‘Dim theID As String = txtID.Text

Dim oldID As String =

SQLDataClass.tblProduct.Rows(Session(“Prog3_Index"))(0)

Dim newName As String = txtName.Text

Dim newPrice As Double = txtPrice.Text

Dim newDesc As String = txtDesc.Text

SQLDataClass.UpdateProduct(theID, newName,

newPrice, newDesc)

End Sub

29

Page 30: CS 3870/CS 5870: Note05

UpdateProduct

‘ SQLDataClass

Public Shared Sub UpdateProduct(theID As String,

newName As String, newPrice As Double, newDesc As String)

‘ Building SQL statement with variables

prodCmd.CommandText = ". . ."

Try

con.Open()

prodCmd.ExecuteNonQuery()

Catch ex

Throw ex

Finally

con.Close()

End Try

End Sub 30

Page 31: CS 3870/CS 5870: Note05

UpdateProduct

‘ SQLDataClass

Public Shared Sub UpdateProduct(theID As String,

newName As String, newPrice As Double, newDesc As String)

prodCmd.CommandText =

" Update Product" & _

" Set ProductName = ‘newName', " & _

" UnitPrice = newPrice, " & _

" Description = ‘newDesc'" & _

" Where ProductID = 'theID‘”

Try

. . .

End Try

End Sub

Incorrect! 31

Page 32: CS 3870/CS 5870: Note05

UpdateProduct

‘ SQLDataClass

Public Shared Sub UpdateProduct(theID As String,

newName As String, newPrice As Double)

‘ Building SQL statement with variables

prodCmd.CommandText =

" Update Product " & _

" Set ProductName = " & newName & “, " & _

" UnitPrice = " & newPrice & ", " & _

" Description = " & newDesc & _

" Where ProductID = " & theID

Try

. . .

End Try

End Sub

Incorrect! 32

Page 33: CS 3870/CS 5870: Note05

UpdateProduct

‘ SQLDataClass

Public Shared Sub UpdateProduct(theID As String,

newName As String, newPrice As Double)

‘ Building SQL statement with variables

prodCmd.CommandText =

" Update Product " & _

" Set ProductName = '" & newName & "'," & _

" UnitPrice = " & newPrice & ", " & _

" Description = '" & newDesc & "'" & _

" Where ProductID = '" & theID & "'“

Try

. . .

End Try

End Sub

Correct! 33

Page 34: CS 3870/CS 5870: Note05

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

Catch ex

Throw new Exception(ex.Message & “ ” &

prodCmd.CommandText)

Finally

con.Close() ‘ always close it

End Try

End Sub

34

Page 35: CS 3870/CS 5870: Note05

Button Update

‘ Page Updating.Aspx

Protected Sub Button6_Click(…) Handles btnUpdate.Click

Dim . . .

Try

DataClass.UpdateProduct(theID, newName,

newPrice, newDesc)

txtMsg.Text = "Record updated“

‘ must update tblProducts

SQLDataClass.getAllProduct()

Catch ex

txtMsg.Text = ex.Message ‘including myCmd.CommandText

End Try

End Sub 35

Page 36: CS 3870/CS 5870: Note05

36

Updating

‘ Page Updating.Aspx

Partial Class Prog3_Updating

Inherits System.Web.UI.Page

Protected Sub Page_Load(. . .) Handles Me.Load

txtMsg.Text = ""

DisplayRow(Session(“Prog3_Index"))

End Sub

Cannot Update Correctly!

Page 37: CS 3870/CS 5870: Note05

37

Updating

Partial Class Prog3_Updating

Inherits System.Web.UI.Page

Protected Sub Page_Load(. . .) Handles Me.Load

txtMsg.Text = ""

DisplayRow(Session(“Prog3_Index"))

End Sub

Cannot Update Correctly!

Post Back!

Page 38: CS 3870/CS 5870: Note05

38

PostBack

Partial Class Prog3_Updating

Inherits System.Web.UI.Page

Protected Sub Page_Load(. . .) Handles Me.Load

txtMsg.Text = “”

If Not IsPostBack Then

DisplayRow(Session(“Prog3_Index"))

Else

‘ Do not do anything

‘ Textboxes keep their values from client

End If

End Sub

Page 39: CS 3870/CS 5870: Note05

39

PostBack

Partial Class Prog3_Updating

Inherits System.Web.UI.Page

Protected Sub Page_Load(. . .) Handles Me.Load

txtMsg.Text = “”

‘ Else is not needed

If Not IsPostBack Then

DisplayRow(Session(“Prog3_Index"))

End If

End Sub

Page 40: CS 3870/CS 5870: Note05

Buttons

• btnNew– New– Save New

• btnDelete– Delete– Cancel

• Enabled/Disabled

40


Recommended