Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.

Slides:



Advertisements
Similar presentations
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Advertisements

Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Microsoft Office Excel 2013 Core Microsoft Office Excel 2013 Core Courseware # 3253 Lesson 8: Macros, Importing and Exporting Data.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Access Lesson 2 Creating a Database
Mark Dixon Page 1 05 – Database Design: Sub-forms.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Master Detail Page Set Enabling Users to See More Detailed Data.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Database Updates Made Easy In WebFocus Using SQL And HTML Painter Sept 2011 Lender Processing Services 1.
Access Tutorial 10 Automating Tasks with Macros
CONFIGURING WINDOWS SERVER MIS 424 Professor Sandvig.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Mark Dixon 1 20 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Server Side Programming ASP1 Server Side Programming Database Integration (cont.) Internet Systems Design.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
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.
® Microsoft Office 2010 Access Tutorial 2 Building a Database and Defining Table Relationships.
Office 2003 Advanced Concepts and Techniques M i c r o s o f t Access Project 6 Switchboards, PivotTables, and PivotCharts.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Mark Dixon Page 1 Web-Application Development Workshop.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
® Microsoft Office 2013 Access Creating a Database.
Tutorial 9 Using Action Queries and Advanced Table Relationships.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Microsoft Access 2013 ®® Tutorial 10 Automating Tasks with Macros.
COMPREHENSIVE Access Tutorial 12 Managing and Securing a Database.
® Microsoft Access 2010 Tutorial 12 Managing and Securing a Database.
Unit 5, Lesson 1 Working with Databases. Objectives Identify the parts of the Access screen. Identify the parts of the Access screen. Understand the purpose.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.
Microsoft Access 2013 ®® Tutorial 12 Managing and Securing a Database.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 8 1 Microsoft Office Access 2003 Tutorial 8 – Integrating Access with the.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
XP New Perspectives on Creating Web Pages With Word Tutorial 1 1 Creating Web Pages With Word Tutorial 1.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Y.-H. Chen International College Ming-Chuan University Fall, 2004
18 – Databases: Structured Query Language
ASP and Database Connectivity
Setting the Permissions
17 – Persistent data storage: relational databases and ADO
Presentation transcript:

Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net

Mark Dixon 2 Questions: HTML in VB Are these correct (assume variables and fields exist)? f = f + r("Description") h = h + r(" Name") a = " " + a " " html = html + h = " " + h + " "   

Mark Dixon 3 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 4 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using server-side code to write data to databases Objectives, by end of this week’s sessions, you should be able to: –create an ASP web page that allows the user to store data in database

Mark Dixon 5 Advice Don’t –put anything on desktop –Especially database

Mark Dixon 6 Example: Person v1 (Specification) User requirement: –Display people's details from database online –need 2 pages: smith jones dixon list of people jones sally person's details

Mark Dixon 7 Example: PeopleList.aspx v1 Sub Page_Load() Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim cn As New OdbcConnection(cs) Dim cmd As OdbcCommand Dim r As OdbcDataReader Dim s As String cmd = New OdbcCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub

Mark Dixon 8 Example: PeopleList.aspx v2 Sub Page_Load() Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim cn As New OdbcConnection(cs) Dim cmd As OdbcCommand Dim r As OdbcDataReader Dim s As String cmd = New OdbcCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & " " s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub now links

Mark Dixon 9 Example: Person.aspx v2 Sub Page_Load() Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim sql As String Dim cn As New OdbcConnection(cs) Dim cmd As OdbcCommand Dim r As OdbcDataReader Dim s As String sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OdbcCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() s = "" If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Back to People List Surname: reads querystring (from previous page) displays data for selected record only

Mark Dixon 10 Example: Person v2 (Specification) User requirement: Display person’s details from database online –Change surname and save to database

Mark Dixon 11 Changing Data SQL –INSERT: inserts a new record INSERT INTO Person (Surname, Age) VALUES ('Smith', 21); –UPDATE: makes changes to specified record UPDATE Person Set Surname = 'Smith', Age = 21 WHERE id = 14; –DELETE: deletes specified record DELETE FROM Person WHERE id = 14

Mark Dixon 12 WARNING!! All changes permanent (no undo) WHERE clause is CRITICAL DELETE FROM Person; Will delete ALL records in table

Mark Dixon 13 Example: Person.aspx v3 (error) Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim cn As New OdbcConnection(cs) Sub Page_Load() Dim sql As String Dim cmd As OdbcCommand Dim r As OdbcDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OdbcCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Sub btnSave_Click(s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OdbcCommand Dim sql As String sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OdbcCommand(sql, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close End Sub Save button executes SQL UPDATE PROBLEM: Page_Load re-reads old surname first

Mark Dixon 14 Example: Person.aspx v3b Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim cn As New OdbcConnection(cs) Sub Page_LoadComplete(s As Object, e As EventArgs) Dim sql As String Dim cmd As OdbcCommand Dim r As OdbcDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OdbcCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Sub btnSave_Click(s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OdbcCommand Dim sql As String sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OdbcCommand(sql, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close End Sub Save button executes SQL UPDATE Fix: Use Page_LoadComplete

Mark Dixon 15 Example: Person.aspx v3c Dim cs As String = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + _ "Dbq=" + Server.MapPath("People.accdb") + ";" Dim cn As New OdbcConnection(cs) Dim sql As String Sub Page_Load () cn.Open() End Sub Sub btnSave_Click (s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OdbcCommand sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OdbcCommand(sql, cn) cmd.ExecuteNonQuery() End Sub Sub Page_LoadComplete (s As Object, e As EventArgs) Dim cmd As OdbcCommand Dim r As OdbcDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OdbcCommand(sql, cn) r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Page_Load: first Click events Page_LoadComplete: last

Mark Dixon 16 Database Permissions Generally –Read: works by default –Write: requires permissions Asp.Net pages run as user: –Visual Studio Logged in user –IIS ASP.Net Account NETWORKSERVICE IIS APPPOOL\DefaultAppPool

Mark Dixon 17 Database Permissions 1 Windows Explorer –Tools –Folder Options –View Tab Need to turn 'simple file sharing' off (as this disables the security tab in file properties)

Mark Dixon 18 Database Permissions 2 In order for ASP to write to a database –Need to give write access to Internet Guest Account for database file (People.mdb) Right-click on file in Windows Explorer (the following screens are for Windows XP)

Mark Dixon 19 Database Permissions 3 Click Security tab Click Add button

Mark Dixon 20 Database Permissions 4 Click Advanced button

Mark Dixon 21 Select Internet Guest Account IUSR_ … Click Find button Click user Click OK button Database Permissions 5

Mark Dixon 22 Database Permissions 6 Select Internet Guest Account Ensure write access is on Repeat for ASPNET account

Mark Dixon 23 Tutorial Exercise: Person Task 1: Get the Person (v1) example from the lecture working. Task 2: Modify your code, so that forename is displayed as well as surname (use a table). Task 3: Get the Person (v2 and v3) example from the lecture working. Task 3: Modify your code, so that a line of text is displayed confirming that data has been saved. Task 4: Modify your code, so that an add button is included, which allows a new record to be added. Task 5: Modify your code, so that a delete button is included, which allows the current record to be deleted.