Unit 9.1 Learning Objectives Data Access in Code

Slides:



Advertisements
Similar presentations
User Controls, Master Pages, GridView. Content User Controls Styles, Themes, Master Pages Working with Data GridView Muzaffer DOĞAN - Anadolu University2.
Advertisements

MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Figure 1. Hit analysis in 2002 of database-driven web applications Hits by Category in 2002 N = 73,873 Results Reporting 27% GME 26% Research 20% Bed Availability.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
CSCI 6962: Server-side Design and Programming
Chapter 9 Using the SqlDataSource Control. References aspx.
Chapter 10 Managing Data with ASP.NET. ASP.NET 2.0, Third Edition2.
Unit 8.2 Learning Objectives How data can be used – The Money Ball Example The Money Ball Example Data Warehouses – The Role of Data Warehouses The Role.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
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.
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.
MIS 3200 – Unit 4.2 ListItem controls – CheckBoxList – ListBox.
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.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
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.
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.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Web Database Programming Using PHP
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Sixth Edition
Y.-H. Chen International College Ming-Chuan University Fall, 2004
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Creating Data Base & Sql Data Source
Unit 7 Learning Objectives
Unit 8.1: Introducing Databases
PROG Advanced Web Apps 5/23/2018 Notes on ADO.NET (1) Wendi Jollymore, ACES.
Practical Office 2007 Chapter 10
Basic Database Concepts
Data Environment and Grouped Report
Web Database Programming Using PHP
Database application MySQL Database and PhpMyAdmin
Active Data Objects Binding ASP.NET Controls to Data
Unit 8.2 How data can be used Accessing Data in Views
Listing 9.1 ShowLocalConnection.aspx
Unit 9.2 Database access from code Database Cycle Review
Intro to PHP & Variables
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Unit 9.3 Learning Objectives Review database access in code
VISUAL BASIC INTRODUCTION TO DATA CONNECTIVITY.
Web DB Programming: PHP
Using List Controls with SQL Server
Chapter 10 ADO.
Adding Record To Your Database
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Creating Data Base & Sql Data Source
Training & Development
Active server pages (ASP.NET)
Web Development & Design Foundations with H T M L 5
MIS 3200 – Unit 5.1 Iteration (looping) Working with List Items
MIS 3200 – Unit 6.1 Moving between pages by redirecting
Active Data Objects Binding ASP.NET Controls to Data
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Unit J: Creating a Database
Presentation transcript:

Unit 9.1 Learning Objectives Data Access in Code Data Access in Design (Review) Five Step Sequence Hands-on

the ConnectionString (e.g. csFees) sqlDataSource (GUI) the SQL command the ConnectionString (e.g. csFees)

GUI Views DetailsView FormView GridView DataList ListView Number of records One Many Insert Yes No Edit/Update Delete Column Format

Accessing Data Database server SQL e.g. MS SQL Server SqlDataSource Database location (Connectionstring) What data (Command) What technology (Namespaces) Run! (DataReader) Browser e.g. FireFox Web server e.g. Windows Server Database server e.g. MS SQL Server HTML ASPX SQL ["1",“Registration Fee",5.00,”2”, “Vehicle License Fee",7.5,”3”, “Weight Fee“, 12.500] Parse (Loop in Reader)

Five Steps to Data in code The Five Step Plan Setup the page for the necessary technology Use Namespaces Setup the location to the database Create a Connection String Create a Command Object (SQL) The work required from the database Open the connection, Execute the Command, Create a Reader Execute! Loop through the Reader and read the data csFees myCommand GUI controls allow us to do a great deal but they can’t do everything .NET provides a number of objects that allow us to perform database manipulation in code The database objects are collected into three Namespaces A namespace is a way to uniquely identify a set of programming objects "1",“Registration Fee",5.00, ”2”, “Vehicle License Fee",7.50, ”3”, “Weight Fee“, 12.50

A. Get ready with Namespaces Namespaces are libraries of database functions Needed while working with database in code “Using” suggests that these functions be loaded The following must be added at the top of the .cs file

B.1 Connection – What is it? Called a Connection String (e.g. csFees) Stored in the web.config file Includes path and name to the database Is created automatically when an sds is configured

