1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.

Slides:



Advertisements
Similar presentations
Direct Data Access, Data Binding. Content Direct Data Access Data Binding Muzaffer DOĞAN - Anadolu University2.
Advertisements

ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Introduction to Database Processing with ADO.NET.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
ADO. NET. What is “ADO.Net”? ADO.Net is a new object model for dealing with databases in.Net. Although some of the concepts are similar to the classical.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Computer Science 317 Database Management Introduction to Web Access to Databases.
Chapter 9 Using the SqlDataSource Control. References aspx.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Welcome  30 Question & 30 Minutes  Question Will Move Automatically After 1 Min Your Exam Start Now Lecturer : Zalak Thakrar.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
Chapter 81 Building Data-Driven Web Applications Introduction to ASP.NET By Kathleen Kalata.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
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.
Overview of Data Access MacDonald Ch. 15 MIS 324 Professor Sandvig.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
A Simple Introduction. What is ADO.net? First the word ADO stands for ActiveX Data Objects And it is an integral part of.Net Framework of Microsoft hence.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
IT533 Lectures DataBinding Controls. Installations Microsoft® SQL Server® 2012 Express Download the sample sql script files from the course’s web site.
IT533 Lectures DataBinding Controls. Installations Microsoft® SQL Server® 2008 Express Download the sample MDF files from the course’s web site CodeWeek3.zip.
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
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.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Data Binding in ASP.NET.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
ASP.NET Rina Zviel-Girshin Lecture 5
Module 9: Accessing Relational Data Using Microsoft Visual Studio.NET.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Module 7: Accessing Data by Using ADO.NET
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Presented by Joseph J. Sarna Jr. JJS Systems, LLC
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
An Introduction to ADO.Net Marmagna Desai.NET Seminar, Fall-2003.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
Why ADO.NET Not your father’s Data Access.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
HNDIT Rapid Application Development
2314 – Programming Language Concepts Introduction to ADO.NET.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Module 4 Introduction ADO.NET.
C# .NET Software Development
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
.NET Data Access and Manipulation
ASP.NET Programming with C# and SQL Server First Edition
Introduction to ADO.NET
Introduction to Database Processing with ADO.NET
Introduction to Database Processing with ADO.NET
ADO.NET and Stored Procedures
ADO.NET Framework.
PROG Advanced Web Apps 5/23/2018 Notes on ADO.NET (1) Wendi Jollymore, ACES.
Programming the Web Using ASP.Net
Lecture 6 VB.Net SQL Server.
VB.NET Using Database.
Tonga Institute of Higher Education
Chapter 10 ADO.
Chapter 10 Accessing Database Files
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Presentation transcript:

1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4

2 Overview Data Binding Data Providers Data Connection Data Manipulations

3 Web-Application Development Client’s PC Web- server Database server Input Output ProcessingStorage Hardware components Web software components Web browser Network OS DB Management System Information processing Developer tools HTML XML JavaScript CSS VBScript ASP C# ASP.NET SQL DBMS

4 ASP.NET and Data Data access and reporting are very important to web-based systems. Usually data items received from DataBases. ASP.NET framework includes a rich set of classes (DataSet, DataReader) and controls (DataGrid, Repeater, DataList, ListBox) to work with database data. Web-page designer has to connect/to bind data to the control. Data can be a single value, a list of values or a collection of values.

5 Data Binding Data binding is a process of retrieving data from a source and dynamically associating it to a property of some visual element. In web-based systems a visual element can be an HTML tag or.NET control. Exists 2 types of data binding: simple connection between one piece of data and a server control and complex binds a list control (ListBox) or iterative control (Repeater) to one or more columns of data.

6 Data Source Usually data binding is performed to fill some list with selectable items from an imported data source like an ArrayList, a HashTable, a database (DataReader, DataView), an XML file or a script. Data is bind to the control via DataSource property. The value of DataSource defines the data source and initialized to collection of data items. and later in some script: rb.DataSource=someArrayList This is a logical link. Nothing happens. Later a DataBind() method is called to load data from associated data source.

7 Simple Data Binding Example void Page_Load() { if(!Page.IsPostBack) { ArrayList countries=new ArrayList(); countries.Add("Norway"); countries.Add("Sweden"); countries.Add("France") countries.Add("Israel") ddl.DataSource=countries; DataBind(); } } A postback of a page means posting back to the same page. You can differentiate between the page’s first request and any postbacks by using the Page.IsPostback property. The Page_Load method runs EVERY time the page is loaded.

8 Simple Data Binding Example void displayMessage(Object s,EventArgs s){ lbl.text="Your favorite country is: " + ddl.SelectedItem.Text; }

9 Output onSelectedIndexChanged ="displayMessage"

10 DataBind() method DataBind() is a method of the Page class and all server controls. When DataBind method on a parent control is called it cascades to all of the children of the control. Calling DataBind on the Page causes all data binding expressions on the page to be evaluated. DataBind() method causes all data binding expressions on the page to be evaluated evaluates all expressions within the page evaluates all data bound properties of the controls.

