1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database.

Slides:



Advertisements
Similar presentations
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Advertisements

1 CS 3870/CS 5870: Lab4 Save with Invalid Price Keep Order After Updating.
User Controls, Master Pages, GridView. Content User Controls Styles, Themes, Master Pages Working with Data GridView Muzaffer DOĞAN - Anadolu University2.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
CSCI 6962: Server-side Design and Programming
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Chapter 10 Managing Data with ASP.NET. ASP.NET 2.0, Third Edition2.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
ASP.NET Part 4 Instructor: Charles Moen CSCI/CINF 4230.
Navigating database with windows forms.. Tiered applications  Provide a means to develop many presentations of the same app  Makes changes to the back.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
1/36 Database Programming with Visual Basic.Net and MS Access IKE Lab. Yunho Song Database Management and Analysis.
UNIT 9.2 Learning Objectives A Real world Application of Databases – The Money Ball Example The Money Ball Example Data Warehouses – The Role of Data Warehouses.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
1 CS 3870/CS 5870: Note 11 Authentication and Authorization Membership Provider.
CS 3870/CS 5870 Web Protocols, Technologies and Applications.
Unit 8.3 Learning Objectives Insert users into the ASP.NET Membership system from code Capture data being sent to the database Capture Exceptions that.
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
1 CS 3870/CS 5870: Note 13 Lab 6 Authentication and Authorization Roles Management.
1 CS 3870/CS 5870: Note 07 Lab 3 Lab 4 Test 1: Two Tables.
1 CS 3870/CS 5870: Note 20 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The.
1 CS387/CS587: Note 08 Shopping Bag DataTable. 2 DataClass Public Shared Function NewShoppingBag() As Data.DataTable Dim bag As New Data.DataTable bag.Columns.Add("Product.
1 Database Design and Development: A Visual Approach © 2006 Prentice Hall Chapter 12 DATABASE DESIGN AND DEVELOPMENT: A VISUAL APPROACH Chapter 12 Accessing.
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 CS 3870/CS 5870: Note 12 Authentication and Authorization Membership Provider.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
1 CS 3870/CS 5870: Note 16 Web User Controls. Prog 7 Copy Prog6 to Prog7 Modify all files for Prog7 Remove Web.config from sub-folders Make sure Prog7.
1 CS387/CS587: Note04 Lab 3. 2 Master Page All Web pages will be similar Should be created before other web pages Add New Items Controls on the Master.
1 CS 3870/CS 5870: Note 13 Web Service. 2 What is Web Service? Providing functionality online to other Web applications SOAP Simple Object Access Protocol.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
1.NET Web Forms Applications: Main Form © 2002 by Jerry Post.
1 CS 3870/CS 5870: Note 14. Prog5 Due 10 PM Wednesday, Oct 21 Authentication and Authorization 2.
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Unit 8.2 Learning Objectives Data Warehouses – The Role of Data Warehouses The Role of Data Warehouses – Group Exercise Accessing Data in Views – Accessing.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
1. Prog3 Try and Catch Testing 2 Catching Database Errors Protected Sub DetailsView1_ItemDeleted( sender As Object, e As DetailsViewDeletedEventArgs)
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Creating Data Base & Sql Data Source
CS 3870/CS 5870 Web Service.
Unit 9.1 Learning Objectives Data Access in Code
Web Application with Database
CS 3870/CS 5870 Web User Controls.
Session Variables and Post Back
Unit 8.2 How data can be used Accessing Data in Views
Web Application with Dataase
Unit 9.2 Database access from code Database Cycle Review
CS 3870/CS 5870 Web User Controls Events (II).
CS 3870 Prog5 Shopping Bag.
Using List Controls with SQL Server
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Creating Data Base & Sql Data Source
CS 3870/CS 5870 Test 2 (50 points) Thursday
Web Application with Dataase
Presentation transcript:

1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database

2 Master Page All Web pages will be similar Should be created before other web pages Add New Items Master Page Check “Place code in separate file” Uncheck “Select master page” –Could create a master page based on another master page May be incomplete

3 Controls on the Master page Form –All controls should be inside Form for dynamic pages Two ContentPlaceHolder controls One in and one in Leave ContentPlaceHolder empty Add controls before/after ContentPlaceHolder –Title and Names –Adding navbar –External CSS file

4 CSS File body { padding-left: 12em; }.navbar { list-style-type: none; padding: 0; margin: 0; position: absolute; top: 4em; left: 1em; width: 11em }

5 Navbar All Products Updating Products Shopping No links yet!

Content Pages Create the three pages using the master page –Web Form (with master) –Select master page –Make sure selecting folder first No Form control on content pages Control Content –ContentPlaceHolderID references ContentPlaceHolder control on master page –Could be empty for 6

7 Complete the Navbar on Master Page All Products Updating Products Shopping

8 The Database UWPCS3870 SQL Server Express on Xray User: jim (not case sensitive) Password: UWPCS3870 (case sensitive)

Table Product Four Columns ProductID : nchar(3), primary key, not updatable ProductNmae: nvarchar(50) UnitPrice : smallmoney Description : nvarchar(MAX), allow nulls 9

10 Accessing Database Data Source Controls –SqlDataSource –AccessDataSource –... Code class –Connection –Command –DataAdpater –AdapterBuilder Prog3 –Use Code class

ASP.NET Folders Solution Explorer Right click Web site Add ASP.NET Folder App_Code (App_Data)... 11

SQLDataClass Code class in folder App_Code No code module All variables and procedures should be Shared Otherwise, need to create object 12

Variables Public Class SQLDataClass Private Const ConStr As String = "Data Source=Xray\Sqlexpress;” & “Initial Catalog=UWPCS3870;Persist Security Info=True;” & “User ID=jim;Password=UWPCS3870" 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. 13

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 14

Retrieve Data Records ‘ Gets the table records and the table schema Public Shared Sub getAllProdcts() ‘ 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 15

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

Binding Gridview Protected Sub Page_Load(...) Handles Me.Load DataClass.getAllProducts() GridView1.DataSource = DataClass.tblProducts GridView1.DataBind() End Sub Refill the data table for each page request. 17

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_” 18

Session Variables Sub Session_Start(...) Session(“Prog3_Index”) = 0 End Sub Protected Sub Page_Load(…) Handles Me.Load DisplayRow(Session(“Prog3_Index”)) End Sub 19

Display Record 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 20

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 21

Enable/Disable Buttons Could make a private Sub. Your choice. 22

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 23

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’; 24

Button Update ‘ID not to be modified Protected Sub Button6_Click(…) Handles btnUpdate.Click Dim theID As String = txtID.Text 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 25

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

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

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

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

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 ‘ To see what is wrong Throw ex(prodCmd.CommandText) End Try End Sub 30

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 Catch ex Throw new Exception(ex.Message & “ ” & myCmd.CommandText) Finally con.Close()‘ always close it End Try End Sub 31

Button Update Protected Sub Button6_Click(…) Handles btnUpdate.Click Dim... Try DataClass.UpdateProduct(theID, newName, newPrice, newDesc) ‘ must update tblProducts SQLDataClass.getAllProduct() Catch ex txtMsg.Text = ex.Message ‘including myCmd.CommandText Finally con.Close()‘ always close it End Try End Sub 32

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

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

35 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

Buttons btnNew –New –Save New btnDelete –Delete –Cancel Enabled/Disabled 36