ASP .NET (2) - York University Lecture Notes/week 8-1-2.pdf · 2 ASP and ADO (assumes knowledge of...
Transcript of ASP .NET (2) - York University Lecture Notes/week 8-1-2.pdf · 2 ASP and ADO (assumes knowledge of...
1
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.
2
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.
3
3rd tier (server to 2nd tier)
2nd tier (server to 1st tier; client to 3rd tier)
Client’s request receivedand a web page is generated.
3-tier architecture …1st tier (client to 2nd tier)
begin
4
…2nd tier (server to 1st tier; client to 3rd tier)
3rd tier (server to 2nd tier)
2nd tier receives response and processes event:1. Creates query2. Submits query to 3rd tier
Receives query
Process Query
1st tier (client to 2nd tier)
User clicks “Show data” button.
5
3rd tier (server to 2nd tier)
Generate results
2nd tier (server to 1st tier; client to 3rd tier)
Receive results (and places result
into DataSet).
1st tier (client to 2nd tier)
end
6
Example
User is prompted for ID and password.
7
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.
8
Example …
1. User typed again ID and password. 2. Request was sent to 2nd tier.3. 2nd tier checked … decided that (ID,Pass) is valid.4. Responded back to 1st tier that can now proceed to
retrieve data.
User presses button.
9
Example …
1. Server received button event, 2. 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.
10
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.
11
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){
}
12
…#region Web Form Designer generated codeoverride 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
13
…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
Response.Redirect( "WebForm2ForADOpartOfASPandADOApplication.aspx");}
}
private string ValidateUser(string userID, string userPassword) {
if (userID.Equals( "abc") && userPassword.Equals( "pass")){
return "Welcome";}else {
return "Invalid login.";}
}private void TextBox1_TextChanged(object sender, System.EventArgs e){}}}
If invalid login, clear textboxes and re-
prompt.
!!! Usage of a second Form here. In case of valid login,
open another Form that performs the database
access. This is not necessary, but it makes the code more modular and convenient to
write.
14
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}
15
…#region Web Form Designer generated codeoverride 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.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";
The connection to DB.
16
…// // 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//
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"));
The query.
17
…/// // 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 DBLabel1.Text += "connected!";
// execute query oleDbDataAdapter1.Fill( dataSet1, "res");
Label1.Text += "... retrieved data!";
// display results to datagridDataGrid1.DataBind();DataGrid1.Visible = true;
}}}
Execute query, receive result (at 2nd tier) and
put result into dataSet1.
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"); )
18
The end