11 DataBind() method Syntax of calling DataBind on the Page: Page.DataBind() or DataBind() DataBind is commonly called from the Page_Load event. protected void Page_Load(Object Src, EventArgs E) { DataBind(); // or Page.DataBind(); } However it can be called from another procedure (next example).

12 Example void SubmitBtn_Click(Object sender, EventArgs e) { Page.DataBind(); } Data binding to a property of another server control Jerusalem Haifa Tel-Aviv Lod Netanya Hadera Ashdod Selected City: ' runat="server" ID="Label1"/>

13 Output Rather than explictly get the variable from the “ CityList" ' and then manipulate a label control a Page.DataBind call is performed. This evaluates any expressions within the page, including - Rather than explictly get the variable from the “ CityList" ' and then manipulate a label control a Page.DataBind call is performed. This evaluates any expressions within the page, including -

14 DataBinding with a DataView A DataView is a great class for presenting data to the user in a more user-friendly format. A DataView represents a databindable, customized view of a DataTable. The DataView and DataTable classes are defined in the System.Data namespace. A DataTable represents one table of in-memory data. Data columns should be defined and added to DataTable dt (dt.Columns.Add ) and later data rows should be added (dt.Rows.Add). To view a DataTable you need to use a DataView or a DataSet class.

15 Example void Page_Load(Object sender, EventArgs e ) { if (!Page.IsPostBack) { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32))); dt.Columns.Add(new DataColumn("StringValue", typeof(string))); dt.Columns.Add(new DataColumn("DateTimeMsec", typeof(Int32))); for (int i = 1; i <= 5; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Item " + i.ToString(); dr[2] = DateTime.Now.Millisecond; dt.Rows.Add(dr); }

16 Example dgr.DataSource = new DataView(dt); dgr.DataBind(); } Databinding to a DataView <asp:DataGrid id="dgr" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" HeaderStyle-BackColor="#aaaadd“ />

17 Output DataGrid is a data bound list control that displays the items from some data source in a table.

18 Complex Data Binding A complex data binding binds a list control (ListBox, DropDownList) or iterative control (Repeater) to one or more than one data element typically more than one record in a database or to more than one of any other type of bindable data elements. Records of data usually retrieved from some database. The ASP.NET framework includes the ADO.NET data access technology for working with databases and other OLE DB data sources. ADO is ActiveX Data Object. ADO.NET is a set of classes to access and work with databases. ADO.NET is an evolution of the ADO data access model of Microsoft.

19 Steps of data connection and data manipulations 3 steps should be done to retrieve some data from DB 1. A connection object created to represent a physical connection to some data store (an SQL Server or an XML file). 2. A command object created to represent a directive to retrieve from (select) or manipulate (insert, update, delete) the data store. 3. A dataset object created to represent the actual data. Note that DataSets are always disconnected from their source connection and data model and can be modified independently. Command Connection DataSet DB

20 Data Providers In ADO.NET different namespaces and classes are created to work with different data providers: System.Data.SqlClient – Microsoft SQL Server 7.0 or higher System.Data.OleDb – OLE DB provider, such as Microsoft Access System.Data.OracleClient – Oracle Database server. In our course you will work with SQL Server using System.Data.SqlClient namespace.

21 System.Data.SqlClient System.Data.SqlClient namespace includes 3 classes to perform data connection and data manipulation: SqlConnection SqlCommand SqlDataReader Database SqlCommand SqlDataReader SqlConnection

22 Opening a connection To work with SQL Server the following directive should be added to ASP.Net pages: The first step is to open a database connection. SqlConnection class is used to establish a connection to SQL Server database. SqlConnection con; con=new SqlConnection("server=localhost;uid=sa;database=asp" ); con.open();

23 SqlConnection An SqlConnection class instance should be created (con=new SqlConnection()). It is initialized by passing a connection string to the constructor of SqlConnection class. The connection string should provide all necessary location and authentication information to connect to SQL server. server=localhost;uid=sa;pwd=secret;database=asp where server=localhost (specifies a ServerName), uid=‘sa’ (specifies a user sa=server admin), pwd (specifies a password if required),Database=‘asp’ specifies the Database Name Finally the connection is opened by calling an open() method - con.open().

24 SqlCommand An SqlCommand class represents an SQL statement (query) or a stored procedure. SqlCommand myCommand = new SqlCommand("select * from Table", con); where a constructor specifies statement/query to perform and using which database connection. Later this object uses an ExecuteReader() method to retrieve the result of the query. The result may return non values (ExecuteNonQuery - update) or to return a single value (ExecuteScalar - count) or to return a DataReader(ExecuteReader). If a record set is returned then it is stored in SqlDataReader object.

25 Partial Example SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;database=asp"); SqlCommand myCommand = new SqlCommand( "UPDATE Table SET phone='(800) ' WHERE au_id = ' '", myConnection); myCommand.Connection.Open(); myCommand.ExecuteNonQuery(); myCommand.Connection.Close();

26 SqlDataReader SqlDataReader class represents a stream of database records returned from a SQL statement (query) or a stored procedure. SqlDataReader dr = myCommand.ExecuteReader(); It is a forward-only, read-only access to a set of rows returned from a SQL server database. Later some control should update it’s DataSource property to this DataReader: MyDataGrid.DataSource = dr; MyDataGrid.DataBind();

27 Final step The final step do not forget to close the DataReader: dr.Close(); to close the Connection: con.Close();

28 Example void Page_Load(Object sender, EventArgs e) { SqlConnection con = new sqlConnection(" Server=localhost;uid=sa;database=pubs "); con.Open(); SqlCommand cmd = new SqlCommand( " Select au_name From Authors ", con ); SqlDataReader dtrAuthors = cmd.ExecuteReader(); while ( dtrAuthors.Read()) { Response.Write( " " ); Response.Write( dtrAuthors[ "au_name" ] );} dtrAuthors.Close(); con.Close(); } Read method returns true if there is a next record to read Gets a value for the field called au_name

29 Any Questions?