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. 1 st tier : client. See the GUI (front end) and interacts with it. 2 nd 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 1 st tier Processes requests. Becomes client to the 3 rd tier, which hosts the data base. Poses DB queries to 3 rd tier. Receives responses (query results) from 3 rd tier. Processes responses of 3 rd tier, assembles.html documents and sends results to 1 st tier. 3 rd tier: (db) server. Hosts the Database. Receives DB queries (typically in SQL) from 2 nd tier. Processes queries and generates results. Sends results to 2 nd tier. 2
begin 1 st tier (client to 2 nd tier) 3-tier architecture 2 nd tier (server to 1 st tier; client to 3 rd tier) 3 rd tier (server to 2 nd tier) Client s request received and a web page is generated. 3
1 st tier (client to 2 nd tier) 2 nd tier (server to 1 st tier; client to 3 rd tier) 3 rd tier (server to 2 nd tier) User clicks Show data button. 2 nd tier receives response and processes event: 1. Creates query 2. Submits query to 3 rd tier Receives query Process Query 4
1 st tier (client to 2 nd tier) 2 nd tier (server to 1 st tier; client to 3 rd tier) 3 rd tier (server to 2 nd tier) Receive results (and places result into DataSet). 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 2 nd tier. 3. 2 nd tier checked decided that (ID,Pass) is not valid. 7
Example 1. User typed again ID and password. 2. Request was sent to 2 nd tier. 3. 2 nd tier checked decided that (ID,Pass) is valid. 4. Responded back to 1 st tier that can now proceed to retrieve data. User presses button. 8
Example 1. Server received button event, 2. Processed event a) Create query, connection to DB, and sent query to 3 rd tier. b) Received results of query from 3 rd tier. c) Placed query results into DataGrid. d) Created web page shown here. e) Sent web page to 1 st 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 System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebApplication3ASPwithADO / <summary> / Summary description for WebForm1. / </summary> 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); / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> 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") ) If invalid login, clear Label3.Text = "INVALID LOGIN! Try again... "; textboxes and reprompt. TextBox1.Text = ""; TextBox2.Text = ""; Page_Load(this, e); reload page else load and display the ADO related form Response.Redirect( "WebForm2ForADOpartOfASPandADOApplication.aspx"); private string ValidateUser(string userid, string userpassword)!!! Usage of a second Form here. In case of valid login, if (userid.equals( "abc") && userpassword.equals( "pass")) open another Form that performs the database return "Welcome"; access. This is not necessary, but it makes the code more else modular and convenient to return "Invalid login."; write. private void TextBox1_TextChanged(object sender, System.EventArgs e) 13
WebForm2ForADOpartOfASPandADOApplication.aspx.cs using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebApplication3ASPwithADO / <summary> / Summary description for WebForm2ForADOpartOfASPandADOApplication. / </summary> 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. / <summary> / Required method for Designer support - do not modify / the contents of this method with the code editor. / </summary> 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 -- e- commerce 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[] The query. 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 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..."; oledbconnection1.open(); open DB Label1.Text += "connected!"; execute query oledbdataadapter1.fill( dataset1, "res"); Label1.Text += "... retrieved data!"; / Execute query, receive result (at 2 nd tier) and put result into dataset1. DataGrid1.DataBind(); DataGrid1.Visible = true; display results to datagrid Populate DataGrid with results (of dataset1) and make it 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