B.2 Connection – create in 3 steps Using its name from the web.config file And can be pulled into your code with the ConfigurationManager Create a connection string object in your “.cs” page Finally, create the Connection Object These two should match Also used with sds In Design View

C.1 Command Object – What is it? Equivalent to the GUI SQL Data Source Contains SQL, database driver, path, and name SQL commands: SELECT, INSERT, UPDATE, DELETE Two types: SELECT commands make DataReaders, and return data All other commends modify the data So they use ExecuteNonQuery & return a number

C.2 Command object – creating it Tells the database what to do Is created with an SQL string & the connection object The Connection Object was made in the previous slide

D. Executing the Command - sequence Open the connection Execute the command, create the reader Loop through the Reader AFTER the looping code, Close the connection // See next slide for looping code "1",“Registration Fee",5.00, ”2”, “Vehicle License Fee",7.50, ”3”, “Weight Fee“, 12.50

E. Parsing through the Reader Review the SELECT statement Retrieve the records if there are any “.Read()” returns TRUE if there is a record FALSE if no records are found Address columns by index or field name Data type is always string There are several ways to process the RecordSet but the simplest is probably to use a DataReader Returns one record at a time to the program when the .Read() method is called Read() returns a value of false if there are no (more) records to read, true if there was a record Can be used to control a loop “Registration Fee",5.00, “Vehicle License Fee",7.50, “Weight Fee“, 12.50 lblFees.Text += "<br />" + strDescription + " " + decFee.ToString("c2");

F. Closing the Connection Technically known as Garbage Collection Done to free up database server memory Critical step, causes Denial of Service on server myReader.Close(); myConnection.Close();

Parameters, a safety measure Are a security measure Are placeholders of a values within SQL Values need to supplied before execution Adding a parameter to a command. The first argument is the name of the parameter, the second argument is the datatype (when you type System.Data.SqlDbType. you will see a list of all the SQL datatypes and can pick the appropriate one. 2. Parameter is given a value 3. Matches the SQL data type in the database 1. Parameter appears

L1: Hands on Purpose: To read and display all the records in a database then read and display selected records Start a new page yourlastnameU9L1 in the Unit9 folder. Add heading, heading size Reading Fee Data Using Code Add a Button to the page Button ID, button text Add a Label to hold the results displayed Label ID, text

U9L1 - 2 Write code that needs to be executed for the button. Use the five step plan: Import namespaces (Step A) Create the connection (Steps B.2.2 & B.2.3) Create an SqlCommand object (Step C) Execute the command object (Step D)

U9L1 - 3 Read the data record in a loop (Step E) Copy the Description data from field 0 to a string variable Convert the Fee data from field 1 and store it in a decimal variable Accumulate the descriptions and fees in lblFees (without deleting previous entries) After the loop finishes, Close the Reader and the Connection (Step F) Add a try/catch block to the page Inside the try, put the risky code Inside the catch, display an appropriate error message

U9L1 - 4 Test your page, you should show fee descriptions and fee amounts, as shown on the right. Once that part works correctly skip a line after your label and type the words “Enter a test value:”, followed by a TextBox Set the (ID) of the TextBox to txtTest Validate for a positive decimal value

U9L1 - 5 Add a button on the line below the TextBox Set the (ID) to btnTest2 Set the Text to “Display fees greater than or equal to the test value” Double click the button to start creating the method Copy all the code from the btnReadFees_Click method and paste it in the new method Locate the SQL statement and change it to say SELECT [FeeDescription], [Fee] FROM Fees WHERE [Fee] >= @Fee

U9L1 - 6 Add a new line of code after the line where you create the sqlCommand Get the test value from the TextBox And create a parameter with it (See slide on adding Parameters) decimal decTest = Convert.ToDecimal(txtTest.Text); myCommand.Parameters.Add("@Fee", System.Data.SqlDbType.Money).Value = decTest;

U9L1 - 7 Test your page and check the results. You should get something like this (depending on what data you have in your database) Add the assignment to your profile page, copy everything to ASPNET and post a link to your profile page in the dropbox