ASP.NET and ADO.NET.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Introduction to ADO.NET Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Website Security ISYS 512. Cookies Data in Cookies System.Web Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Introduction to ADO.Net and Visual Studio Database Tools
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Working with GridView Control: Adding Columns. Adding Buttons to a Bound GridView: 1. Drag the WebProduct table from Data connection to a page 2. Demo.
Working with the Application Object. Session VS Application Object The Session object helps to preserve data on a per user basis. What if we want to initialize.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
Command Object’s ExecuteNonQuery Method ISYS 512.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
Coding ADO.NET Objects: Connection, Command, DataReader.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Coding ADO.NET Objects: Connection, Command, DataReader
Using List Controls with SQL Server
Coding ADO.NET Objects: Connection, Command, DataReader
Chapter 10 Accessing Database Files
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Shopping Cart Demo.
Command Object’s ExecuteNonQuery Method
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

ASP.NET and ADO.NET

WebForm Wizard Drag/Drop tables from Server Explorer to create GridView control automatically.

Binding with AccessdataSource or sqlDataSource Controls 1. Define an sqlDataSource/AccessDataSource control: Open ToolBox Data tab and double click sqlDataSource/AccessDataSource control Use the control’s Smart tag to configure the data source 2. Bind control to the data source.

Bind a GridView to a DataSource 1. Define a data source 2. Add a GridView control and click the smart tag to choose the data source. Note: We can configure the data source.

Bind a ListBox to a DataSource Example: Creating a webform with a listbox of CID and display Cname when a CID is selected. Add ListBox Click SmartTag to define DataSource Listbox DataTextField and DataValueField properties. ListBox SelectedIndexChanged Event.

Code Example protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { Response.Write(ListBox1.SelectedValue.ToString()); } Note 1: We can specify only one value field. Note 2: The Listbox PostBack property must set to true for this example.

Postback Postback is the process by which the browser posts information back to the server telling the server to handle the event, the server does so and sends the resulting HTML back to the browser. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser.

ASP.Net Controls’ AutoPostBack Property Button always triggers postback. Other controls, by default, this property is set to false.

ASP.Net and DataReader Demos Unlike window form, DataReader can be used as an ASP.Net control’s data source for binding

Using DataReader as a DataSource For a GridView protected void Page_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); GridView1.DataSource = objDataReader; GridView1.DataBind(); } Note: DataBind method

Controls of ListControl Base Class DropDownList, ListBox, CheckBoxList, RadioButtonList Properties: AutoPostBack DataSource DataTextField: The field in the datasource that provides the text to be used for the list items. DataValueField SelectedItem, SelectedValue, SelectedIndex Note: Value of the selected item: SelectedItem.Value Event: OnSelectedIndexChanged

Effect of Postback protected void Page_Load(object sender, EventArgs e) { ListBox1.Items.Add("Apple"); ListBox1.Items.Add("Orange"); ListBox1.Items.Add("Banana"); } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) Response.Write(ListBox1.SelectedItem);

Page.ISPostBack property: Test if a page is reloaded because of postback protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) ListBox1.Items.Add("Apple"); ListBox1.Items.Add("Orange"); ListBox1.Items.Add("Banana"); }

The Effects of PostBack on Bound Controls If the databinding is done in a PageLoad event procedure without checking postback, the databinding is repeated and the control is initialized to its original state. For list controls, such as ListBox, CheckboxList, and Radiobuttonlist, the selected item is no longer selected. Note: Demo previous example without checking postback.

Binding a DataReader to a ListBox DataSource: ListBox1.DataSource = objDataReader; DataTextField: ListBox1.DataTextField = "CID"; DataValueField: ListBox1.DataValueField = "CNAME"; DataBind: ListBox1.DataBind();

Binding a DataReader to a ListBox protected void Page_Load(object sender, EventArgs e) { // if (!Page.IsPostBack) string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); ListBox1.DataSource = objDataReader; ListBox1.DataTextField = "CID"; ListBox1.DataValueField = "CNAME"; ListBox1.DataBind(); } Note: Does not work without checking Postback

Customer/Orders Form protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); ListBox1.DataSource = objDataReader; ListBox1.DataTextField = "CID"; ListBox1.DataValueField = "CID"; ListBox1.DataBind(); }

