ASP .NET (2).pdf

16 downloads 3007 Views 1MB Size Report
ASP .NET (2). These slides are meant to be for teaching purposes only and only for the students that .... public class WebForm2ForADOpartOfASPandADOApplication : System.Web.UI. .... visible. Note, this looks different from how it would be.
ASP .NET (2)

These slides are meant to be for teaching purposes only and only for the students that are registered in CSE4413 and should not be published as a book or in any form of commercial product, unless written permission is obtained. 1

ASP and ADO (assumes knowledge of ADO) • • • •

We can access a database from within a Web Application (ASP .NET program), by combining ASP.NET and ADO.NET. This results to a 3-Tier architecture. 1st tier : client. See the GUI (front end) and interacts with it. 2nd tier: (asp) server. Hosts the ASP.NET program. – – – – –

Hosts the .aspx and .cs files as well as the program(s) that perform ADO activities. Does not host the Database. Receives requests from the 1st tier Processes requests. Becomes client to the 3rd tier, which hosts the data base. • • •



Poses DB queries to 3rd tier. Receives responses (query results) from 3rd tier. Processes responses of 3rd tier, assembles .html documents and sends results to 1st tier.

3rd tier: (db) server. Hosts the Database. – – –

Receives DB queries (typically in SQL) from 2nd tier. Processes queries and generates results. Sends results to 2nd tier.

2

begin

3-tier architecture …

1st tier (client to 2nd tier)

2nd tier (server to 1st tier;

3rd tier (server to

client to 3rd tier)

2nd tier)

Client’s request received and a web page is generated.

3

1st tier (client to 2nd tier)

User clicks “Show data” button.

2nd tier (server to 1st tier;

… 3rd tier (server

client to 3rd tier)

to 2nd tier)

2nd tier receives response and processes event: 1. Creates query 2. Submits query to 3rd tier

Receives query

Process Query 4

1st tier (client to 2nd tier)

2nd tier (server to 1st tier; client to 3rd tier)

Receive results (and places result into DataSet).

3rd tier (server to 2nd tier)

Generate results

end 5

Example

User is prompted for ID and password. 6

Example …

1. User typed ID and password. 2. Request was sent to 2nd tier. 3. 2nd tier checked … decided that (ID,Pass) is not valid. 7

Example …

1. 2. 3. 4.

User typed again ID and password. Request was sent to 2nd tier. 2nd tier checked … decided that (ID,Pass) is valid. Responded back to 1st tier that can now proceed to retrieve data.

User presses button. 8

Example …

1. 2.

Server received button event, Processed event a) Create query, connection to DB, and sent query to 3rd tier. b) Received results of query from 3rd tier. c) Placed query results into DataGrid. d) Created web page shown here. e) Sent web page to 1st tier.

9

The code This application has two .aspx files and two .cs files.

This .cs file handles the login process.

This .cs file handles the DB access process.

10

The code … using using using using using using using

System.Data; System.Drawing; System.Web; System.Web.SessionState; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.HtmlControls;

namespace WebApplication3ASPwithADO { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e) { }

11

… #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged); this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion

12

private void Button1_Click(object sender, System.EventArgs e) { string userID = TextBox1.Text; string userPassword = TextBox2.Text; string loginResult = ValidateUser(userID, userPassword); if (!loginResult.Equals( "Welcome") ) { Label3.Text = "INVALID LOGIN! Try again ... "; TextBox1.Text = ""; TextBox2.Text = ""; Page_Load(this, e); // reload page } else { // load and display the ADO related form

… If invalid login, clear textboxes and reprompt.

Response.Redirect( "WebForm2ForADOpartOfASPandADOApplication.aspx"); } }

!!! Usage of a second Form private string ValidateUser(string userID, string userPassword) here. In case of valid login, { open another Form that if (userID.Equals( "abc") && userPassword.Equals( "pass")) { performs the database return "Welcome"; access. This is not necessary, } but it makes the code more else modular and convenient to { write. return "Invalid login."; } } private void TextBox1_TextChanged(object sender, System.EventArgs e) { 13 }}}

WebForm2ForADOpartOfASPandADOApplication.aspx .cs

using using using using

System.Web.SessionState; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.HtmlControls;

namespace WebApplication3ASPwithADO { /// /// Summary description for WebForm2ForADOpartOfASPandADOApplication. /// public class WebForm2ForADOpartOfASPandADOApplication : System.Web.UI.Page { protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1; protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1; protected System.Data.DataSet dataSet1; protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; protected System.Data.OleDb.OleDbConnection oleDbConnection1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here }

14

#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); The }



connection to DB.

/// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand(); this.dataSet1 = new System.Data.DataSet(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""E:\4413 -- ecommerce course\__WINTER 2006\MySlides\_Slides _3 ADO NET slides\ADO .NET My code tests\MyStudentsDB.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

15

// // oleDbDataAdapter1 // this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1; this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Enroll", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("cno", "cno"), new System.Data.Common.DataColumnMapping("dname", "dname"), new System.Data.Common.DataColumnMapping("grade", "grade"), new System.Data.Common.DataColumnMapping("secno", "secno"), new System.Data.Common.DataColumnMapping("sid", "sid")})}); // // oleDbSelectCommand1 //

… The query.

this.oleDbSelectCommand1.CommandText = "SELECT cno, dname, sid, grade FROM Enroll WHERE (sid = 104)"; this.oleDbSelectCommand1.Connection = this.oleDbConnection1; // // oleDbInsertCommand1 // this.oleDbInsertCommand1.CommandText = "INSERT INTO Enroll(cno, dname, grade, secno, sid) VALUES (?, ?, ?, ?, ?)"; this.oleDbInsertCommand1.Connection = this.oleDbConnection1; this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("cno", System.Data.OleDb.OleDbType.Integer, 0, "cno")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("dname", System.Data.OleDb.OleDbType.VarWChar, 255, "dname")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("grade", System.Data.OleDb.OleDbType.Double, 0, "grade")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("secno", System.Data.OleDb.OleDbType.Integer, 0, "secno")); this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("sid", System.Data.OleDb.OleDbType.Integer, 0, "sid"));

16

…/

// // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US"); this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); } #endregion

private void Button1_Click(object sender, System.EventArgs e) { Label1.Text = ".. connectiing to DB..."; Execute query, receive oleDbConnection1.Open(); // open DB result (at 2nd tier) and Label1.Text += "connected!"; // execute query oleDbDataAdapter1.Fill( dataSet1, "res");

put result into dataSet1.

Label1.Text += "... retrieved data!"; // display results to datagrid DataGrid1.DataBind(); Populate DataGrid with results (of dataSet1) and make it DataGrid1.Visible = true; } } }

visible. Note, this looks different from how it would be done normally in a windows application. (oleDbDataAdapter1.Fill( dataSet1, "res"); DataGrid1.SetDataBinding( dataSet1, "res"); ) 17

The end

18