Continue protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL ="select * from orders where cid= '" + ListBox1.SelectedValue.ToString() + "'"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); GridView1.DataSource = objDataReader; GridView1.DataBind(); } Note: How to display a message if selected customer has no order?

Example A RadioButtonList with customer ratings. RadiobuttonList has a selectedIndexChanged event similar to the listbox. Select a rating and display customers of the selected rating in a data grid.

Select Rating from a RadiobuttonList protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer where rating= '" + RadioButtonList1.SelectedValue + "'"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); GridView1.DataSource = objDataReader; GridView1.DataBind(); } Note: AutoPostBack must set to true.

ExecuteNonQuery Demp:Adding a new record string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQLInsert; strSQLInsert = "Insert into Customer values ('"; strSQLInsert += TextBox1.Text + "','" + TextBox2.Text + "','"; strSQLInsert += TextBox3.Text + "','" + TextBox4.Text + "')"; OleDbCommand objCommInsert = new OleDbCommand(strSQLInsert, objConn); Response.Write("Record added sucessfully"); objConn.Open(); try { objCommInsert.ExecuteNonQuery(); } catch (Exception ex) Response.Write(ex.Message); objConn.Close();

Demo:Reader and Update Use a DataReader to create a dropwdownList with customer CIDs.. Display selected customer data in textboxes. Update customer’s rating using Command object’s ExecuteNonQuery method.

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); ListBox1.DataSource = objDataReader; ListBox1.DataTextField = "CID"; ListBox1.DataValueField = "CID"; ListBox1.DataBind(); }

OleDbConnection objConn = new OleDbConnection(strConn); protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer where cid= '" + ListBox1.SelectedValue.ToString() + "'"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); try OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); objDataReader.Read(); TextBox1.Text = objDataReader["Cname"].ToString(); TextBox2.Text = objDataReader["City"].ToString(); TextBox3.Text = objDataReader["Rating"].ToString(); objConn.Close(); } catch (SystemException ex) Response.Write(ex.Message);

protected void Button1_Click(object sender, EventArgs e) { if (ratingChanged) string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQLUpd = "Update customer set rating = '" + TextBox3.Text + "'"; strSQLUpd = strSQLUpd + " where cid='"+ ListBox1.SelectedValue+ "'"; OleDbCommand objCommUpd = new OleDbCommand(strSQLUpd, objConn); objConn.Open(); objCommUpd.ExecuteNonQuery(); objConn.Close(); } Boolean ratingChanged = false; protected void TextBox3_TextChanged(object sender, EventArgs e) ratingChanged = true;

DataSet and Related Objects Tables DataView DataAdapter Commands Connection DataSource

Binding GridView to DataSet NOTE: Must also Import System.Data protected void Page_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet objDataSet = new DataSet(); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); GridView1.DataSource = objDataSet; GridView1.DataMember = "Customer"; GridView1.DataBind(); }

Binding a ListBox with Code public partial class WebForm9 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) if (!Page.IsPostBack) string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet objDataSet = new DataSet(); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); ListBox1.DataSource = objDataSet.Tables["Customer"]; ListBox1.DataTextField = "CID"; ListBox1.DataValueField = "Cname"; ListBox1.DataBind(); } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) TextBox1.Text = ListBox1.SelectedValue.ToString();

How PostBack Affect Dataset Object. 1 How PostBack Affect Dataset Object? 1. It will be recreated A RadiobuttonList to select Rating and display customers in gridview DataSet objDataSet = new DataSet(); DataView objDataView = new DataView(); protected void Page_Load(object sender, EventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); objDataView = objDataSet.Tables["customer"].DefaultView; GridView1.DataSource = objDataView; GridView1.DataBind(); } protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) objDataView.RowFilter = "rating = '" + RadioButtonList1.SelectedItem.ToString() + "'";

How PostBack Affect Dataset Object? 2. It will be lost protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); string strSQL = "select * from customer;"; OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConn); objAdapter.Fill(objDataSet, "Customer"); objDataView = objDataSet.Tables["customer"].DefaultView; GridView1.DataSource = objDataView; GridView1.DataBind(); }